Completed
Push — master ( 956196...175692 )
by Gaetano
09:59
created

ContentMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
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
class ContentMatcher extends AbstractMatcher
10
{
11
    const MATCH_CONTENT_ID = 'content_id';
12
    const MATCH_LOCATION_ID = 'location_id';
13
    const MATCH_CONTENT_REMOTE_ID = 'content_remote_id';
14
    const MATCH_LOCATION_REMOTE_ID = 'location_remote_id';
15
    const MATCH_PARENT_LOCATION_ID = 'parent_location_id';
16
    const MATCH_PARENT_LOCATION_REMOTE_ID = 'parent_location_remote_id';
17
    const MATCH_CONTENT_TYPE_IDENTIFIER = 'content_type';
18
19
    protected $allowedConditions = array(
20
        self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID,
21
        self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID, self::MATCH_CONTENT_TYPE_IDENTIFIER
22
    );
23
    protected $returns = 'Content';
24
25
    /**
26
     * @param array $conditions key: condition, value: int / string / int[] / string[]
27
     * @return ContentCollection
28
     */
29
    public function matchContent(array $conditions)
30
    {
31
        $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...
32
33
        foreach ($conditions as $key => $values) {
0 ignored issues
show
Bug introduced by
The expression $conditions of type null is not traversable.
Loading history...
34
35
            if (!is_array($values)) {
36
                $values = array($values);
37
            }
38
39
            switch ($key) {
40
                case self::MATCH_CONTENT_ID:
41
                   return new ContentCollection($this->findContentsByContentIds($values));
42
43
                case self::MATCH_LOCATION_ID:
44
                    return new ContentCollection($this->findContentsByLocationIds($values));
45
46
                case self::MATCH_CONTENT_REMOTE_ID:
47
                    return new ContentCollection($this->findContentsByContentRemoteIds($values));
48
49
                case self::MATCH_LOCATION_REMOTE_ID:
50
                    return new ContentCollection($this->findContentsByLocationRemoteIds($values));
51
52
                case self::MATCH_PARENT_LOCATION_ID:
53
                    return new ContentCollection($this->findContentsByParentLocationIds($values));
54
55
                case self::MATCH_PARENT_LOCATION_REMOTE_ID:
56
                    return new ContentCollection($this->findContentsByParentLocationRemoteIds($values));
57
58
                case self::MATCH_CONTENT_TYPE_IDENTIFIER:
59
                    return new ContentCollection($this->findContentsByContentTypeIdentifiers($values));
60
            }
61
        }
62
    }
63
64
65
    /**
66
     * @param int[] $contentIds
67
     * @return Content[]
68
     */
69
    protected function findContentsByContentIds(array $contentIds)
70
    {
71
        $contents = [];
72
73
        foreach ($contentIds as $contentId) {
74
            $contents[] = $this->repository->getContentService()->loadContent($contentId);
75
        }
76
77
        return $contents;
78
    }
79
80
    /**
81
     * @param string[] $remoteContentIds
82
     * @return Content[]
83
     */
84
    protected function findContentsByContentRemoteIds(array $remoteContentIds)
85
    {
86
        $contents = [];
87
88
        foreach ($remoteContentIds as $remoteContentId) {
89
            $contents[] = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId);
90
        }
91
92
        return $contents;
93
    }
94
95
    /**
96
     * @param int[] $locationIds
97
     * @return Content[]
98
     */
99 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...
100
    {
101
        $contentIds = [];
102
103
        foreach ($locationIds as $locationId) {
104
            $location = $this->repository->getLocationService()->loadLocation($locationId);
105
            $contentIds[] = $location->contentId;
106
        }
107
108
        return $this->findContentsByContentIds($contentIds);
109
    }
110
111
    /**
112
     * @param string[] $remoteLocationIds
113
     * @return Content[]
114
     */
115 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...
116
    {
117
        $contentIds = [];
118
119
        foreach ($remoteLocationIds as $remoteLocationId) {
120
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteLocationId);
121
            $contentIds[] = $location->contentId;
122
        }
123
124
        return $this->findContentsByContentIds($contentIds);
125
    }
126
127
    /**
128
     * @param int[] $parentLocationIds
129
     * @return Content[]
130
     */
131 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...
132
    {
133
        $query = new Query();
134
        $query->limit = PHP_INT_MAX;
135
        $query->filter = new Query\Criterion\ParentLocationId($parentLocationIds);
136
        $results = $this->repository->getSearchService()->findContent($query);
137
138
        $contents = [];
139
        foreach ($results->searchHits as $result) {
140
            $contents[] = $result->valueObject;
141
        }
142
143
        return $contents;
144
    }
145
146
    /**
147
     * @param string[] $remoteParentLocationIds
148
     * @return Content[]
149
     */
150 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...
151
    {
152
        $locationIds = [];
153
154
        foreach ($remoteParentLocationIds as $remoteParentLocationId) {
155
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($remoteParentLocationId);
156
            $locationIds[] = $location->id;
157
        }
158
159
        return $this->findContentsByParentLocationIds($locationIds);
160
    }
161
162
    /**
163
     * @param string[] $contentTypeIdentifiers
164
     * @return Content[]
165
     */
166 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...
167
    {
168
        $query = new Query();
169
        $query->limit = PHP_INT_MAX;
170
        $query->filter = new Query\Criterion\ContentTypeIdentifier($contentTypeIdentifiers);
171
        $results = $this->repository->getSearchService()->findContent($query);
172
173
        $contents = [];
174
        foreach ($results->searchHits as $result) {
175
            $contents[] = $result->valueObject;
176
        }
177
178
        return $contents;
179
    }
180
}
181