Completed
Push — master ( b2c38b...f910d1 )
by Gaetano
07:22
created

LocationManager::matchContents()   C

Complexity

Conditions 13
Paths 97

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 20.4274

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
ccs 11
cts 17
cp 0.6471
rs 6.6166
cc 13
nc 97
nop 2
crap 20.4274

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use eZ\Publish\API\Repository\Values\Content\Location;
6
use Kaliop\eZMigrationBundle\API\Collection\ContentCollection;
7
use Kaliop\eZMigrationBundle\API\Collection\LocationCollection;
8
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
9
use Kaliop\eZMigrationBundle\Core\Matcher\LocationMatcher;
10
use Kaliop\eZMigrationBundle\Core\Helper\SortConverter;
11
12
/**
13
 * Handles location migrations.
14
 */
15
class LocationManager extends RepositoryExecutor
16
{
17
    protected $supportedStepTypes = array('location');
18
    protected $supportedActions = array('create', 'load', 'update', 'delete', 'trash');
19
20
    protected $contentMatcher;
21
    protected $locationMatcher;
22
    protected $sortConverter;
23
24 80
    public function __construct(ContentMatcher $contentMatcher, LocationMatcher $locationMatcher, SortConverter $sortConverter)
25
    {
26 80
        $this->contentMatcher = $contentMatcher;
27 80
        $this->locationMatcher = $locationMatcher;
28 80
        $this->sortConverter = $sortConverter;
29 80
    }
30
31
    /**
32
     * Method to handle the create operation of the migration instructions
33
     */
34 2
    protected function create($step)
35
    {
36 2
        $locationService = $this->repository->getLocationService();
37
38 2
        if (!isset($step->dsl['parent_location']) && !isset($step->dsl['parent_location_id'])) {
39
            throw new \Exception('Missing parent location id. This is required to create the new location.');
40
        }
41
42
        // support legacy tag: parent_location_id
43 2
        if (!isset($step->dsl['parent_location']) && isset($step->dsl['parent_location_id'])) {
44 1
            $parentLocationIds = $step->dsl['parent_location_id'];
45
        } else {
46 2
            $parentLocationIds = $step->dsl['parent_location'];
47
        }
48
49 2
        if (!is_array($parentLocationIds)) {
50 2
            $parentLocationIds = array($parentLocationIds);
51
        }
52
53 2
        if (isset($step->dsl['is_main']) && count($parentLocationIds) > 1) {
54
            throw new \Exception('Can not set more than one new location as main.');
55
        }
56
57
        // resolve references and remote ids
58 2
        foreach ($parentLocationIds as $id => $parentLocationId) {
59 2
            $parentLocationId = $this->referenceResolver->resolveReference($parentLocationId);
60 2
            $parentLocationIds[$id] = $this->matchLocationByKey($parentLocationId)->id;
61
        }
62
63 2
        $contentCollection = $this->matchContents('create', $step);
64
65 2
        $locations = array();
66 2
        foreach ($contentCollection as $content) {
0 ignored issues
show
Bug introduced by
The expression $contentCollection of type object<Kaliop\eZMigratio...ContentCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
67 2
            $contentInfo = $content->contentInfo;
68
69 2
            foreach ($parentLocationIds as $parentLocationId) {
70 2
                $locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId);
71
72 2
                if (isset($step->dsl['is_hidden'])) {
73 1
                    $locationCreateStruct->hidden = $this->referenceResolver->resolveReference($step->dsl['is_hidden']);
74
                }
75
76 2 View Code Duplication
                if (isset($step->dsl['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...
77 1
                    $locationCreateStruct->priority = $this->referenceResolver->resolveReference($step->dsl['priority']);
78
                }
79
80 2 View Code Duplication
                if (isset($step->dsl['sort_order'])) {
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...
81 1
                    $locationCreateStruct->sortOrder = $this->getSortOrder($this->referenceResolver->resolveReference($step->dsl['sort_order']));
82
                }
83
84 2 View Code Duplication
                if (isset($step->dsl['sort_field'])) {
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...
85 1
                    $locationCreateStruct->sortField = $this->getSortField($this->referenceResolver->resolveReference($step->dsl['sort_field']));
86
                }
87
88 2 View Code Duplication
                if (isset($step->dsl['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...
89
                    $locationCreateStruct->remoteId = $this->referenceResolver->resolveReference($step->dsl['remote_id']);
90 2
                }
91 1
92
                $location = $locationService->createLocation($contentInfo, $locationCreateStruct);
93 1
94
                if (isset($step->dsl['is_main'])) {
95
                    $this->setMainLocation($location);
96 2
                    // we have to reload the location so that correct data can be set as reference
97
                    $location = $locationService->loadLocation($location->id);
98
                }
99
100 2
                $locations[] = $location;
101
            }
102 2
        }
103
104 2
        $locationCollection = new LocationCollection($locations);
105
106
        $this->setReferences($locationCollection, $step);
107 2
108
        return $locationCollection;
109 2
    }
110
111 2
    protected function load($step)
112
    {
113 2
        $locationCollection = $this->matchLocations('load', $step);
114
115
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('load', $step) on line 113 can be null; however, Kaliop\eZMigrationBundle...ecutor::setReferences() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
116
117
        return $locationCollection;
118
    }
119
120
    /**
121 1
     * Updates information for a location like priority, sort field and sort order.
122
     * Updates the visibility of the location when needed.
123 1
     * Can move a location and its children to a new parent location or swap two locations.
124
     */
125 1
    protected function update($step)
126
    {
127 1
        $locationService = $this->repository->getLocationService();
128
129
        $locationCollection = $this->matchLocations('update', $step);
130
131 1
        if (count($locationCollection) > 1 && isset($step->dsl['references'])) {
132
            throw new \Exception("Can not execute Location update because multiple locations match, and a references section is specified in the dsl. References can be set when only 1 location matches");
133
        }
134
135
        if (count($locationCollection) > 1 && isset($step->dsl['swap_with_location'])) {
136 1
            throw new \Exception("Can not execute Location update because multiple locations match, and a swap_with_location is specified in the dsl.");
137
        }
138
139
        // support legacy tag: parent_location_id
140 1
        if (isset($step->dsl['swap_with_location']) && (isset($step->dsl['parent_location']) || isset($step->dsl['parent_location_id']))) {
141
            throw new \Exception('Cannot move location to a new parent and swap location with another location at the same time.');
142 1
        }
143 1
144 1
        foreach ($locationCollection as $key => $location) {
0 ignored issues
show
Bug introduced by
The expression $locationCollection of type object<Kaliop\eZMigratio...ocationCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
145 1
146
            if (isset($step->dsl['priority'])
147 1
                || isset($step->dsl['sort_field'])
148
                || isset($step->dsl['sort_order'])
149 1
                || isset($step->dsl['remote_id'])
150 1
            ) {
151
                $locationUpdateStruct = $locationService->newLocationUpdateStruct();
152
153 1 View Code Duplication
                if (isset($step->dsl['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...
154
                    $locationUpdateStruct->priority = $this->referenceResolver->resolveReference($step->dsl['priority']);
155
                }
156
157 1 View Code Duplication
                if (isset($step->dsl['sort_field'])) {
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...
158 1
                    $locationUpdateStruct->sortField = $this->getSortField($this->referenceResolver->resolveReference($step->dsl['sort_field']), $location->sortField);
159
                }
160
161 1 View Code Duplication
                if (isset($step->dsl['sort_order'])) {
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...
162
                    $locationUpdateStruct->sortOrder = $this->getSortOrder($this->referenceResolver->resolveReference($step->dsl['sort_order']), $location->sortOrder);
163
                }
164
165 1 View Code Duplication
                if (isset($step->dsl['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...
166
                    $locationUpdateStruct->remoteId = $this->referenceResolver->resolveReference($step->dsl['remote_id']);
167
                }
168
169 1
                $location = $locationService->updateLocation($location, $locationUpdateStruct);
170 1
            }
171 1
172
            // Check if visibility needs to be updated
173 1
            if (isset($step->dsl['is_hidden'])) {
174
                if ($step->dsl['is_hidden']) {
175
                    $location = $locationService->hideLocation($location);
176
                } else {
177
                    $location = $locationService->unhideLocation($location);
178 1
                }
179
            }
180 1
181 1
            // Move or swap location
182
            if (isset($step->dsl['parent_location']) || isset($step->dsl['parent_location_id'])) {
183 1
                // Move the location and all its children to a new parent
184
                $parentLocationId = isset($step->dsl['parent_location']) ? $step->dsl['parent_location'] : $step->dsl['parent_location_id'];
185 1
                $parentLocationId = $this->referenceResolver->resolveReference($parentLocationId);
186
187
                $newParentLocation = $locationService->loadLocation($parentLocationId);
188 1
189 1
                $locationService->moveSubtree($location, $newParentLocation);
190
191
                // we have to reload the location to be able to set references to the modified data
192
                $location = $locationService->loadLocation($location->id);
193
            } elseif (isset($step->dsl['swap_with_location'])) {
194
                // Swap locations
195
                $swapLocationId = $step->dsl['swap_with_location'];
196
                $swapLocationId = $this->referenceResolver->resolveReference($swapLocationId);
197
198
                $locationToSwap = $this->matchLocationByKey($swapLocationId);
199
200
                $locationService->swapLocation($location, $locationToSwap);
201
202
                // we have to reload the location to be able to set references to the modified data
203 1
                $location = $locationService->loadLocation($location->id);
204 1
            }
205
206
            // make the location the main one
207 1
            if (isset($step->dsl['is_main'])) {
208
                $this->setMainLocation($location);
209
210 1
                //have to reload the location so that correct data can be set as reference
211
                $location = $locationService->loadLocation($location->id);
212
            }
213 1
214
            $locationCollection[$key] = $location;
215 1
        }
216
217
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('update', $step) on line 129 can be null; however, Kaliop\eZMigrationBundle...ecutor::setReferences() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
218
219
        return $locationCollection;
220
    }
221
222
    /**
223
     * Delete locations
224
     */
225 View Code Duplication
    protected function delete($step)
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...
226
    {
227
        $locationCollection = $this->matchLocations('delete', $step);
228
229
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('delete', $step) on line 227 can be null; however, Kaliop\eZMigrationBundle...ecutor::setReferences() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
230
231
        $locationService = $this->repository->getLocationService();
232
233
        foreach ($locationCollection as $location) {
0 ignored issues
show
Bug introduced by
The expression $locationCollection of type object<Kaliop\eZMigratio...ocationCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
234
            $locationService->deleteLocation($location);
235
        }
236
237
        return $locationCollection;
238
    }
239 1
240
    /**
241 1
     * Delete locations sending them to the trash
242
     */
243 1 View Code Duplication
    protected function trash($step)
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...
244
    {
245 1
        $locationCollection = $this->matchLocations('delete', $step);
246
247 1
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('delete', $step) on line 245 can be null; however, Kaliop\eZMigrationBundle...ecutor::setReferences() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
248 1
249
        $trashService = $this->repository->getTrashService();
250
251 1
        foreach ($locationCollection as $location) {
0 ignored issues
show
Bug introduced by
The expression $locationCollection of type object<Kaliop\eZMigratio...ocationCollection>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
252
            $trashService->trash($location);
253
        }
254
255
        return $locationCollection;
256
    }
257
258
    /**
259 2
     * @param string $action
260
     * @return LocationCollection
261 2
     * @throws \Exception
262
     */
263
    protected function matchLocations($action, $step)
264
    {
265
        if (!isset($step->dsl['location_id']) && !isset($step->dsl['match'])) {
266 2
            throw new \Exception("The id or a match condition is required to $action a location");
267 2
        }
268
269 1
        // Backwards compat
270
        if (isset($step->dsl['match'])) {
271
            $match = $step->dsl['match'];
272
        } else {
273 2
            $match = array('location_id' => $step->dsl['location_id']);
274
        }
275 2
276
        // convert the references passed in the match
277
        $match = $this->resolveReferencesRecursively($match);
278
279
        $offset = isset($step->dsl['match_offset']) ? $this->referenceResolver->resolveReference($step->dsl['match_offset']) : 0;
280
        $limit = isset($step->dsl['match_limit']) ? $this->referenceResolver->resolveReference($step->dsl['match_limit']) : 0;
281
        $sort = isset($step->dsl['match_sort']) ? $this->referenceResolver->resolveReference($step->dsl['match_sort']) : array();
282
283
        return $this->locationMatcher->match($match, $sort, $offset, $limit);
284 2
    }
285
286 2
    /**
287
     * @param Location $location
288 2
     * @param array $references the definitions of the references to set
289 2
     * @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute
290 2
     * @return array key: the reference names, values: the reference values
291 1
     */
292 2 View Code Duplication
    protected function getReferencesValues($location, array $references, $step)
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...
293 2
    {
294 1
        $refs = array();
295 1
296 1
        foreach ($references as $reference) {
297 1
            switch ($reference['attribute']) {
298 1
                case 'location_id':
299 1
                case 'id':
300 1
                    $value = $location->id;
301 1
                    break;
302 1
                case 'remote_id':
303 1
                case 'location_remote_id':
304 1
                    $value = $location->remoteId;
305 1
                    break;
306 1
                case 'always_available':
307 1
                    $value = $location->contentInfo->alwaysAvailable;
308 1
                    break;
309 1
                case 'content_id':
310 1
                    $value = $location->contentId;
311 1
                    break;
312 1
                case 'content_type_id':
313 1
                    $value = $location->contentInfo->contentTypeId;
314 1
                    break;
315 1
                case 'content_type_identifier':
316 1
                    $contentTypeService = $this->repository->getContentTypeService();
317 1
                    $value = $contentTypeService->loadContentType($location->contentInfo->contentTypeId)->identifier;
318 1
                    break;
319 1
                case 'current_version':
320 1
                case 'current_version_no':
321 1
                    $value = $location->contentInfo->currentVersionNo;
322 1
                    break;
323 1
                case 'depth':
324 1
                    $value = $location->depth;
325 1
                    break;
326 1
                case 'is_hidden':
327 1
                    $value = $location->hidden;
328 1
                    break;
329 1
                case 'main_location_id':
330 1
                    $value = $location->contentInfo->mainLocationId;
331 1
                    break;
332 1
                case 'main_language_code':
333 1
                    $value = $location->contentInfo->mainLanguageCode;
334 1
                    break;
335 1
                case 'modification_date':
336 1
                    $value = $location->contentInfo->modificationDate->getTimestamp();
337 1
                    break;
338 1
                case 'name':
339 1
                    $value = $location->contentInfo->name;
340 1
                    break;
341 1
                case 'owner_id':
342 1
                    $value = $location->contentInfo->ownerId;
343 1
                    break;
344 1
                case 'parent_location_id':
345 1
                    $value = $location->parentLocationId;
346 1
                    break;
347 1
                case 'path':
348 1
                    $value = $location->pathString;
349 1
                    break;
350 1
                case 'priority':
351 1
                    $value = $location->priority;
352 1
                    break;
353 1
                case 'publication_date':
354 1
                    $value = $location->contentInfo->publishedDate->getTimestamp();
355 1
                    break;
356 1
                case 'section_id':
357 1
                    $value = $location->contentInfo->sectionId;
358 1
                    break;
359 1
                case 'section_identifier':
360 1
                    $sectionService = $this->repository->getSectionService();
361
                    $value = $sectionService->loadSection($location->contentInfo->sectionId)->identifier;
362
                    break;
363
                case 'sort_field':
364
                    $value = $this->sortConverter->sortField2Hash($location->sortField);
365 2
                    break;
366
                case 'sort_order':
367
                    $value = $this->sortConverter->sortOrder2Hash($location->sortOrder);
368 2
                    break;
369
                default:
370
                    throw new \InvalidArgumentException('Location Manager does not support setting references for attribute ' . $reference['attribute']);
371
            }
372
373
            $refs[$reference['identifier']] = $value;
374
        }
375 12
376
        return $refs;
377 12
    }
378
379
    /**
380
     * @param int|string|array $locationKey
381
     * @return Location
382
     */
383
    public function matchLocationByKey($locationKey)
384
    {
385
        return $this->locationMatcher->matchOneByKey($locationKey);
386
    }
387 2
388
    /**
389 2
     * NB: weirdly enough, it returns contents, not locations
390
     *
391
     * @param string $action
392
     * @return ContentCollection
393
     * @throws \Exception
394 2
     */
395
    protected function matchContents($action, $step)
396
    {
397
        if (!isset($step->dsl['object_id']) && !isset($step->dsl['remote_id']) && !isset($step->dsl['match'])) {
398
            throw new \Exception("The ID or remote ID of an object or a Match Condition is required to $action a new location.");
399
        }
400
401
        // Backwards compat
402 2
        if (!isset($step->dsl['match'])) {
403
            if (isset($step->dsl['object_id'])) {
404
                $step->dsl['match'] = array('content_id' => $step->dsl['object_id']);
405 2
            } elseif (isset($step->dsl['remote_id'])) {
406 2
                $step->dsl['match'] = array('content_remote_id' => $step->dsl['remote_id']);
407
            }
408
        }
409
410
        $match = $step->dsl['match'];
411 2
412
        // convert the references passed in the match
413
        foreach ($match as $condition => $values) {
414
            if (is_array($values)) {
415 2
                foreach ($values as $position => $value) {
416
                    $match[$condition][$position] = $this->referenceResolver->resolveReference($value);
417
                }
418 1
            } else {
419
                $match[$condition] = $this->referenceResolver->resolveReference($values);
420 1
            }
421 1
        }
422 1
423 1
        $offset = isset($step->dsl['match_offset']) ? $this->referenceResolver->resolveReference($step->dsl['match_offset']) : 0;
424 1
        $limit = isset($step->dsl['match_limit']) ? $this->referenceResolver->resolveReference($step->dsl['match_limit']) : 0;
425
        $sort = isset($step->dsl['match_sort']) ? $this->referenceResolver->resolveReference($step->dsl['match_sort']) : array();
426
427
        return $this->contentMatcher->matchContent($match, $sort, $offset, $limit);
428
    }
429
430
    protected function setMainLocation(Location $location)
431
    {
432
        $contentService = $this->repository->getContentService();
433 1
        $contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct();
434
        $contentMetaDataUpdateStruct->mainLocationId = $location->id;
435 1
        $contentService->updateContentMetadata($location->contentInfo, $contentMetaDataUpdateStruct);
436
    }
437 1
438 1
    /**
439
     * @param $newValue
440
     * @param null $currentValue
441 1
     * @return int|null
442
     *
443
     * * @todo make protected
444
     */
445
    public function getSortField($newValue, $currentValue = null)
446
    {
447
        $sortField = $currentValue;
448
449
        if ($newValue !== null) {
450
            $sortField = $this->sortConverter->hash2SortField($newValue);
451
        }
452
453
        return $sortField;
454
    }
455 1
456
    /**
457 1
     * Get the sort order based on the current value and the value in the DSL definition.
458
     *
459 1
     * @see \eZ\Publish\API\Repository\Values\Content\Location::SORT_ORDER_*
460 1
     *
461
     * @param int $newValue
462
     * @param int $currentValue
463 1
     * @return int|null
464
     *
465
     * @todo make protected
466
     */
467
    public function getSortOrder($newValue, $currentValue = null)
468
    {
469
        $sortOrder = $currentValue;
470
471
        if ($newValue !== null) {
472
            $sortOrder = $this->sortConverter->hash2SortOrder($newValue);
473
        }
474
475
        return $sortOrder;
476
    }
477
}
478