1 | <?php |
||
28 | class Location extends RestController |
||
29 | { |
||
30 | /** |
||
31 | * Location service. |
||
32 | * |
||
33 | * @var \eZ\Publish\API\Repository\LocationService |
||
34 | */ |
||
35 | protected $locationService; |
||
36 | |||
37 | /** |
||
38 | * Content service. |
||
39 | * |
||
40 | * @var \eZ\Publish\API\Repository\ContentService |
||
41 | */ |
||
42 | protected $contentService; |
||
43 | |||
44 | /** |
||
45 | * Trash service. |
||
46 | * |
||
47 | * @var \eZ\Publish\API\Repository\TrashService |
||
48 | */ |
||
49 | protected $trashService; |
||
50 | |||
51 | /** |
||
52 | * Construct controller. |
||
53 | * |
||
54 | * @param \eZ\Publish\API\Repository\LocationService $locationService |
||
55 | * @param \eZ\Publish\API\Repository\ContentService $contentService |
||
56 | * @param \eZ\Publish\API\Repository\TrashService $trashService |
||
57 | */ |
||
58 | public function __construct(LocationService $locationService, ContentService $contentService, TrashService $trashService) |
||
64 | |||
65 | /** |
||
66 | * Loads the location for a given ID (x)or remote ID. |
||
67 | * |
||
68 | * @throws \eZ\Publish\Core\REST\Server\Exceptions\BadRequestException |
||
69 | * |
||
70 | * @return \eZ\Publish\Core\REST\Server\Values\TemporaryRedirect |
||
71 | */ |
||
72 | public function redirectLocation(Request $request) |
||
93 | |||
94 | /** |
||
95 | * Creates a new location for object with id $contentId. |
||
96 | * |
||
97 | * @param mixed $contentId |
||
98 | * |
||
99 | * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException |
||
100 | * |
||
101 | * @return \eZ\Publish\Core\REST\Server\Values\CreatedLocation |
||
102 | */ |
||
103 | public function createLocation($contentId, Request $request) |
||
122 | |||
123 | /** |
||
124 | * Loads a location. |
||
125 | * |
||
126 | * @param string $locationPath |
||
127 | * |
||
128 | * @return \eZ\Publish\Core\REST\Server\Values\RestLocation |
||
129 | */ |
||
130 | public function loadLocation($locationPath) |
||
150 | |||
151 | /** |
||
152 | * Deletes a location. |
||
153 | * |
||
154 | * @param string $locationPath |
||
155 | * |
||
156 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
157 | */ |
||
158 | public function deleteSubtree($locationPath) |
||
167 | |||
168 | /** |
||
169 | * Copies a subtree to a new destination. |
||
170 | * |
||
171 | * @param string $locationPath |
||
172 | * |
||
173 | * @return \eZ\Publish\Core\REST\Server\Values\ResourceCreated |
||
174 | */ |
||
175 | public function copySubtree($locationPath, Request $request) |
||
201 | |||
202 | /** |
||
203 | * Moves a subtree to a new location. |
||
204 | * |
||
205 | * @param string $locationPath |
||
206 | * |
||
207 | * @throws \eZ\Publish\Core\REST\Server\Exceptions\BadRequestException if the Destination header cannot be parsed as location or trash |
||
208 | * |
||
209 | * @return \eZ\Publish\Core\REST\Server\Values\ResourceCreated | \eZ\Publish\Core\REST\Server\Values\NoContent |
||
210 | */ |
||
211 | public function moveSubtree($locationPath, Request $request) |
||
212 | { |
||
213 | $locationToMove = $this->locationService->loadLocation( |
||
214 | $this->extractLocationIdFromPath($locationPath) |
||
215 | ); |
||
216 | |||
217 | $destinationLocationId = null; |
||
218 | $destinationHref = $request->headers->get('Destination'); |
||
219 | try { |
||
220 | // First check to see if the destination is for moving within another subtree |
||
221 | $destinationLocationId = $this->extractLocationIdFromPath( |
||
222 | $this->requestParser->parseHref($destinationHref, 'locationPath') |
||
223 | ); |
||
224 | |||
225 | // We're moving the subtree |
||
226 | $destinationLocation = $this->locationService->loadLocation($destinationLocationId); |
||
227 | $this->locationService->moveSubtree($locationToMove, $destinationLocation); |
||
228 | |||
229 | // Reload the location to get the new position is subtree |
||
230 | $locationToMove = $this->locationService->loadLocation($locationToMove->id); |
||
231 | |||
232 | return new Values\ResourceCreated( |
||
233 | $this->router->generate( |
||
234 | 'ezpublish_rest_loadLocation', |
||
235 | array( |
||
236 | 'locationPath' => trim($locationToMove->pathString, '/'), |
||
237 | ) |
||
238 | ) |
||
239 | ); |
||
240 | } catch (Exceptions\InvalidArgumentException $e) { |
||
241 | // If parsing of destination fails, let's try to see if destination is trash |
||
242 | try { |
||
243 | $route = $this->requestParser->parse($destinationHref); |
||
244 | if (!isset($route['_route']) || $route['_route'] !== 'ezpublish_rest_loadTrashItems') { |
||
245 | throw new Exceptions\InvalidArgumentException(''); |
||
246 | } |
||
247 | // Trash the subtree |
||
248 | $trashItem = $this->trashService->trash($locationToMove); |
||
249 | |||
250 | if (isset($trashItem)) { |
||
251 | return new Values\ResourceCreated( |
||
252 | $this->router->generate( |
||
253 | 'ezpublish_rest_loadTrashItem', |
||
254 | array('trashItemId' => $trashItem->id) |
||
255 | ) |
||
256 | ); |
||
257 | } else { |
||
258 | // Only a location has been trashed and not the object |
||
259 | return new Values\NoContent(); |
||
260 | } |
||
261 | } catch (Exceptions\InvalidArgumentException $e) { |
||
262 | // If that fails, the Destination header is not formatted right |
||
263 | // so just throw the BadRequestException |
||
264 | throw new BadRequestException("{$destinationHref} is not an acceptable destination"); |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Swaps a location with another one. |
||
271 | * |
||
272 | * @param string $locationPath |
||
273 | * |
||
274 | * @return \eZ\Publish\Core\REST\Server\Values\NoContent |
||
275 | */ |
||
276 | public function swapLocation($locationPath, Request $request) |
||
294 | |||
295 | /** |
||
296 | * Loads a location by remote ID. |
||
297 | * |
||
298 | * @todo remove, or use in loadLocation with filter |
||
299 | * |
||
300 | * @return \eZ\Publish\Core\REST\Server\Values\LocationList |
||
301 | */ |
||
302 | public function loadLocationByRemoteId(Request $request) |
||
316 | |||
317 | /** |
||
318 | * Loads all locations for content object. |
||
319 | * |
||
320 | * @param mixed $contentId |
||
321 | * |
||
322 | * @return \eZ\Publish\Core\REST\Server\Values\LocationList |
||
323 | */ |
||
324 | public function loadLocationsForContent($contentId, Request $request) |
||
341 | |||
342 | /** |
||
343 | * Loads child locations of a location. |
||
344 | * |
||
345 | * @param string $locationPath |
||
346 | * |
||
347 | * @return \eZ\Publish\Core\REST\Server\Values\LocationList |
||
348 | */ |
||
349 | public function loadLocationChildren($locationPath, Request $request) |
||
373 | |||
374 | /** |
||
375 | * Extracts and returns an item id from a path, e.g. /1/2/58 => 58. |
||
376 | * |
||
377 | * @param string $path |
||
378 | * |
||
379 | * @return mixed |
||
380 | */ |
||
381 | private function extractLocationIdFromPath($path) |
||
387 | |||
388 | /** |
||
389 | * Updates a location. |
||
390 | * |
||
391 | * @param string $locationPath |
||
392 | * |
||
393 | * @return \eZ\Publish\Core\REST\Server\Values\RestLocation |
||
394 | */ |
||
395 | public function updateLocation($locationPath, Request $request) |
||
421 | } |
||
422 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.