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\Repository; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use eZ\Publish\API\Repository\BookmarkService as BookmarkServiceInterface; |
13
|
|
|
use eZ\Publish\API\Repository\Repository as RepositoryInterface; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Bookmark\BookmarkList; |
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
16
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
17
|
|
|
use eZ\Publish\SPI\Persistence\Bookmark\Bookmark; |
18
|
|
|
use eZ\Publish\SPI\Persistence\Bookmark\CreateStruct; |
19
|
|
|
use eZ\Publish\SPI\Persistence\Bookmark\Handler as BookmarkHandler; |
20
|
|
|
|
21
|
|
|
class BookmarkService implements BookmarkServiceInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
25
|
|
|
*/ |
26
|
|
|
protected $repository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \eZ\Publish\SPI\Persistence\Bookmark\Handler |
30
|
|
|
*/ |
31
|
|
|
protected $bookmarkHandler; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* BookmarkService constructor. |
35
|
|
|
* |
36
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
37
|
|
|
* @param \eZ\Publish\SPI\Persistence\Bookmark\Handler $bookmarkHandler |
38
|
|
|
*/ |
39
|
|
|
public function __construct(RepositoryInterface $repository, BookmarkHandler $bookmarkHandler) |
40
|
|
|
{ |
41
|
|
|
$this->repository = $repository; |
42
|
|
|
$this->bookmarkHandler = $bookmarkHandler; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function createBookmark(Location $location): void |
49
|
|
|
{ |
50
|
|
|
$loadedLocation = $this->repository->getLocationService()->loadLocation($location->id); |
51
|
|
|
|
52
|
|
|
if ($this->isBookmarked($loadedLocation)) { |
53
|
|
|
throw new InvalidArgumentException('$location', 'location is already bookmarked.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$createStruct = new CreateStruct(); |
57
|
|
|
$createStruct->name = $loadedLocation->contentInfo->name; |
|
|
|
|
58
|
|
|
$createStruct->locationId = $loadedLocation->id; |
59
|
|
|
$createStruct->userId = $this->getCurrentUserId(); |
60
|
|
|
|
61
|
|
|
$this->repository->beginTransaction(); |
62
|
|
|
try { |
63
|
|
|
$this->bookmarkHandler->create($createStruct); |
64
|
|
|
$this->repository->commit(); |
65
|
|
|
} catch (Exception $ex) { |
66
|
|
|
$this->repository->rollback(); |
67
|
|
|
throw $ex; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function deleteBookmark(Location $location): void |
75
|
|
|
{ |
76
|
|
|
$loadedLocation = $this->repository->getLocationService()->loadLocation($location->id); |
77
|
|
|
|
78
|
|
|
$bookmarks = $this->bookmarkHandler->loadByUserIdAndLocationId( |
79
|
|
|
$this->getCurrentUserId(), [$loadedLocation->id] |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
if (empty($bookmarks)) { |
83
|
|
|
throw new InvalidArgumentException('$location', 'location is not bookmarked.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->repository->beginTransaction(); |
87
|
|
|
try { |
88
|
|
|
$this->bookmarkHandler->delete(reset($bookmarks)->id); |
89
|
|
|
$this->repository->commit(); |
90
|
|
|
} catch (Exception $ex) { |
91
|
|
|
$this->repository->rollback(); |
92
|
|
|
throw $ex; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function loadBookmarks(int $offset = 0, int $limit = 25): BookmarkList |
100
|
|
|
{ |
101
|
|
|
$currentUserId = $this->getCurrentUserId(); |
102
|
|
|
|
103
|
|
|
$list = new BookmarkList(); |
104
|
|
|
$list->totalCount = $this->bookmarkHandler->countUserBookmarks($currentUserId); |
105
|
|
|
if ($list->totalCount > 0) { |
106
|
|
|
$bookmarks = $this->bookmarkHandler->loadUserBookmarks($currentUserId, $offset, $limit); |
107
|
|
|
|
108
|
|
|
$list->items = array_map(function (Bookmark $bookmark) { |
109
|
|
|
return $this->repository->getLocationService()->loadLocation($bookmark->locationId); |
110
|
|
|
}, $bookmarks); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $list; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function isBookmarked(Location $location): bool |
120
|
|
|
{ |
121
|
|
|
$bookmarks = $this->bookmarkHandler->loadByUserIdAndLocationId( |
122
|
|
|
$this->getCurrentUserId(), [$location->id] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return !empty($bookmarks); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function getCurrentUserId(): int |
129
|
|
|
{ |
130
|
|
|
return $this->repository |
131
|
|
|
->getPermissionResolver() |
132
|
|
|
->getCurrentUserReference() |
133
|
|
|
->getUserId(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.