-
Notifications
You must be signed in to change notification settings - Fork 512
/
AncestorIdFilter.php
126 lines (113 loc) · 4.17 KB
/
AncestorIdFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace Akeneo\Pim\Enrichment\Bundle\Elasticsearch\Filter\Field;
use Akeneo\Pim\Enrichment\Component\Product\Exception\InvalidOperatorException;
use Akeneo\Pim\Enrichment\Component\Product\Exception\ObjectNotFoundException;
use Akeneo\Pim\Enrichment\Component\Product\Query\Filter\FieldFilterHelper;
use Akeneo\Pim\Enrichment\Component\Product\Query\Filter\Operators;
use Akeneo\Pim\Enrichment\Component\Product\Repository\ProductModelRepositoryInterface;
use Akeneo\Tool\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface;
/**
* An ancestor is a product model that is either a parent or a grand parent.
* Look for documents having the given ancestor(s).
*
* Imagine the following tree:
* RPM
* \PM1
* \P11
* \P12
* \PM2
* \P21
*
* Using this filter with "IN LIST PM1" would return:
* \P11
* \P12
*
* @author Samir Boulil <samir.boulil@akeneo.com>
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class AncestorIdFilter extends AbstractFieldFilter
{
private const ANCESTOR_ID_ES_FIELD = 'ancestors.ids';
/** @var IdentifiableObjectRepositoryInterface */
private $productModelRepository;
/**
* @param ProductModelRepositoryInterface $productModelRepository
* @param array $supportedFields
* @param array $supportedOperators
*/
public function __construct(
ProductModelRepositoryInterface $productModelRepository,
array $supportedFields,
array $supportedOperators
) {
$this->productModelRepository = $productModelRepository;
$this->supportedFields = $supportedFields;
$this->supportedOperators = $supportedOperators;
}
/**
* {@inheritdoc}
*/
public function addFieldFilter($field, $operator, $values, $locale = null, $channel = null, $options = []): void
{
if (null === $this->searchQueryBuilder) {
throw new \LogicException('The search query builder is not initialized in the filter.');
}
if (!$this->supportsOperator($operator)) {
throw InvalidOperatorException::notSupported($operator, AncestorIdFilter::class);
}
$this->checkValues($values);
switch ($operator) {
case Operators::IN_LIST:
$this->searchQueryBuilder->addShould(
[
[
'terms' => [
self::ANCESTOR_ID_ES_FIELD => $values,
],
]
]
);
break;
case Operators::NOT_IN_LIST:
$mustNotClause = [
'terms' => [
self::ANCESTOR_ID_ES_FIELD => $values,
],
];
$filterClause = [
'exists' => [
'field' => self::ANCESTOR_ID_ES_FIELD,
],
];
$this->searchQueryBuilder->addMustNot($mustNotClause);
$this->searchQueryBuilder->addFilter($filterClause);
break;
}
}
/**
* Checks the value we want to filter on is valid
*
* @param mixed $values
*
* @throws ObjectNotFoundException
*/
private function checkValues($values): void
{
FieldFilterHelper::checkArray(self::ANCESTOR_ID_ES_FIELD, $values, static::class);
foreach ($values as $value) {
FieldFilterHelper::checkString(self::ANCESTOR_ID_ES_FIELD, $value, static::class);
if (!$this->isValidId($value)) {
throw new ObjectNotFoundException(
sprintf('Object "product model" with ID "%s" does not exist', $value)
);
}
}
}
private function isValidId(string $value): bool
{
$id = str_replace('product_model_', '', $value);
return null !== $this->productModelRepository->findOneBy(['id' => $id]);
}
}