Completed
Push — master ( 2d5a04...ae10c9 )
by Gaetano
06:49
created

LocationManager::matchLocationByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 20
    protected $supportedActions = array('create', 'load', 'update', 'delete', 'trash');
19
20 20
    protected $contentMatcher;
21 20
    protected $locationMatcher;
22 20
    protected $sortConverter;
23
24
    public function __construct(ContentMatcher $contentMatcher, LocationMatcher $locationMatcher, SortConverter $sortConverter)
25
    {
26
        $this->contentMatcher = $contentMatcher;
27 1
        $this->locationMatcher = $locationMatcher;
28
        $this->sortConverter = $sortConverter;
29 1
    }
30
31 1
    /**
32
     * Method to handle the create operation of the migration instructions
33
     */
34 1
    protected function create($step)
35 1
    {
36 1
        $locationService = $this->repository->getLocationService();
37 1
38 1
        if (!isset($step->dsl['parent_location']) && !isset($step->dsl['parent_location_id'])) {
39 1
            throw new \Exception('Missing parent location id. This is required to create the new location.');
40 1
        }
41 1
42
        // support legacy tag: parent_location_id
43 1
        if (!isset($step->dsl['parent_location']) && isset($step->dsl['parent_location_id'])) {
44
            $parentLocationIds = $step->dsl['parent_location_id'];
45 1
        } else {
46 1
            $parentLocationIds = $step->dsl['parent_location'];
47 1
        }
48
49 1
        if (!is_array($parentLocationIds)) {
50 1
            $parentLocationIds = array($parentLocationIds);
51
        }
52 1
53
        if (isset($step->dsl['is_main']) && count($parentLocationIds) > 1) {
54 1
            throw new \Exception('Can not set more than one new location as main.');
55 1
        }
56 1
57
        // resolve references and remote ids
58 1
        foreach ($parentLocationIds as $id => $parentLocationId) {
59 1
            $parentLocationId = $this->referenceResolver->resolveReference($parentLocationId);
60
            $parentLocationIds[$id] = $this->matchLocationByKey($parentLocationId)->id;
61 1
        }
62 1
63 1
        $contentCollection = $this->matchContents('create', $step);
64
65 1
        $locations = null;
66 1
        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
            $contentInfo = $content->contentInfo;
68
69
            foreach ($parentLocationIds as $parentLocationId) {
70
                $locationCreateStruct = $locationService->newLocationCreateStruct($parentLocationId);
71
72
                if (isset($step->dsl['is_hidden'])) {
73
                    $locationCreateStruct->hidden = $step->dsl['is_hidden'];
74
                }
75 1
76
                if (isset($step->dsl['priority'])) {
77 1
                    $locationCreateStruct->priority = $step->dsl['priority'];
78
                }
79 1
80
                if (isset($step->dsl['sort_order'])) {
81 1
                    $locationCreateStruct->sortOrder = $this->getSortOrder($step->dsl['sort_order']);
82
                }
83
84
                if (isset($step->dsl['sort_field'])) {
85 1
                    $locationCreateStruct->sortField = $this->getSortField($step->dsl['sort_field']);
86
                }
87
88
                $location = $locationService->createLocation($contentInfo, $locationCreateStruct);
89 1
90
                if (isset($step->dsl['is_main'])) {
91
                    $this->setMainLocation($location);
92
                }
93
94
                $locations[] = $location;
95
            }
96
        }
97
98 1
        $locationCollection = new LocationCollection($locations);
0 ignored issues
show
Documentation introduced by
$locations is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
100
        $this->setReferences($locationCollection, $step);
101 1
102 1
        return $locationCollection;
103 1
    }
104 1
105 1
    protected function load($step)
106 1
    {
107
        $locationCollection = $this->matchLocations('load', $step);
108 1
109 1
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('load', $step) on line 107 can be null; however, Kaliop\eZMigrationBundle...anager::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...
110 1
111
        return $locationCollection;
112 1
    }
113
114
    /**
115
     * Updates information for a location like priority, sort field and sort order.
116 1
     * Updates the visibility of the location when needed.
117 1
     * Can move a location and its children to a new parent location or swap two locations.
118 1
     */
119
    protected function update($step)
