Completed
Push — master ( 8c5037...eed80c )
by Gaetano
07:22
created

LocationMatcher::findLocationsByContentRemoteIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\Content\Query;
6
use Kaliop\eZMigrationBundle\API\Collection\LocationCollection;
7
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
8
9
/**
10
 * @todo extend to allow matching by visibility, subtree, depth, object state, section, creation/modification date...
11
 * @todo optimize the matches on multiple conditions (and, or) by compiling them in a single query
12
 */
13
class LocationMatcher extends RepositoryMatcher
14
{
15
    use FlexibleKeyMatcherTrait;
16
17
    const MATCH_CONTENT_ID = 'content_id';
18
    const MATCH_LOCATION_ID = 'location_id';
19
    const MATCH_CONTENT_REMOTE_ID = 'content_remote_id';
20
    const MATCH_LOCATION_REMOTE_ID = 'location_remote_id';
21
    const MATCH_PARENT_LOCATION_ID = 'parent_location_id';
22
    const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id';
23
24
    protected $allowedConditions = array(
25
        self::MATCH_AND, self::MATCH_OR,
26
        self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID,
27
        self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID
28
    );
29
    protected $returns = 'Location';
30
31
    /**
32 1
     * @param array $conditions key: condition, value: int / string / int[] / string[]
33
     * @return LocationCollection
34 1
     */
35
    public function match(array $conditions)
36
    {
37
        return $this->matchLocation($conditions);
38
    }
39
40
    /**
41 1
     * @param array $conditions key: condition, value: value: int / string / int[] / string[]
42
     * @return LocationCollection
43 1
     */
44
    public function matchLocation(array $conditions)
45 1
    {
46
        $this->validateConditions($conditions);
47 1
48 1
        foreach ($conditions as $key => $values) {
49 1
50
            if (!is_array($values)) {
51
                $values = array($values);
52 1
            }
53
54
            switch ($key) {
55 1
                case self::MATCH_CONTENT_ID:
56 1
                   return new LocationCollection($this->findLocationsByContentIds($values));
57
58 1
                case self::MATCH_LOCATION_ID:
59
                    return new LocationCollection($this->findLocationsByLocationIds($values));
60
61 1
                case self::MATCH_CONTENT_REMOTE_ID:
62 1
                    return new LocationCollection($this->findLocationsByContentRemoteIds($values));
63
64
                case self::MATCH_LOCATION_REMOTE_ID:
65
                    return new LocationCollection($this->findLocationsByLocationRemoteIds($values));
66
67
                case self::MATCH_PARENT_LOCATION_ID:
68
                    return new LocationCollection($this->findLocationsByParentLocationIds($values));
69
70
                case self::MATCH_PARENT_LOCATION_REMOTE_ID:
71
                    return new LocationCollection($this->findLocationsByParentLocationRemoteIds($values));
72
73
                case self::MATCH_AND:
74
                    return $this->matchAnd($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchAnd($values); of type object|array adds the type array to the return on line 74 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...nMatcher::matchLocation of type Kaliop\eZMigrationBundle...LocationCollection|null.
Loading history...
75
76
                case self::MATCH_OR:
77
                    return $this->matchOr($values);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->matchOr($values); of type object|array adds the type array to the return on line 77 which is incompatible with the return type documented by Kaliop\eZMigrationBundle...nMatcher::matchLocation of type Kaliop\eZMigrationBundle...LocationCollection|null.
Loading history...
78
            }
79
        }
80
    }
81
82
    /**
83
     * When matching by key, we accept location Id and remote Id only
84
     * @param int|string $key
85
     * @return array
86
     */
87
    protected function getConditionsFromKey($key)
88
    {
89
        if (is_int($key) || ctype_digit($key)) {
90
            return array(self::MATCH_LOCATION_ID => $key);
91
        }
92
        return array(self::MATCH_LOCATION_REMOTE_ID => $key);
93
    }
94
95
    /**
96
     * Returns all locations of a set of objects
97
     *
98
     * @param int[] $contentIds
99
     * @return Location[]
100
     */
101 View Code Duplication
    protected function findLocationsByContentIds(array $contentIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $locations = [];
104
105
        foreach ($contentIds as $contentId) {
106
            $content = $this->repository->getContentService()->loadContent($contentId);
107
            $locations = array_merge($locations, $this->repository->getLocationService()->loadLocations($content->contentInfo));
108
        }
109
110
        return $locations;
111
    }
112
113 1
    /**
114
     * Returns all locations of a set of objects
115 1
     *
116
     * @param int[] $remoteContentIds
117 1
     * @return Location[]
118 1
     */
119 1 View Code Duplication
    protected function findLocationsByContentRemoteIds(array $remoteContentIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121 1
        $locations = [];
122
123
        foreach ($remoteContentIds as $remoteContentId) {
124
            $content = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId);
125
            $locations = array_merge($locations, $this->repository->getLocationService()->loadLocations($content->contentInfo));
126
        }
127
128 1
        return $locations;
129
    }
130 1
131
    /**
132 1
     * @param int[] $locationIds
133 1
     * @return Location[]
134 1
     */
135
    protected function findLocationsByLocationIds(array $locationIds)
136 1
    {
137
        $locations = [];
138
139
        foreach ($locationIds as $locationId) {
140
            $locations[] = $this->repository->getLocationService()->loadLocation($locationId);
141
        }
142
143
        return $locations;
144
    }
145
146
    /**
147
     * @param int[] $locationRemoteIds
148
     * @return Location[]
149
     */
150
    protected function findLocationsByLocationRemoteIds($locationRemoteIds)
151
    {
152
        $locations = [];
153
154
        foreach ($locationRemoteIds as $locationRemoteId) {
155
            $locations[] = $this->repository->getLocationService()->loadLocationByRemoteId($locationRemoteId);
156
        }
157
158
        return $locations;
159
    }
160
161
    /**
162
     * @param int[] $parentLocationIds
163
     * @return Location[]
164
     */
165
    protected function findLocationsByParentLocationIds($parentLocationIds)
166
    {
167
        $query = new LocationQuery();
168
        $query->limit = PHP_INT_MAX;
169
        $query->filter = new Query\Criterion\ParentLocationId($parentLocationIds);
170
171
        $results = $this->repository->getSearchService()->findLocations($query);
172
173
        $locations = [];
174
175
        foreach ($results->searchHits as $result) {
176
            $locations[] = $result->valueObject;
177
        }
178
179
        return $locations;
180
    }
181
182
    /**
183
     * @param int[] $parentLocationRemoteIds
184
     * @return Location[]
185
     */
186 View Code Duplication
    protected function findLocationsByParentLocationRemoteIds($parentLocationRemoteIds)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        $locationIds = [];
189
190
        foreach ($parentLocationRemoteIds as $parentLocationRemoteId) {
191
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($parentLocationRemoteId);
192
            $locationIds[] = $location->id;
193
        }
194
195
        return $this->findLocationsByParentLocationIds($locationIds);
196
    }
197
}
198