1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27namespace PrestaShop\PrestaShop\Core\Search\Builder;
28
29use PrestaShop\PrestaShop\Core\Search\Filters;
30
31/**
32 * This builder does not modify the filters but instead saves them in database for
33 * each Employee, thus it can then be found by the RepositoryFiltersBuilder.
34 */
35final class PersistFiltersBuilder extends AbstractRepositoryFiltersBuilder
36{
37    /**
38     * @param Filters|null $filters
39     *
40     * @return Filters
41     *
42     * @throws \Doctrine\ORM\OptimisticLockException
43     */
44    public function buildFilters(Filters $filters = null)
45    {
46        if (null === $filters || !$this->employeeProvider->getId() || !$this->shopId || !$filters->needsToBePersisted()) {
47            return $filters;
48        }
49
50        $filterId = $this->getFilterId($filters);
51        if (empty($filterId) && (empty($this->controller) || empty($this->action))) {
52            return $filters;
53        }
54
55        $filtersToSave = $filters->all();
56        unset($filtersToSave['offset']);
57
58        if (!empty($filterId)) {
59            $this->adminFilterRepository->createOrUpdateByEmployeeAndFilterId(
60                $this->employeeProvider->getId(),
61                $this->shopId,
62                $filtersToSave,
63                $filterId
64            );
65        } else {
66            $this->adminFilterRepository->createOrUpdateByEmployeeAndRouteParams(
67                $this->employeeProvider->getId(),
68                $this->shopId,
69                $filtersToSave,
70                $this->controller,
71                $this->action
72            );
73        }
74
75        return $filters;
76    }
77}
78