Completed
Push — master ( 09cee1...75eab4 )
by Gaetano
06:21
created

TrashManager::recover()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 3
Ratio 16.67 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 3
loc 18
ccs 0
cts 14
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 1
crap 20
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Collection\LocationCollection;
6
use Kaliop\eZMigrationBundle\API\Collection\TrashedItemCollection;
7
use Kaliop\eZMigrationBundle\Core\Matcher\TrashMatcher;
8
use Kaliop\eZMigrationBundle\Core\Helper\SortConverter;
9
10
/**
11
 * Handles trash migrations.
12
 */
13
class TrashManager extends RepositoryExecutor
14
{
15
    protected $supportedActions = array('purge', 'recover', 'delete');
16
    protected $supportedStepTypes = array('trash');
17
18
    /** @var TrashMatcher $trashMatcher */
19
    protected $trashMatcher;
20
21
    protected $sortConverter;
22
23
    /**
24
     * @param TrashMatcher $trashMatcher
25
     */
26
    public function __construct(TrashMatcher $trashMatcher, SortConverter $sortConverter)
27
    {
28
        $this->trashMatcher = $trashMatcher;
29
        $this->sortConverter = $sortConverter;
30
    }
31
32
    /**
33
     * Handles emptying the trash
34
     */
35
    protected function purge($step)
0 ignored issues
show
Unused Code introduced by
The parameter $step is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $trashService = $this->repository->getTrashService();
38
39
        $trashService->emptyTrash();
40
41
        return true;
42
    }
43
44
    /**
45
     * Handles the trash-restore migration action
46
     *
47
     * @todo support handling of restoration to custom locations
48
     */
49
    protected function recover($step)
50
    {
51
        $itemsCollection = $this->matchItems('restore', $step);
52
53 View Code Duplication
        if (count($itemsCollection) > 1 && array_key_exists('references', $step->dsl)) {
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...
54
            throw new \Exception("Can not execute Trash restore because multiple types match, and a references section is specified in the dsl. References can be set when only 1 section matches");
55
        }
56
57
        $locations = array();
58
        $trashService = $this->repository->getTrashService();
59
        foreach ($itemsCollection as $key => $item) {
0 ignored issues
show
Bug introduced by
The expression $itemsCollection of type object<Kaliop\eZMigratio...hedItemCollection>|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...
60
            $locations[] = $trashService->recover($item);
61
        }
62
63
        $this->setReferences(new LocationCollection($locations), $step);
0 ignored issues
show
Documentation introduced by
new \Kaliop\eZMigrationB...nCollection($locations) is of type object<Kaliop\eZMigratio...ion\LocationCollection>, but the function expects a object<Object>|object<Ka...ctCollectionCollection>.

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...
64
65
        return $itemsCollection;
66
    }
67
68
    /**
69
     * Handles the trash-delete migration action
70
     */
71
    protected function delete($step)
72
    {
73
        $itemsCollection = $this->matchItems('delete', $step);
74
75 View Code Duplication
        if (count($itemsCollection) > 1 && array_key_exists('references', $step->dsl)) {
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...
76
            throw new \Exception("Can not execute Trash restore because multiple types match, and a references section is specified in the dsl. References can be set when only 1 section matches");
77
        }
78
79
        $this->setReferences($itemsCollection, $step);
0 ignored issues
show
Documentation introduced by
$itemsCollection is of type object<Kaliop\eZMigratio...hedItemCollection>|null, but the function expects a object<Object>|object<Ka...ctCollectionCollection>.

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...
80
81
        $trashService = $this->repository->getTrashService();
82
        foreach ($itemsCollection as $key => $item) {
0 ignored issues
show
Bug introduced by
The expression $itemsCollection of type object<Kaliop\eZMigratio...hedItemCollection>|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...
83
            $trashService->deleteTrashItem($item);
84
        }
85
86
        $this->setReferences($itemsCollection, $step);
0 ignored issues
show
Documentation introduced by
$itemsCollection is of type object<Kaliop\eZMigratio...hedItemCollection>|null, but the function expects a object<Object>|object<Ka...ctCollectionCollection>.

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...
87
88
        return $itemsCollection;
89
    }
