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

TrashService::emptyTrash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * TrashService 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\SignalSlot;
10
11
use eZ\Publish\API\Repository\TrashService as TrashServiceInterface;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\Content\TrashItem;
14
use eZ\Publish\API\Repository\Values\Content\Location;
15
use eZ\Publish\API\Repository\Values\Content\Query;
16
use eZ\Publish\Core\SignalSlot\Signal\TrashService\TrashSignal;
17
use eZ\Publish\Core\SignalSlot\Signal\TrashService\RecoverSignal;
18
use eZ\Publish\Core\SignalSlot\Signal\TrashService\EmptyTrashSignal;
19
use eZ\Publish\Core\SignalSlot\Signal\TrashService\DeleteTrashItemSignal;
20
21
/**
22
 * TrashService class.
23
 */
24
class TrashService implements TrashServiceInterface
25
{
26
    /**
27
     * Aggregated service.
28
     *
29
     * @var \eZ\Publish\API\Repository\TrashService
30
     */
31
    protected $service;
32
33
    /**
34
     * SignalDispatcher.
35
     *
36
     * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher
37
     */
38
    protected $signalDispatcher;
39
40
    /**
41
     * Constructor.
42
     *
43
     * Construct service object from aggregated service and signal
44
     * dispatcher
45
     *
46
     * @param \eZ\Publish\API\Repository\TrashService $service
47
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher
48
     */
49
    public function __construct(TrashServiceInterface $service, SignalDispatcher $signalDispatcher)
50
    {
51
        $this->service = $service;
52
        $this->signalDispatcher = $signalDispatcher;
53
    }
54
55
    /**
56
     * Loads a trashed location object from its $id.
57
     *
58
     * Note that $id is identical to original location, which has been previously trashed
59
     *
60
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location
61
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist
62
     *
63
     * @param mixed $trashItemId
64
     *
65
     * @return \eZ\Publish\API\Repository\Values\Content\TrashItem
66
     */
67
    public function loadTrashItem($trashItemId)
68
    {
69
        return $this->service->loadTrashItem($trashItemId);
70
    }
71
72
    /**
73
     * Sends $location and all its children to trash and returns the corresponding trash item.
74
     *
75
     * The current user may not have access to the returned trash item, check before using it.
76
     * Content is left untouched.
77
     *
78
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location
79
     *
80
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
81
     *
82
     * @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem
83
     */
84 View Code Duplication
    public function trash(Location $location)
85
    {
86
        $returnValue = $this->service->trash($location);
87
        $this->signalDispatcher->emit(
88
            new TrashSignal(
89
                array(
90
                    'locationId' => $location->id,
91
                    'parentLocationId' => $location->parentLocationId,
92
                    'contentId' => $location->contentId,
93
                    'contentTrashed' => $returnValue instanceof TrashItem,
94
                )
95
            )
96
        );
97
98
        return $returnValue;
99
    }
100
101
    /**
102
     * Recovers the $trashedLocation at its original place if possible.
103
     *
104
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
105
     *
106
     * If $newParentLocation is provided, $trashedLocation will be restored under it.
107
     *
108
     * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
109
     * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
110
     *
111
     * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location
112
     */
113
    public function recover(TrashItem $trashItem, Location $newParentLocation = null)
114
    {
115
        $newLocation = $this->service->recover($trashItem, $newParentLocation);
116
        $this->signalDispatcher->emit(
117
            new RecoverSignal(
118
                array(
119
                    'trashItemId' => $trashItem->id,
120
                    'contentId' => $trashItem->contentId,
121
                    'newParentLocationId' => $newLocation->parentLocationId,
122
                    'newLocationId' => $newLocation->id,
123
                )
124
            )
125
        );
126
127
        return $newLocation;
128
    }
129
130
    /**
131
     * Empties trash.
132
     *
133
     * All locations contained in the trash will be removed. Content objects will be removed
134
     * if all locations of the content are gone.
135
     *
136
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash
137
     */
138
    public function emptyTrash()
139
    {
140
        $returnValue = $this->service->emptyTrash();
141
        $this->signalDispatcher->emit(new EmptyTrashSignal(array()));
142
143
        return $returnValue;
144
    }
145
146
    /**
147
     * Deletes a trash item.
148
     *
149
     * The corresponding content object will be removed
150
     *
151
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item
152
     *
153
     * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
154
     */
155
    public function deleteTrashItem(TrashItem $trashItem)
156
    {
157
        $returnValue = $this->service->deleteTrashItem($trashItem);
158
        $this->signalDispatcher->emit(
159
            new DeleteTrashItemSignal(
160
                array(
161
                    'trashItemId' => $trashItem->id,
162
                )
163
            )
164
        );
165
166
        return $returnValue;
167
    }
168
169
    /**
170
     * Returns a collection of Trashed locations contained in the trash, which are readable by the current user.
171
     *
172
     * $query allows to filter/sort the elements to be contained in the collection.
173
     *
174
     * @param \eZ\Publish\API\Repository\Values\Content\Query $query
175
     *
176
     * @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult
177
     */
178
    public function findTrashItems(Query $query)
179
    {
180
        return $this->service->findTrashItems($query);
181
    }
182
183
    /**
184
     * Checks if Content is in trash when we can't rely only on locationId (ie. because ContentId is all we have,
185
     * and Content in trash is not part of a tree anymore, so does not have mainLocationId).
186
     *
187
     * @param int $contentId
188
     *
189
     * @return bool
190
     */
191
    public function isContentInTrash($contentId)
192
    {
193
        return $this->service->isContentInTrash($contentId);
194
    }
195
}
196