Completed
Push — ezp-28439 ( b6528d )
by
unknown
19:13
created

Handler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 230
c 0
b 0
f 0
rs 10
wmc 19
lcom 1
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A loadTrashItem() 0 6 1
A loadTrashItemsByContentId() 0 8 1
C trashSubtree() 0 41 8
A recover() 0 4 1
A findTrashItems() 0 12 2
A emptyTrash() 0 9 2
A deleteTrashItem() 0 4 1
A delete() 0 8 2
1
<?php
2
3
/**
4
 * File containing the Trash Handler class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Content\Location\Trash;
10
11
use eZ\Publish\SPI\Persistence\Content\Location\Trashed;
12
use eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler as BaseTrashHandler;
13
use eZ\Publish\Core\Persistence\Legacy\Content\Handler as ContentHandler;
14
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
15
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler as LocationHandler;
16
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway;
17
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper;
18
19
/**
20
 * The Location Handler interface defines operations on Location elements in the storage engine.
21
 */
22
class Handler implements BaseTrashHandler
23
{
24
    /**
25
     * Location handler.
26
     *
27
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler
28
     */
29
    protected $locationHandler;
30
31
    /**
32
     * Gateway for handling location data.
33
     *
34
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway
35
     */
36
    protected $locationGateway;
37
38
    /**
39
     * Mapper for handling location data.
40
     *
41
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper
42
     */
43
    protected $locationMapper;
44
45
    /**
46
     * Content handler.
47
     *
48
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\Handler
49
     */
50
    protected $contentHandler;
51
52
    /**
53
     * Construct from userGateway.
54
     *
55
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler $locationHandler
56
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway
57
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper
58
     * @param \eZ\Publish\Core\Persistence\Legacy\Content\Handler $contentHandler
59
     *
60
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Trash\Handler
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
61
     */
62
    public function __construct(
63
        LocationHandler $locationHandler,
64
        LocationGateway $locationGateway,
65
        LocationMapper $locationMapper,
66
        ContentHandler $contentHandler
67
    ) {
68
        $this->locationHandler = $locationHandler;
69
        $this->locationGateway = $locationGateway;
70
        $this->locationMapper = $locationMapper;
71
        $this->contentHandler = $contentHandler;
72
    }
73
74
    /**
75
     * Loads the data for the trashed location identified by $id.
76
     * $id is the same as original location (which has been previously trashed).
77
     *
78
     * @param int $id
79
     *
80
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
81
     *
82
     * @return \eZ\Publish\SPI\Persistence\Content\Location\Trashed
83
     */
84
    public function loadTrashItem($id)
85
    {
86
        $data = $this->locationGateway->loadTrashByLocation($id);
87
88
        return $this->locationMapper->createLocationFromRow($data, null, new Trashed());
89
    }
90
91
    /**
92
     * Loads the data for the trashed locations identified by Content object $id.
93
     *
94
     * @param int $id
95
     *
96
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
97
     *
98
     * @return \eZ\Publish\SPI\Persistence\Content\Location\Trashed[]
99
     */
100
    public function loadTrashItemsByContentId($id)
101
    {
102
        $rows = $this->locationGateway->loadTrashByContent($id);
103
104
        return array_map(function($data) {
105
            return $this->locationMapper->createLocationFromRow($data, null, new Trashed());
106
        }, $rows);
107
    }
108
109
    /**
110
     * Sends a subtree starting to $locationId to the trash
111
     * and returns a Trashed object corresponding to $locationId.
112
     *
113
     * Moves all locations in the subtree to the Trash. The associated content
114
     * objects are left untouched.
115
     *
116
     * @param mixed $locationId
117
     *
118
     * @todo Handle field types actions
119
     *
120
     * @return null|\eZ\Publish\SPI\Persistence\Content\Location\Trashed null if location was deleted, otherwise Trashed object
121
     */
122
    public function trashSubtree($locationId)
123
    {
124
        $locationRows = $this->locationGateway->getSubtreeContent($locationId);
125
        $isLocationRemoved = false;
126
        $parentLocationId = null;
127
128
        foreach ($locationRows as $locationRow) {
129
            if ($locationRow['node_id'] == $locationId) {
130
                $parentLocationId = $locationRow['parent_node_id'];
131
            }
132
133
            if ($this->locationGateway->countLocationsByContentId($locationRow['contentobject_id']) == 1) {
134
                $this->locationGateway->trashLocation($locationRow['node_id']);
135
            } else {
136
                if ($locationRow['node_id'] == $locationId) {
137
                    $isLocationRemoved = true;
138
                }
139
                $this->locationGateway->removeLocation($locationRow['node_id']);
140
141
                if ($locationRow['node_id'] == $locationRow['main_node_id']) {
142
                    $newMainLocationRow = $this->locationGateway->getFallbackMainNodeData(
143
                        $locationRow['contentobject_id'],
144
                        $locationRow['node_id']
145
                    );
146
147
                    $this->locationHandler->changeMainLocation(
148
                        $locationRow['contentobject_id'],
149
                        $newMainLocationRow['node_id'],
150
                        $newMainLocationRow['contentobject_version'],
151
                        $newMainLocationRow['parent_node_id']
152
                    );
153
                }
154
            }
155
        }
156
157
        if (isset($parentLocationId)) {
158
            $this->locationHandler->markSubtreeModified($parentLocationId, time());
159
        }
160
161
        return $isLocationRemoved ? null : $this->loadTrashItem($locationId);
162
    }
163
164
    /**
165
     * Returns a trashed location to normal state.
166
     *
167
     * Recreates the originally trashed location in the new position.
168
     * If this is not possible (because the old location does not exist any more),
169
     * a ParentNotFound exception is thrown.
170
     *
171
     * Returns newly restored location Id.
172
     *
173
     * @param mixed $trashedId
174
     * @param mixed $newParentId
175
     *
176
     * @return int Newly restored location id
177
     *
178
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException If $newParentId is invalid
179
     *
180
     * @todo Handle field types actions
181
     */
182
    public function recover($trashedId, $newParentId)
183
    {
184
        return $this->locationGateway->untrashLocation($trashedId, $newParentId)->id;
185
    }
186
187
    /**
188
     * Returns an array of all trashed locations satisfying the $criterion (if provided),
189
     * sorted with SortClause objects contained in $sort (if any).
190
     * If no criterion is provided (null), no filter is applied.
191
     *
192
     * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
193
     * @param int $offset Offset to start listing from, 0 by default
194
     * @param int $limit Limit for the listing. Null by default (no limit)
195
     * @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause[] $sort
196
     *
197
     * @return \eZ\Publish\SPI\Persistence\Content\Location\Trashed[]
198
     */
199
    public function findTrashItems(Criterion $criterion = null, $offset = 0, $limit = null, array $sort = null)
200
    {
201
        // CBA: Ignore criterion for now.
202
        $rows = $this->locationGateway->listTrashed($offset, $limit, $sort);
203
        $items = array();
204
205
        foreach ($rows as $row) {
206
            $items[] = $this->locationMapper->createLocationFromRow($row, null, new Trashed());
207
        }
208
209
        return $items;
210
    }
211
212
    /**
213
     * Empties the trash
214
     * Everything contained in the trash must be removed.
215
     */
216
    public function emptyTrash()
217
    {
218
        $trashedItems = $this->findTrashItems();
219
        foreach ($trashedItems as $item) {
220
            $this->delete($item);
221
        }
222
223
        $this->locationGateway->cleanupTrash();
224
    }
225
226
    /**
227
     * Removes a trashed location identified by $trashedLocationId from trash
228
     * Associated content has to be deleted.
229
     *
230
     * @param int $trashedId
231
     */
232
    public function deleteTrashItem($trashedId)
233
    {
234
        $this->delete($this->loadTrashItem($trashedId));
235
    }
236
237
    /**
238
     * Triggers delete operations for $trashItem.
239
     * If there is no more locations for corresponding content, then it will be deleted as well.
240
     *
241
     * @param \eZ\Publish\SPI\Persistence\Content\Location\Trashed $trashItem
242
     */
243
    protected function delete(Trashed $trashItem)
244
    {
245
        $this->locationGateway->removeElementFromTrash($trashItem->id);
246
247
        if ($this->locationGateway->countLocationsByContentId($trashItem->contentId) < 1) {
248
            $this->contentHandler->deleteContent($trashItem->contentId);
249
        }
250
    }
251
}
252