1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\REST\Server\Controller; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\BookmarkService; |
12
|
|
|
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException; |
13
|
|
|
use eZ\Publish\API\Repository\LocationService; |
14
|
|
|
use eZ\Publish\Core\REST\Common\Exceptions; |
15
|
|
|
use eZ\Publish\Core\REST\Common\Value as RestValue; |
16
|
|
|
use eZ\Publish\Core\REST\Server\Values; |
17
|
|
|
use eZ\Publish\Core\REST\Server\Controller as RestController; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
|
20
|
|
|
class Bookmark extends RestController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \eZ\Publish\API\Repository\BookmarkService |
24
|
|
|
*/ |
25
|
|
|
protected $bookmarkService; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \eZ\Publish\API\Repository\LocationService |
29
|
|
|
*/ |
30
|
|
|
protected $locationService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Bookmark constructor. |
34
|
|
|
* |
35
|
|
|
* @param \eZ\Publish\API\Repository\BookmarkService $bookmarkService |
36
|
|
|
* @param \eZ\Publish\API\Repository\LocationService $locationService |
37
|
|
|
*/ |
38
|
|
|
public function __construct(BookmarkService $bookmarkService, LocationService $locationService) |
39
|
|
|
{ |
40
|
|
|
$this->bookmarkService = $bookmarkService; |
41
|
|
|
$this->locationService = $locationService; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add given location to bookmarks. |
46
|
|
|
* |
47
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
48
|
|
|
* @param int $locationId |
49
|
|
|
* |
50
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
51
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
52
|
|
|
* |
53
|
|
|
* @return \eZ\Publish\Core\REST\Common\Value |
54
|
|
|
*/ |
55
|
|
|
public function createBookmark(Request $request, int $locationId): RestValue |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$location = $this->locationService->loadLocation($locationId); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
$this->bookmarkService->createBookmark($location); |
61
|
|
|
|
62
|
|
|
return new Values\ResourceCreated( |
63
|
|
|
$this->router->generate( |
64
|
|
|
'ezpublish_rest_isBookmarked', |
65
|
|
|
[ |
66
|
|
|
'locationId' => $locationId, |
67
|
|
|
] |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} catch (InvalidArgumentException $e) { |
71
|
|
|
return new Values\Conflict(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Deletes a given bookmark. |
77
|
|
|
* |
78
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
79
|
|
|
* @param int $locationId |
80
|
|
|
* |
81
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
82
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
83
|
|
|
* |
84
|
|
|
* @return \eZ\Publish\Core\REST\Common\Value |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function deleteBookmark(Request $request, int $locationId): RestValue |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$location = $this->locationService->loadLocation($locationId); |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$this->bookmarkService->deleteBookmark($location); |
92
|
|
|
|
93
|
|
|
return new Values\NoContent(); |
94
|
|
|
} catch (InvalidArgumentException $e) { |
95
|
|
|
throw new Exceptions\NotFoundException("Location {$locationId} is not bookmarked"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Checks if given location is bookmarked. |
101
|
|
|
* |
102
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
103
|
|
|
* @param int $locationId |
104
|
|
|
* |
105
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
106
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
107
|
|
|
* |
108
|
|
|
* @return \eZ\Publish\Core\REST\Server\Values\OK |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function isBookmarked(Request $request, int $locationId): Values\OK |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$location = $this->locationService->loadLocation($locationId); |
113
|
|
|
|
114
|
|
|
if (!$this->bookmarkService->isBookmarked($location)) { |
115
|
|
|
throw new Exceptions\NotFoundException("Location {$locationId} is not bookmarked"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return new Values\OK(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* List bookmarked locations. |
123
|
|
|
* |
124
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
125
|
|
|
* |
126
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
127
|
|
|
* |
128
|
|
|
* @return \eZ\Publish\Core\REST\Common\Value |
129
|
|
|
*/ |
130
|
|
|
public function loadBookmarks(Request $request): RestValue |
131
|
|
|
{ |
132
|
|
|
$offset = $request->query->getInt('offset', 0); |
133
|
|
|
$limit = $request->query->getInt('limit', 25); |
134
|
|
|
|
135
|
|
|
$restLocations = []; |
136
|
|
|
$bookmarks = $this->bookmarkService->loadBookmarks($offset, $limit); |
137
|
|
|
foreach ($bookmarks as $bookmark) { |
138
|
|
|
$restLocations[] = new Values\RestLocation( |
139
|
|
|
$bookmark, |
140
|
|
|
$this->locationService->getLocationChildCount($bookmark) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return new Values\BookmarkList($bookmarks->totalCount, $restLocations); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Extracts and returns an item id from a path, e.g. /1/2/58 => 58. |
149
|
|
|
* |
150
|
|
|
* @param string $path |
151
|
|
|
* |
152
|
|
|
* @return mixed |
153
|
|
|
*/ |
154
|
|
|
private function extractLocationIdFromPath(string $path) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$pathParts = explode('/', $path); |
157
|
|
|
|
158
|
|
|
return array_pop($pathParts); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.