Completed
Pull Request — master (#135)
by Alex
07:43
created

LocationMatcher::matchLocation()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 35
Code Lines 23

Duplication

Lines 8
Ratio 22.86 %

Code Coverage

Tests 10
CRAP Score 12.2083

Importance

Changes 0
Metric Value
dl 8
loc 35
ccs 10
cts 19
cp 0.5263
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 23
nc 12
nop 1
crap 12.2083
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
use eZ\Publish\API\Repository\Values\Content\Location;
9
10
class LocationMatcher extends QueryBasedMatcher
11
{
12
    use FlexibleKeyMatcherTrait;
13
14
    const MATCH_DEPTH = 'depth';
15
    const MATCH_IS_MAIN_LOCATION = 'is_main_location';
16
    const MATCH_PRIORITY = 'priority';
17
18
    protected $allowedConditions = array(
19
        self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
20
        self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID,
21
        self::MATCH_ATTRIBUTE, self::MATCH_CONTENT_TYPE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER, self::MATCH_GROUP,
22
        self::MATCH_CREATION_DATE, self::MATCH_MODIFICATION_DATE, self::MATCH_OBJECT_STATE, self::MATCH_OWNER,
23
        self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_SECTION, self::MATCH_SUBTREE,
24
        self::MATCH_VISIBILITY,
25
        // aliases
26
        'content_type', 'content_type_id', 'content_type_identifier',
27
        // location-only
28
        self::MATCH_DEPTH, self::MATCH_IS_MAIN_LOCATION, self::MATCH_PRIORITY,
29
    );
30
    protected $returns = 'Location';
31
32 1
    /**
33
     * @param array $conditions key: condition, value: int / string / int[] / string[]
34 1
     * @return LocationCollection
35
     */
36
    public function match(array $conditions)
37
    {
38
        return $this->matchLocation($conditions);
39
    }
40
41 1
    /**
42
     * @param array $conditions key: condition, value: value: int / string / int[] / string[]
43 1
     * @return LocationCollection
44
     */
45 1
    public function matchLocation(array $conditions)
46
    {
47 1
        $this->validateConditions($conditions);
48 1
49 1
        if (count($conditions) === 1) {
50
            $key = array_keys($conditions)[0];
51
            switch ($key) {
52 1 View Code Duplication
                case self::MATCH_LOCATION_ID:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
                    $location = $this->repository->getLocationService()->loadLocation($conditions[$key]);
54
                    return new LocationCollection(array($location));
55 1
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
56 1
57 View Code Duplication
                case self::MATCH_LOCATION_REMOTE_ID:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58 1
                    $location = $this->repository->getLocationService()->loadLocationByRemoteId($conditions[$key]);
59
                    return new LocationCollection(array($location));
60
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61 1
            }
62 1
        }
63
64
        foreach ($conditions as $key => $values) {
65
66
            $query = new LocationQuery();
67
            $query->limit = self::INT_MAX_16BIT;
68
            if (isset($query->performCount)) $query->performCount = false;
69
            $query->filter = $this->getQueryCriterion($key, $values);
70
            $results = $this->repository->getSearchService()->findLocations($query);
71
72
            $locations = [];
73
            foreach ($results->searchHits as $result) {
74
                $locations[$result->valueObject->id] = $result->valueObject;
75
            }
76
77
            return new LocationCollection($locations);
78
        }
79
    }
80
81
    /**
82
     * When matching by key, we accept location Id and remote Id only
83
     * @param int|string $key
84
     * @return array
85
     */
86
    protected function getConditionsFromKey($key)
87
    {
88
        if (is_int($key) || ctype_digit($key)) {
89
            return array(self::MATCH_LOCATION_ID => $key);
90
        }
91
        return array(self::MATCH_LOCATION_REMOTE_ID => $key);
92
    }
93
94
    protected function getQueryCriterion($key, $values)
95
    {
96
        if (!is_array($values)) {
97
            $values = array($values);
98
        }
99
100
        switch ($key) {
101 View Code Duplication
            case self::MATCH_DEPTH:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
                $match = reset($values);
103
                $operator = key($values);
104
                if (!isset(self::$operatorsMap[$operator])) {
105
                    throw new \Exception("Can not use '$operator' as comparison operator for depth");
106
                }
107
                return new Query\Criterion\Location\Depth(self::$operatorsMap[$operator], $match);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\A...ap[$operator], $match); (eZ\Publish\API\Repositor...riterion\Location\Depth) is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...cher::getQueryCriterion of type eZ\Publish\API\Repositor...ry\Criterion\LogicalNot.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
108
109
            case self::MATCH_IS_MAIN_LOCATION:
110
            case 'is_main':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
111
                /// @todo error/warning if there is more than 1 value...
112
                $value = reset($values);
113 1
                if ($value) {
114
                    return new Query\Criterion\Location\IsMainLocation(Query\Criterion\Location\IsMainLocation::MAIN);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\A...\IsMainLocation::MAIN); (eZ\Publish\API\Repositor...Location\IsMainLocation) is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...cher::getQueryCriterion of type eZ\Publish\API\Repositor...ry\Criterion\LogicalNot.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
115 1
                } else {
116
                    return new Query\Criterion\Location\IsMainLocation(Query\Criterion\Location\IsMainLocation::NOT_MAIN);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\A...ainLocation::NOT_MAIN); (eZ\Publish\API\Repositor...Location\IsMainLocation) is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...cher::getQueryCriterion of type eZ\Publish\API\Repositor...ry\Criterion\LogicalNot.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
117 1
                }
118 1
119 1 View Code Duplication
            case self::MATCH_PRIORITY:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
                $match = reset($values);
121 1
                $operator = key($values);
122
                if (!isset(self::$operatorsMap[$operator])) {
123
                    throw new \Exception("Can not use '$operator' as comparison operator for depth");
124
                }
125
                return new Query\Criterion\Location\Priority(self::$operatorsMap[$operator], $match);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\A...ap[$operator], $match); (eZ\Publish\API\Repositor...erion\Location\Priority) is incompatible with the return type of the parent method Kaliop\eZMigrationBundle...cher::getQueryCriterion of type eZ\Publish\API\Repositor...ry\Criterion\LogicalNot.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
