Completed
Push — master ( 2c6719...58280f )
by Gaetano
11:02
created

ContentMatcher::match()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\Content\Content;
6
use eZ\Publish\API\Repository\Values\Content\Query;
7
use Kaliop\eZMigrationBundle\API\Collection\ContentCollection;
8
9
/**
10
 * @todo extend to allow matching by object state, status, section
11
 * @todo extend to allow matching on multiple conditions (AND)
12
 */
13
class ContentMatcher extends AbstractMatcher
14
{
15
    const MATCH_CONTENT_ID = 'content_id';
16
    const MATCH_LOCATION_ID = 'location_id';
17
    const MATCH_CONTENT_REMOTE_ID = 'content_remote_id';
18
    const MATCH_LOCATION_REMOTE_ID = 'location_remote_id';
19
    const MATCH_PARENT_LOCATION_ID = 'parent_location_id';
20
    const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id';
21
    const MATCH_CONTENT_TYPE_IDENTIFIER = 'content_type';
22
23
    protected $allowedConditions = array(
24
        self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID,
25
        self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER
26
    );
27
    protected $returns = 'Content';
28
29
    /**
30
     * @param array $conditions key: condition, value: int / string / int[] / string[]
31
     * @return ContentCollection
32
     */
33
    public function match(array $conditions)
34
    {
35
        return $this->matchContent($conditions);
36
    }
37
38
    /**
39
     * @param array $conditions key: condition, value: int / string / int[] / string[]
40
     * @return ContentCollection
41
     */
42
    public function matchContent(array $conditions)
43
    {
44
        $conditions = $this->validateConditions($conditions);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $conditions is correct as $this->validateConditions($conditions) (which targets Kaliop\eZMigrationBundle...r::validateConditions()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
46
        foreach ($conditions as $key => $values) {
0 ignored issues
show
Bug introduced by
The expression $conditions of type null is not traversable.
Loading history...
47
48
            if (!is_array($values)) {
49
                $values = array($values);
50
            }
51
52
            switch ($key) {
53
                case self::MATCH_CONTENT_ID:
54
                   return new ContentCollection($this->findContentsByContentIds($values));
55
56
                case self::MATCH_LOCATION_ID:
57
                    return new ContentCollection($this->findContentsByLocationIds($values));
58
59
                case self::MATCH_CONTENT_REMOTE_ID:
60
                    return new ContentCollection($this->findContentsByContentRemoteIds($values));
61
62
                case self::MATCH_LOCATION_REMOTE_ID:
63
                    return new ContentCollection($this->findContentsByLocationRemoteIds($values));
64
65
                case self::MATCH_PARENT_LOCATION_ID:
66
                    return new ContentCollection($this->findContentsByParentLocationIds($values));
67
68
                case self::MATCH_PARENT_LOCATION_REMOTE_ID:
69
                    return new ContentCollection($this->findContentsByParentLocationRemoteIds($values));
70
71
                case self::MATCH_CONTENT_TYPE_IDENTIFIER:
72
                    return new ContentCollection($this->findContentsByContentTypeIdentifiers($values));
73
            }
74
        }
75
    }
76
77
78
    /**
79
     * @param int[] $contentIds
80
     * @return Content[]
81
     */
82
    protected function findContentsByContentIds(array $contentIds)
83
    {
84
        $contents = [];
85
86
        foreach ($contentIds as $contentId) {
87
            $contents[] = $this->repository->getContentService()->loadContent($contentId);
88
        }
89
90
        return $contents;
91
    }
92
93
    /**
94
     * @param string[] $remoteContentIds
95
     * @return Content[]
96
     */
97
    protected function findContentsByContentRemoteIds(array $remoteContentIds)
98
    {
99
        $contents = [];
100
101
        foreach ($remoteContentIds as $remoteContentId) {
102
            $contents[] = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId);
103
        }
104
105
        return $contents;
106
    }
107
108
    /**
109
     * @param int[] $locationIds
110
     * @return Content[]
111
     */
112 View Code Duplication
    protected function findContentsByLocationIds(array $locationIds)
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...
113
    {
114
        $contentIds = [];
115
116
        foreach ($locationIds as $locationId) {
117
            $location = $this->repository->getLocationService()->loadLocation($locationId);
118
            $contentIds[] = $location->contentId;
119
        }
120
121
        return $this->findContentsByContentIds($contentIds);
122
    }
123
124
    /**
125
     * @param string[] $remoteLocationIds
126
     * @return Content[]
127
     */
128 View Code Duplication
    protected function findContentsByLocationRemoteIds($remoteLocationIds)
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...
129
    {
130
        $contentIds = [];
131
132
        foreach ($remoteLocationIds as $remoteLocationId) {
133
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteLocationId);
134
            $contentIds[] = $location->contentId;
135
        }
136
137
        return $this->findContentsByContentIds($contentIds);
138
    }
139
140
    /**
141
     * @param int[] $parentLocationIds
142
     * @return Content[]
143
     */
144 View Code Duplication
    protected function findContentsByParentLocationIds($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...
145
    {
146
        $query = new Query();
147
        $query->limit = PHP_INT_MAX;
148
        $query->filter = new Query\Criterion\ParentLocationId($parentLocationIds);
149
        $results = $this->repository->getSearchService()->findContent($query);
150
151
        $contents = [];
152
        foreach ($results->searchHits as $result) {
153
            $contents[] = $result->valueObject;
154
        }
155
156
        return $contents;
157
    }
158
159
    /**
160
     * @param string[] $remoteParentLocationIds
161
     * @return Content[]
162
     */
163 View Code Duplication
    protected function findContentsByParentLocationRemoteIds($remoteParentLocationIds)
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...
164
    {
165
        $locationIds = [];
166
167
        foreach ($remoteParentLocationIds as $remoteParentLocationId) {
168
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteParentLocationId);
169
            $locationIds[] = $location->id;
170
        }
171
172
        return $this->findContentsByParentLocationIds($locationIds);
173
    }
174
175
    /**
176
     * @param string[] $contentTypeIdentifiers
177
     * @return Content[]
178
     */
179 View Code Duplication
    protected function findContentsByContentTypeIdentifiers(array $contentTypeIdentifiers)
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...
180
    {
181
        $query = new Query();
182
        $query->limit = PHP_INT_MAX;
183
        $query->filter = new Query\Criterion\ContentTypeIdentifier($contentTypeIdentifiers);
184
        $results = $this->repository->getSearchService()->findContent($query);
185
186
        $contents = [];
187
        foreach ($results->searchHits as $result) {
188
            $contents[] = $result->valueObject;
189
        }
190
191
        return $contents;
192
    }
193
}
194