TrashManager::purge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    protected function purge(/** @scrutinizer ignore-unused */ $step)

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

Loading history...
37
    {
38 1
        $trashService = $this->repository->getTrashService();
39
40 1
        $trashService->emptyTrash();
41
42 1
        return true;
43
    }
44
45
    /**
46
     * Handles the trash-restore migration action
47
     *
48
     * @todo support handling of restoration to custom locations
49
     */
50 1
    protected function recover($step)
51
    {
52 1
        $itemsCollection = $this->matchItems('restore', $step);
53
54 1
        $this->validateResultsCount($itemsCollection, $step);
55
56 1
        $locations = array();
57 1
        $trashService = $this->repository->getTrashService();
58 1
        foreach ($itemsCollection as $key => $item) {
59 1
            $locations[] = $trashService->recover($item);
60
        }
61
62 1
        $this->setReferences(new LocationCollection($locations), $step);
63
64 1
        return $itemsCollection;
65
    }
66
67 1
    protected function load($step)
68
    {
69 1
        $itemsCollection = $this->matchItems('load', $step);
70
71 1
        $this->validateResultsCount($itemsCollection, $step);
72
73 1
        $this->setReferences($itemsCollection, $step);
74
75 1
        return $itemsCollection;
76
    }
77
78
    /**
79
     * Handles the trash-delete migration action
80
     */
81 1
    protected function delete($step)
82
    {
83 1
        $itemsCollection = $this->matchItems('delete', $step);
84
85 1
        $this->validateResultsCount($itemsCollection, $step);
86
87 1
        $this->setReferences($itemsCollection, $step);
88
89 1
        $trashService = $this->repository->getTrashService();
90 1
        foreach ($itemsCollection as $key => $item) {
91 1
            $trashService->deleteTrashItem($item);
92
        }
93
94 1
        $this->setReferences($itemsCollection, $step);
95
96 1
        return $itemsCollection;
97
    }
98
99
    /**
100
     * @param string $action
101
     * @return TrashedItemCollection
102
     * @throws \Exception
103
     */
104 1
    protected function matchItems($action, $step)
105
    {
106 1
        if (!isset($step->dsl['match'])) {
107
            throw new InvalidStepDefinitionException("A match condition is required to $action trash items");
108
        }
109
110
        // convert the references passed in the match
111 1
        $match = $this->resolveReferencesRecursively($step->dsl['match']);
112
113 1
        return $this->trashMatcher->match($match);
114
    }
115
116
    /**
117
     * @param \eZ\Publish\API\Repository\Values\Content\TrashItem|\eZ\Publish\API\Repository\Values\Content\Location $item
118
     * @param array $references the definitions of the references to set
119
     * @param $step
120
     * @throws InvalidStepDefinitionException
121
     * @return array key: the reference names, values: the reference values
122
     */
123 1
    protected function getReferencesValues($item, array $references, $step)
124
    {
125 1
        $refs = array();
126
127 1
        foreach ($references as $key => $reference) {
128 1
            $reference = $this->parseReferenceDefinition($key, $reference);
129 1
            switch ($reference['attribute']) {
130
                // a trashed item extends a location, so in theory everything 'location' here should work
131 1
                case 'location_id':
132 1
                case 'id':
133 1
                    $value = $item->id;
134 1
                    break;
135 1
                case 'remote_id':
136 1
                case 'location_remote_id':
137
                    $value = $item->remoteId;
138
                    break;
139 1
                case 'always_available':
140
                    $value = $item->contentInfo->alwaysAvailable;
141
                    break;
142 1
                case 'content_id':
143 1
                    $value = $item->contentId;
144 1
                    break;
145
                case 'content_type_id':
146
                    $value = $item->contentInfo->contentTypeId;
147
                    break;
148
                case 'content_type_identifier':
149
                    $contentTypeService = $this->repository->getContentTypeService();
150
                    $value = $contentTypeService->loadContentType($item->contentInfo->contentTypeId)->identifier;
151
                    break;
152
                case 'current_version':
153
                case 'current_version_no':
154
                    $value = $item->contentInfo->currentVersionNo;
155
                    break;
156
                case 'depth':
157
                    $value = $item->depth;
158
                    break;
159
                case 'is_hidden':
160
                    $value = $item->hidden;
161
                    break;
162
                case 'main_location_id':
163
                    $value = $item->contentInfo->mainLocationId;
164
                    break;
165
                case 'main_language_code':
166
                    $value = $item->contentInfo->mainLanguageCode;
167
                    break;
168
                case 'modification_date':
169
                    $value = $item->contentInfo->modificationDate->getTimestamp();
170
                    break;
171
                case 'name':
172
                    $value = $item->contentInfo->name;
173
                    break;
174
                case 'owner_id':
175
                    $value = $item->contentInfo->ownerId;
176
                    break;
177
                case 'parent_location_id':
178
                    $value = $item->parentLocationId;
179
                    break;
180
                case 'path':
181
                    $value = $item->pathString;
182
                    break;
183
                case 'priority':
184
                    $value = $item->priority;
185
                    break;
186
                case 'publication_date':
187
                    $value = $item->contentInfo->publishedDate->getTimestamp();
188
                    break;
189
                case 'section_id':
190
                    $value = $item->contentInfo->sectionId;
191
                    break;
192
                case 'section_identifier':
193
                    $sectionService = $this->repository->getSectionService();
194
                    $value = $sectionService->loadSection($item->contentInfo->sectionId)->identifier;
195
                    break;
196
                case 'sort_field':
197
                    $value = $this->sortConverter->sortField2Hash($item->sortField);
198
                    break;
199
                case 'sort_order':
200
                    $value = $this->sortConverter->sortOrder2Hash($item->sortOrder);
201
                    break;
202
                default:
203
                    throw new InvalidStepDefinitionException('Trash Manager does not support setting references for attribute ' . $reference['attribute']);
204
            }
205
206 1
            $refs[$reference['identifier']] = $value;
207
        }
208
209 1
        return $refs;
210
    }
211
}
212