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

TrashService::isContentInTrash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
rs 10
1
<?php
2
3
/**
4
 * File containing the 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
10
namespace eZ\Publish\Core\REST\Client;
11
12
use eZ\Publish\API\Repository\TrashService as APITrashService;
13
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14
use eZ\Publish\API\Repository\Values\Content\Query;
15
use eZ\Publish\API\Repository\Values\Content\Location;
16
use eZ\Publish\API\Repository\Values\Content\Trash\SearchResult;
17
use eZ\Publish\API\Repository\Values\Content\TrashItem as APITrashItem;
18
use eZ\Publish\Core\Repository\Values\Content\TrashItem;
19
use eZ\Publish\Core\REST\Common\RequestParser;
20
use eZ\Publish\Core\REST\Common\Input\Dispatcher;
21
use eZ\Publish\Core\REST\Common\Output\Visitor;
22
use eZ\Publish\Core\REST\Common\Message;
23
24
/**
25
 * Trash service used for content/location trash handling.
26
 */
27
class TrashService implements APITrashService, Sessionable
28
{
29
    /**
30
     * @var \eZ\Publish\Core\REST\Client\LocationService
31
     */
32
    private $locationService;
33
34
    /**
35
     * @var \eZ\Publish\Core\REST\Client\HttpClient
36
     */
37
    private $client;
38
39
    /**
40
     * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher
41
     */
42
    private $inputDispatcher;
43
44
    /**
45
     * @var \eZ\Publish\Core\REST\Common\Output\Visitor
46
     */
47
    private $outputVisitor;
48
49
    /**
50
     * @var \eZ\Publish\Core\REST\Common\RequestParser
51
     */
52
    private $requestParser;
53
54
    /**
55
     * @param \eZ\Publish\Core\REST\Client\LocationService $locationService
56
     * @param \eZ\Publish\Core\REST\Client\HttpClient $client
57
     * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher
58
     * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor
59
     * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser
60
     */
61 View Code Duplication
    public function __construct(LocationService $locationService, HttpClient $client, Dispatcher $inputDispatcher, Visitor $outputVisitor, RequestParser $requestParser)
62
    {
63
        $this->locationService = $locationService;
64
        $this->client = $client;
65
        $this->inputDispatcher = $inputDispatcher;
66
        $this->outputVisitor = $outputVisitor;
67
        $this->requestParser = $requestParser;
68
    }
69
70
    /**
71
     * Set session ID.
72
     *
73
     * Only for testing
74
     *
75
     * @param mixed $id
76
     */
77
    public function setSession($id)
78
    {
79
        if ($this->outputVisitor instanceof Sessionable) {
80
            $this->outputVisitor->setSession($id);
81
        }
82
    }
83
84
    /**
85
     * Loads a trashed location object from its $id.
86
     *
87
     * Note that $id is identical to original location, which has been previously trashed
88
     *
89
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location
90
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist
91
     *
92
     * @param mixed $trashItemId
93
     *
94
     * @return \eZ\Publish\API\Repository\Values\Content\TrashItem
95
     */
96
    public function loadTrashItem($trashItemId)
97
    {
98
        $response = $this->client->request(
99
            'GET',
100
            $trashItemId,
101
            new Message(
102
                array('Accept' => $this->outputVisitor->getMediaType('Location'))
103
            )
104
        );
105
106
        $location = $this->inputDispatcher->parse($response);
107
108
        return $this->buildTrashItem($location);
0 ignored issues
show
Compatibility introduced by
$location of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not a sub-type of object<eZ\Publish\API\Re...alues\Content\Location>. It seems like you assume a child class of the class eZ\Publish\API\Repository\Values\ValueObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
109
    }
110
111
    /**
112
     * Sends $location and all its children to trash and returns the corresponding trash item.
113
     *
114
     * The current user may not have access to the returned trash item, check before using it.
115
     * Content is left untouched.
116
     *
117
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location
118
     *
119
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
120
     *
121
     * @return \eZ\Publish\API\Repository\Values\Content\TrashItem
122
     */
123
    public function trash(Location $location)
124
    {
125
        throw new \Exception('@todo: Implement.');
126
    }
127
128
    /**
129
     * Recovers the $trashedLocation at its original place if possible.
130
     *
131
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
132
     *
133
     * If $newParentLocation is provided, $trashedLocation will be restored under it.
134
     *
135
     * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
136
     * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
137
     *
138
     * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location
139
     */
140
    public function recover(APITrashItem $trashItem, Location $newParentLocation = null)
141
    {
142
        throw new \Exception('@todo: Implement.');
143
    }
144
145
    /**
146
     * Empties trash.
147
     *
148
     * All locations contained in the trash will be removed. Content objects will be removed
149
     * if all locations of the content are gone.
150
     *
151
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash
152
     */
153 View Code Duplication
    public function emptyTrash()
154
    {
155
        $response = $this->client->request(
156
            'DELETE',
157
            $this->requestParser->generate('trashItems'),
158
            new Message(
159
                // @todo: What media-type should we set here? Actually, it should be
160
                // all expected exceptions + none? Or is "Location" correct,
161
                // since this is what is to be expected by the resource
162
                // identified by the URL?
163
                array('Accept' => $this->outputVisitor->getMediaType('Location'))
164
            )
165
        );
166
167
        if (!empty($response->body)) {
168
            $this->inputDispatcher->parse($response);
169
        }
170
    }
171
172
    /**
173
     * Deletes a trash item.
174
     *
175
     * The corresponding content object will be removed
176
     *
177
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item
178
     *
179
     * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
180
     */
181 View Code Duplication
    public function deleteTrashItem(APITrashItem $trashItem)
182
    {
183
        $response = $this->client->request(
184
            'DELETE',
185
            $trashItem->id,
186
            new Message(
187
                // @todo: What media-type should we set here? Actually, it should be
188
                // all expected exceptions + none? Or is "Location" correct,
189
                // since this is what is to be expected by the resource
190
                // identified by the URL?
191
                array('Accept' => $this->outputVisitor->getMediaType('Location'))
192
            )
193
        );
194
195
        if (!empty($response->body)) {
196
            $this->inputDispatcher->parse($response);
197
        }
198
    }
199
200
    /**
201
     * Returns a collection of Trashed locations contained in the trash, which are readable by the current user.
202
     *
203
     * $query allows to filter/sort the elements to be contained in the collection.
204
     *
205
     * @param \eZ\Publish\API\Repository\Values\Content\Query $query
206
     *
207
     * @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult
208
     */
209 View Code Duplication
    public function findTrashItems(Query $query)
210
    {
211
        $response = $this->client->request(
212
            'GET',
213
            $this->requestParser->generate('trashItems'),
214
            new Message(
215
                array('Accept' => $this->outputVisitor->getMediaType('LocationList'))
216
            )
217
        );
218
219
        $locations = $this->inputDispatcher->parse($response);
220
221
        $trashItems = array();
222
        foreach ($locations as $location) {
0 ignored issues
show
Bug introduced by
The expression $locations of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not traversable.
Loading history...
223
            $trashItems[] = $this->buildTrashItem($location);
224
        }
225
226
227
        return new SearchResult([
228
            'items' => $trashItems,
229
            'totalCount' => count($trashItems),
230
        ]);
231
    }
232
233
    /**
234
     * Converts the Location value object to TrashItem value object.
235
     *
236
     * @param \eZ\Publish\API\Repository\Values\Content\Location $location
237
     *
238
     * @return \eZ\Publish\API\Repository\Values\Content\TrashItem
239
     */
240 View Code Duplication
    protected function buildTrashItem(Location $location)
241
    {
242
        return new TrashItem(
243
            array(
244
                'contentInfo' => $location->contentInfo,
245
                'id' => $location->id,
246
                'priority' => $location->priority,
247
                'hidden' => $location->hidden,
248
                'invisible' => $location->invisible,
249
                'remoteId' => $location->remoteId,
250
                'parentLocationId' => $location->parentLocationId,
251
                'pathString' => $location->pathString,
252
                'depth' => (int)$location->depth,
253
                'sortField' => $location->sortField,
254
                'sortOrder' => $location->sortOrder,
255
            )
256
        );
257
    }
258
259
    /**
260
     * Checks if Content is in trash when we can't rely only on locationId (ie. because ContentId is all we have,
261
     * and Content in trash is not part of a tree anymore, so does not have mainLocationId.
262
     *
263
     * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo
0 ignored issues
show
Bug introduced by
There is no parameter named $contentInfo. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
264
     *
265
     * @return bool
266
     */
267
    public function isContentInTrash($contentId)
268
    {
269
        throw new \Exception('@todo: Implement.');
270
    }
271
}
272