90
91
    /**
92
     * @param string $action
93
     * @return TrashedItemCollection
94
     * @throws \Exception
95
     */
96 View Code Duplication
    protected function matchItems($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...
97
    {
98
        if (!isset($step->dsl['match'])) {
99
            throw new \Exception("A match condition is required to $action trash items");
100
        }
101
102
        // convert the references passed in the match
103
        $match = $this->resolveReferencesRecursively($step->dsl['match']);
104
105
        return $this->trashMatcher->match($match);
106
    }
107
108
    /**
109
     * @param \eZ\Publish\API\Repository\Values\Content\TrashItem|\eZ\Publish\API\Repository\Values\Content\Location $item
110
     * @param array $references the definitions of the references to set
111
     * @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute
112
     * @return array key: the reference names, values: the reference values
113
     */
114 View Code Duplication
    protected function getReferencesValues($item, array $references)
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...
115
    {
116
        $refs = array();
117
118
        foreach ($references as $reference) {
119
            switch ($reference['attribute']) {
120
                // a trashed item extends a location, so in theory everything 'location' here should work
121
                case 'location_id':
122
                case 'id':
123
                    $value = $item->id;
124
                    break;
125
                case 'remote_id':
126
                case 'location_remote_id':
127
                    $value = $item->remoteId;
128
                    break;
129
                case 'always_available':
130
                    $value = $item->contentInfo->alwaysAvailable;
131
                    break;
132
                case 'content_id':
133
                    $value = $item->contentId;
134
                    break;
135
                case 'content_type_id':
136
                    $value = $item->contentInfo->contentTypeId;
137
                    break;
138
                case 'content_type_identifier':
139
                    $contentTypeService = $this->repository->getContentTypeService();
140
                    $value = $contentTypeService->loadContentType($item->contentInfo->contentTypeId)->identifier;
141
                    break;
142
                case 'current_version':
143
                case 'current_version_no':
144
                    $value = $item->contentInfo->currentVersionNo;
145
                    break;
146
                case 'depth':
147
                    $value = $item->depth;
148
                    break;
149
                case 'is_hidden':
150
                    $value = $item->hidden;
151
                    break;
152
                case 'main_location_id':
153
                    $value = $item->contentInfo->mainLocationId;
154
                    break;
155
                case 'main_language_code':
156
                    $value = $item->contentInfo->mainLanguageCode;
157
                    break;
158
                case 'modification_date':
159
                    $value = $item->contentInfo->modificationDate->getTimestamp();
160
                    break;
161
                case 'name':
162
                    $value = $item->contentInfo->name;
163
                    break;
164
                case 'owner_id':
165
                    $value = $item->contentInfo->ownerId;
166
                    break;
167
                case 'parent_location_id':
168
                    $value = $item->parentLocationId;
169
                    break;
170
                case 'path':
171
                    $value = $item->pathString;
172
                    break;
173
                case 'priority':
174
                    $value = $item->priority;
175
                    break;
176
                case 'publication_date':
177
                    $value = $item->contentInfo->publishedDate->getTimestamp();
178
                    break;
179
                case 'section_id':
180
                    $value = $item->contentInfo->sectionId;
181
                    break;
182
                case 'section_identifier':
183
                    $sectionService = $this->repository->getSectionService();
184
                    $value = $sectionService->loadSection($item->contentInfo->sectionId)->identifier;
185
                    break;
186
                case 'sort_field':
187
                    $value = $this->sortConverter->sortField2Hash($item->sortField);
188
                    break;
189
                case 'sort_order':
190
                    $value = $this->sortConverter->sortOrder2Hash($item->sortOrder);
191
                    break;
192
                default:
193
                    throw new \InvalidArgumentException('Trash Manager does not support setting references for attribute ' . $reference['attribute']);
194
            }
195
196
            $refs[$reference['identifier']] = $value;
197
        }
198
199
        return $refs;
200
    }
201
}
202