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

LocationMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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\Query;
6
use Kaliop\eZMigrationBundle\API\Collection\LocationCollection;
7
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
8
9
class LocationMatcher 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
18
    protected $allowedConditions = array(
19
        self::MATCH_CONTENT_ID, self::MATCH_LOCATION_ID, self::MATCH_CONTENT_REMOTE_ID, self::MATCH_LOCATION_REMOTE_ID,
20
        self::MATCH_PARENT_LOCATION_ID, self::MATCH_PARENT_LOCATION_REMOTE_ID
21
    );
22
    protected $returns = 'Location';
23
24
    /**
25
     * @param array $conditions key: condition, value: value: int / string / int[] / string[]
26
     * @return LocationCollection
27
     */
28
    public function matchLocation(array $conditions)
29
    {
30
        foreach ($conditions as $key => $values) {
31
32
            if (!is_array($values)) {
33
                $values = array($values);
34
            }
35
36
            switch ($key) {
37
                case self::MATCH_CONTENT_ID:
38
                   return new LocationCollection($this->findLocationsByContentIds($values));
39
40
                case self::MATCH_LOCATION_ID:
41
                    return new LocationCollection($this->findLocationsByLocationIds($values));
42
43
                case self::MATCH_CONTENT_REMOTE_ID:
44
                    return new LocationCollection($this->findLocationsByContentRemoteIds($values));
45
46
                case self::MATCH_LOCATION_REMOTE_ID:
47
                    return new LocationCollection($this->findLocationsByLocationRemoteIds($values));
48
49
                case self::MATCH_PARENT_LOCATION_ID:
50
                    return new LocationCollection($this->findLocationsByParentLocationIds($values));
51
52
                case self::MATCH_PARENT_LOCATION_REMOTE_ID:
53
                    return new LocationCollection($this->findLocationsByParentLocationRemoteIds($values));
54
            }
55
        }
56
    }
57
58
    /**
59
     * Returns all locations of a set of objects
60
     *
61
     * @param int[] $contentIds
62
     * @return Location[]
63
     */
64 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...
65
    {
66
        $locations = [];
67
68
        foreach ($contentIds as $contentId) {
69
            $content = $this->repository->getContentService()->loadContent($contentId);
70
            $locations = array_merge($locations, $this->repository->getLocationService()->loadLocations($content->contentInfo));
71
        }
72
73
        return $locations;
74
    }
75
76
    /**
77
     * Returns all locations of a set of objects
78
     *
79
     * @param int[] $remoteContentIds
80
     * @return Location[]
81
     */
82 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...
83
    {
84
        $locations = [];
85
86
        foreach ($remoteContentIds as $remoteContentId) {
87
            $content = $this->repository->getContentService()->loadContentByRemoteId($remoteContentId);
88
            $locations = array_merge($locations, $this->repository->getLocationService()->loadLocations($content->contentInfo));
89
        }
90
91
        return $locations;
92
    }
93
94
    /**
95
     * @param int[] $locationIds
96
     * @return Location[]
97
     */
98
    protected function findLocationsByLocationIds(array $locationIds)
99
    {
100
        $locations = [];
101
102
        foreach ($locationIds as $locationId) {
103
            $locations[] = $this->repository->getLocationService()->loadLocation($locationId);
104
        }
105
106
        return $locations;
107
    }
108
109
    /**
110
     * @param int[] $locationRemoteIds
111
     * @return Location[]
112
     */
113
    protected function findLocationsByLocationRemoteIds($locationRemoteIds)
114
    {
115
        $locations = [];
116
117
        foreach ($locationRemoteIds as $locationRemoteId) {
118
            $locations[] = $this->repository->getLocationService()->loadLocationByRemoteId($locationRemoteId);
119
        }
120
121
        return $locations;
122
    }
123
124
    /**
125
     * @param int[] $parentLocationIds
126
     * @return Location[]
127
     */
128 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...
129
    {
130
        $query = new LocationQuery();
131
        $query->limit = PHP_INT_MAX;
132
        $query->filter = new Query\Criterion\ParentLocationId($parentLocationIds);
133
134
        $results = $this->repository->getSearchService()->findLocations($query);
135
136
        $locations = [];
137
138
        foreach ($results->searchHits as $result) {
139
            $locations[] = $result->valueObject;
140
        }
141
142
        return $locations;
143
    }
144
145
    /**
146
     * @param int[] $parentLocationRemoteIds
147
     * @return Location[]
148
     */
149 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...
150
    {
151
        $locationIds = [];
152
153
        foreach ($parentLocationRemoteIds as $parentLocationRemoteId) {
154
            $location = $this->repository->getLocationService()->loadLocationByRemoteId($parentLocationRemoteId);
155
            $locationIds[] = $location->id;
156
        }
157
158
        return $this->findLocationsByParentLocationIds($locationIds);
159
    }
160
}
161