120 1
    {
121
        $locationService = $this->repository->getLocationService();
122
123
        $locationCollection = $this->matchLocations('update', $step);
124 1
125 1
        if (count($locationCollection) > 1 && isset($step->dsl['references'])) {
126
            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");
127
        }
128 1
129 1
        if (count($locationCollection) > 1 && isset($step->dsl['swap_with_location'])) {
130 1
            throw new \Exception("Can not execute Location update because multiple locations match, and a swap_with_location is specified in the dsl.");
131 1
        }
132 1
133
        // support legacy tag: parent_location_id
134 1
        if (isset($step->dsl['swap_with_location']) && (isset($step->dsl['parent_location']) || isset($step->dsl['parent_location_id']))) {
135
            throw new \Exception('Cannot move location to a new parent and swap location with another location at the same time.');
136
        }
137 1
138
        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...
139 1
140 1
            if (isset($step->dsl['priority'])
141 1
                || isset($step->dsl['sort_field'])
142 1
                || isset($step->dsl['sort_order'])
143 1
                || isset($step->dsl['remote_id'])
144 1
            ) {
145 1
                $locationUpdateStruct = $locationService->newLocationUpdateStruct();
146
147
                if (isset($step->dsl['priority'])) {
148
                    $locationUpdateStruct->priority = $step->dsl['priority'];
149
                }
150
151
                if (isset($step->dsl['sort_field'])) {
152
                    $locationUpdateStruct->sortField = $this->getSortField($step->dsl['sort_field'], $location->sortField);
153
                }
154
155
                if (isset($step->dsl['sort_order'])) {
156 1
                    $locationUpdateStruct->sortOrder = $this->getSortOrder($step->dsl['sort_order'], $location->sortOrder);
157 1
                }
158 1
159
                if (isset($step->dsl['remote_id'])) {
160
                    $locationUpdateStruct->remoteId = $step->dsl['remote_id'];
161
                }
162
163
                $location = $locationService->updateLocation($location, $locationUpdateStruct);
164
            }
165
166
            // Check if visibility needs to be updated
167
            if (isset($step->dsl['is_hidden'])) {
168
                if ($step->dsl['is_hidden']) {
169
                    $location = $locationService->hideLocation($location);
170
                } else {
171
                    $location = $locationService->unhideLocation($location);
172
                }
173
            }
174
175
            // Move or swap location
176
            if (isset($step->dsl['parent_location']) || isset($step->dsl['parent_location_id'])) {
177
                // Move the location and all its children to a new parent
178
                $parentLocationId = isset($step->dsl['parent_location']) ? $step->dsl['parent_location'] : $step->dsl['parent_location_id'];
179
                $parentLocationId = $this->referenceResolver->resolveReference($parentLocationId);
180
181
                $newParentLocation = $locationService->loadLocation($parentLocationId);
182 1
183
                $locationService->moveSubtree($location, $newParentLocation);
184 1
            } elseif (isset($step->dsl['swap_with_location'])) {
185 1
                // Swap locations
186
                $swapLocationId = $step->dsl['swap_with_location'];
187
                $swapLocationId = $this->referenceResolver->resolveReference($swapLocationId);
188
189 1
                $locationToSwap = $this->matchLocationByKey($swapLocationId);
190 1
191 1
                $locationService->swapLocation($location, $locationToSwap);
192
            }
193 1
194
            // make the location the main one
195
            if (isset($step->dsl['is_main'])) {
196 1
                $this->setMainLocation($location);
197 1
            }
198
199
            $locationCollection[$key] = $location;
200
        }
201
202
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('update', $step) on line 123 can be null; however, Kaliop\eZMigrationBundle...anager::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...
203
204 1
        return $locationCollection;
205 1
    }
206 1
207 1
    /**
208 1
     * Delete locations
209
     */
210 1 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...
211
    {
212
        $locationCollection = $this->matchLocations('delete', $step);
213
214
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('delete', $step) on line 212 can be null; however, Kaliop\eZMigrationBundle...anager::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...
215
216
        $locationService = $this->repository->getLocationService();
217
218
        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...
219
            $locationService->deleteLocation($location);
220 1
        }
221
222 1
        return $locationCollection;
223
    }
224
225
    /**
226
     * Delete locations sending them to the trash
227 1
     */
228 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...
229
    {
230
        $locationCollection = $this->matchLocations('delete', $step);
231
232
        $this->setReferences($locationCollection, $step);
0 ignored issues
show
Bug introduced by
It seems like $locationCollection defined by $this->matchLocations('delete', $step) on line 230 can be null; however, Kaliop\eZMigrationBundle...anager::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...
233
234
        $trashService = $this->repository->getTrashService();
235 1
236
        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...
237
            $trashService->trash($location);
238 1
        }
239 1
240
        return $locationCollection;
241
    }
242
243
    /**
244
     * @param string $action
245
     * @return LocationCollection
246 1
     * @throws \Exception
247 1
     */
248 1 View Code Duplication
    protected function matchLocations($action, $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...
249
    {
250 1
        if (!isset($step->dsl['location_id']) && !isset($step->dsl['match'])) {
251
            throw new \Exception("The id or a match condition is required to $action a location");
252 1
        }
253
254
        // Backwards compat
255 1
        if (isset($step->dsl['match'])) {
256 1
            $match = $step->dsl['match'];
257 1
        } else {
258
            $match = array('location_id' => $step->dsl['location_id']);
259 1
        }
260
261
        // convert the references passed in the match
262
        $match = $this->resolveReferencesRecursively($match);
0 ignored issues
show
Deprecated Code introduced by
The method Kaliop\eZMigrationBundle...ReferencesRecursively() has been deprecated with message: will be moved into the reference resolver classes

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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