126
        }
127
128 1
        return parent::getQueryCriterion($key, $values);
129
    }
130 1
131
    /**
132 1
     * Returns all locations of a set of objects
133 1
     *
134 1
     * @param int[] $contentIds
135
     * @return Location[]
136 1
     * @deprecated
137
     */
138 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...
139
    {
140
        $locations = [];
141
142
        foreach ($contentIds as $contentId) {
143
            $content = $this->repository->getContentService()->loadContent($contentId);
144
            foreach($this->repository->getLocationService()->loadLocations($content->contentInfo) as $location) {
145
                $locations[$location->id] = $location;
146
            }
147
        }
148
149
        return $locations;
150
    }
151
152
    /**
153
     * Returns all locations of a set of objects
154
     *
155
     * @param int[] $remoteContentIds
156
     * @return Location[]
157
     * @deprecated
158
     */
159 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...
160
    {
161
        $locations = [];
162
163
        foreach ($remoteContentIds as $remoteContentId) {
164
            $content = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId);
165
            foreach($this->repository->getLocationService()->loadLocations($content->contentInfo) as $location) {
166
                $locations[$location->id] = $location;
167
            }
168
        }
169
170
        return $locations;
171
    }
172
173
    /**
174
     * @param int[] $locationIds
175
     * @return Location[]
176
     * @deprecated
177
     */
178
    protected function findLocationsByLocationIds(array $locationIds)
179
    {
180
        $locations = [];
181
182
        foreach ($locationIds as $locationId) {
183
            $locations[$locationId] = $this->repository->getLocationService()->loadLocation($locationId);
184
        }
185
186
        return $locations;
187
    }
188
189
    /**
190
     * @param int[] $locationRemoteIds
191
     * @return Location[]
192
     * @deprecated
193
     */
194
    protected function findLocationsByLocationRemoteIds($locationRemoteIds)
195
    {
196
        $locations = [];
197
198
        foreach ($locationRemoteIds as $locationRemoteId) {
199
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($locationRemoteId);
200
            $locations[$location->id] = $location;
201
        }
202
203
        return $locations;
204
    }
205
206
    /**
207
     * @param int[] $parentLocationIds
208
     * @return Location[]
209
     * @deprecated
210
     */
211 View Code Duplication
    protected function findLocationsByParentLocationIds($parentLocationIds)
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...
212
    {
213
        $query = new LocationQuery();
214
        $query->limit = self::INT_MAX_16BIT;
215
        if (isset($query->performCount)) $query->performCount = false;
216
        $query->filter = new Query\Criterion\ParentLocationId($parentLocationIds);
217
218
        $results = $this->repository->getSearchService()->findLocations($query);
219
220
        $locations = [];
221
222
        foreach ($results->searchHits as $result) {
223
            $locations[$result->valueObject->id] = $result->valueObject;
224
        }
225
226
        return $locations;
227
    }
228
229
    /**
230
     * @param int[] $parentLocationRemoteIds
231
     * @return Location[]
232
     * @deprecated
233
     */
234 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...
235
    {
236
        $locationIds = [];
237
238
        foreach ($parentLocationRemoteIds as $parentLocationRemoteId) {
239
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($parentLocationRemoteId);
240
            $locationIds[$location->id] = $location->id;
241
        }
242
243
        return $this->findLocationsByParentLocationIds($locationIds);
0 ignored issues
show
Deprecated Code introduced by
The method Kaliop\eZMigrationBundle...nsByParentLocationIds() has been deprecated.

This method has been deprecated.

Loading history...
244
    }
245
}
246