Passed
Push — master ( 03dfdf...d55943 )
by Gaetano
10:43
created

TrashManager::purge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 1
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 96
     */
27
    public function __construct(TrashMatcher $trashMatcher, SortConverter $sortConverter)
28 96
    {
29 96
        $this->trashMatcher = $trashMatcher;
30 96
        $this->sortConverter = $sortConverter;
31
    }
32
33
    /**
34
     * Handles emptying the trash
35 1
     */
36
    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 1
    {
38
        $trashService = $this->repository->getTrashService();
39 1
40
        $trashService->emptyTrash();
41 1
42
        return true;
43
    }
44
45
    /**
46
     * Handles the trash-restore migration action
47
     *
48
     * @todo support handling of restoration to custom locations
49 1
     */
50
    protected function recover($step)
51 1
    {
52
        $itemsCollection = $this->matchItems('restore', $step);
53 1
54
        $this->validateResultsCount($itemsCollection, $step);
55
56
        $locations = array();
57 1
        $trashService = $this->repository->getTrashService();
58 1
        foreach ($itemsCollection as $key => $item) {
59 1
            $locations[] = $trashService->recover($item);
60 1
        }
61
62
        $this->setReferences(new LocationCollection($locations), $step);
63 1
64
        return $itemsCollection;
65 1
    }
66
67
    protected function load($step)
68
    {
69
        $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 1
78 1
    /**
79 1
     * Handles the trash-delete migration action
80
     */
81
    protected function delete($step)
82 1
    {
83
        $itemsCollection = $this->matchItems('delete', $step);
84 1
85
        $this->validateResultsCount($itemsCollection, $step);
86
87
        $this->setReferences($itemsCollection, $step);
88
89
        $trashService = $this->repository->getTrashService();
90
        foreach ($itemsCollection as $key => $item) {
91
            $trashService->deleteTrashItem($item);
92 1
        }
93
94 1
        $this->setReferences($itemsCollection, $step);
95
96
        return $itemsCollection;
97
    }
98
99 1
    /**
100
     * @param string $action
101 1
     * @return TrashedItemCollection
102
     * @throws \Exception
103
     */
104
    protected function matchItems($action, $step)
105
    {
106
        if (!isset($step->dsl['match'])) {
107
            throw new InvalidStepDefinitionException("A match condition is required to $action trash items");
108
        }
109
110 1
        // convert the references passed in the match
111
        $match = $this->resolveReferencesRecursively($step->dsl['match']);
112 1
113
        return $this->trashMatcher->match($match);
114 1
    }
115 1
116
    /**
117 1
     * @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 1
     * @param $step
120 1
     * @throws InvalidStepDefinitionException
121
     * @return array key: the reference names, values: the reference values
122
     */
123
    protected function getReferencesValues($item, array $references, $step)
124
    {
125
        $refs = array();
126
127
        foreach ($references as $key => $reference) {
128
            $reference = $this->parseReferenceDefinition($key, $reference);
129
            switch ($reference['attribute']) {
130
                // a trashed item extends a location, so in theory everything 'location' here should work
131
                case 'location_id':
132
                case 'id':
133
                    $value = $item->id;
134
                    break;
135
                case 'remote_id':
136
                case 'location_remote_id':
137
                    $value = $item->remoteId;
138
                    break;
139
                case 'always_available':
140
                    $value = $item->contentInfo->alwaysAvailable;
141
                    break;
142
                case 'content_id':
143
                    $value = $item->contentId;
144
                    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 1
                case 'section_identifier':
193
                    $sectionService = $this->repository->getSectionService();
194
                    $value = $sectionService->loadSection($item->contentInfo->sectionId)->identifier;
195 1
                    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
            $refs[$reference['identifier']] = $value;
207
        }
208
209
        return $refs;
210
    }
211
}
212