Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/REST/Server/Controller/Bookmark.php
Go to the documentation of this file.
1 <?php
2
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 {
23 protected $bookmarkService;
24
26 protected $locationService;
27
34 public function __construct(BookmarkService $bookmarkService, LocationService $locationService)
35 {
36 $this->bookmarkService = $bookmarkService;
37 $this->locationService = $locationService;
38 }
39
51 public function createBookmark(Request $request, int $locationId): RestValue
52 {
53 $location = $this->locationService->loadLocation($locationId);
54
55 try {
56 $this->bookmarkService->createBookmark($location);
57
58 return new Values\ResourceCreated(
59 $this->router->generate(
60 'ezpublish_rest_isBookmarked',
61 [
62 'locationId' => $locationId,
63 ]
64 )
65 );
66 } catch (InvalidArgumentException $e) {
67 return new Values\Conflict();
68 }
69 }
70
82 public function deleteBookmark(Request $request, int $locationId): RestValue
83 {
84 $location = $this->locationService->loadLocation($locationId);
85
86 try {
87 $this->bookmarkService->deleteBookmark($location);
88
89 return new Values\NoContent();
90 } catch (InvalidArgumentException $e) {
91 throw new Exceptions\NotFoundException("Location {$locationId} is not bookmarked");
92 }
93 }
94
106 public function isBookmarked(Request $request, int $locationId): Values\OK
107 {
108 $location = $this->locationService->loadLocation($locationId);
109
110 if (!$this->bookmarkService->isBookmarked($location)) {
111 throw new Exceptions\NotFoundException("Location {$locationId} is not bookmarked");
112 }
113
114 return new Values\OK();
115 }
116
126 public function loadBookmarks(Request $request): RestValue
127 {
128 $offset = $request->query->getInt('offset', 0);
129 $limit = $request->query->getInt('limit', 25);
130
131 $restLocations = [];
132 $bookmarks = $this->bookmarkService->loadBookmarks($offset, $limit);
133 foreach ($bookmarks as $bookmark) {
134 $restLocations[] = new Values\RestLocation(
135 $bookmark,
136 $this->locationService->getLocationChildCount($bookmark)
137 );
138 }
139
140 return new Values\BookmarkList($bookmarks->totalCount, $restLocations);
141 }
142
150 private function extractLocationIdFromPath(string $path)
151 {
152 $pathParts = explode('/', $path);
153
154 return array_pop($pathParts);
155 }
156 }
eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
This exception is thrown if a service method is called with an illegal or non appropriate value.
Definition: API/Repository/Exceptions/InvalidArgumentException.php:13
eZ\Publish\Core\REST\Common\Value
Definition: Core/REST/Common/Value.php:10
eZ\Publish\Core\REST\Server\Controller\Bookmark
Definition: Core/REST/Server/Controller/Bookmark.php:21
eZ\Publish\Core\REST\Server\Controller\Bookmark\deleteBookmark
deleteBookmark(Request $request, int $locationId)
Deletes a given bookmark.
Definition: Core/REST/Server/Controller/Bookmark.php:82
eZ\Publish\Core\REST\Server\Controller\Bookmark\$bookmarkService
$bookmarkService
Definition: Core/REST/Server/Controller/Bookmark.php:23
eZ\Publish\Core\REST\Server\Controller\Bookmark\$locationService
$locationService
Definition: Core/REST/Server/Controller/Bookmark.php:26
eZ\Publish\Core\REST\Server\Controller\Bookmark\extractLocationIdFromPath
extractLocationIdFromPath(string $path)
Extracts and returns an item id from a path, e.g.
Definition: Core/REST/Server/Controller/Bookmark.php:150
eZ\Publish\Core\REST\Server\Controller\Bookmark\__construct
__construct(BookmarkService $bookmarkService, LocationService $locationService)
Bookmark constructor.
Definition: Core/REST/Server/Controller/Bookmark.php:34
eZ\Publish\Core\REST\Server\Controller\Bookmark\isBookmarked
isBookmarked(Request $request, int $locationId)
Checks if given location is bookmarked.
Definition: Core/REST/Server/Controller/Bookmark.php:106
eZ\Publish\Core\REST\Server\Controller\Bookmark\createBookmark
createBookmark(Request $request, int $locationId)
Add given location to bookmarks.
Definition: Core/REST/Server/Controller/Bookmark.php:51
eZ\Publish\Core\REST\Server\Controller\Bookmark\loadBookmarks
loadBookmarks(Request $request)
List bookmarked locations.
Definition: Core/REST/Server/Controller/Bookmark.php:126
eZ\Publish\Core\REST\Server\Values\BookmarkList
Definition: Core/REST/Server/Values/BookmarkList.php:14
eZ\Publish\Core\REST\Server\Values\Conflict
Definition: Values/Conflict.php:12
eZ\Publish\Core\REST\Server\Values\NoContent
Definition: Values/NoContent.php:12
eZ\Publish\Core\REST\Server\Values\OK
Value object for a 200 OK response.
Definition: Values/OK.php:13
eZ\Publish\Core\REST\Server\Values\ResourceCreated
Definition: Values/ResourceCreated.php:12
eZ\Publish\Core\REST\Server\Values\RestLocation
RestLocation view model.
Definition: Values/RestLocation.php:16
eZ\Publish\API\Repository\BookmarkService
Bookmark Service.
Definition: API/Repository/BookmarkService.php:21
eZ\Publish\API\Repository\LocationService
Definition: API/Repository/LocationService.php:21
eZ\Publish\Core\REST\Common\Exceptions
Definition: Core/REST/Common/Exceptions/ForbiddenException.php:7
eZ\Publish\Core\REST\Server\Controller
Definition: BinaryContent.php:7
eZ\Publish\Core\REST\Server\Values
Definition: Core/REST/Server/Values/BookmarkList.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.