-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathBookmarkService.php
More file actions
148 lines (124 loc) · 4.54 KB
/
BookmarkService.php
File metadata and controls
148 lines (124 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace eZ\Publish\Core\Repository;
use Exception;
use eZ\Publish\API\Repository\BookmarkService as BookmarkServiceInterface;
use eZ\Publish\API\Repository\Exceptions\BadStateException;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\API\Repository\Values\Bookmark\BookmarkList;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Filter\Filter;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\SPI\Persistence\Bookmark\CreateStruct;
use eZ\Publish\SPI\Persistence\Bookmark\Handler as BookmarkHandler;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class BookmarkService implements BookmarkServiceInterface
{
/** @var \eZ\Publish\API\Repository\Repository */
protected $repository;
/** @var \eZ\Publish\SPI\Persistence\Bookmark\Handler */
protected $bookmarkHandler;
/** @var \Psr\Log\LoggerInterface */
private $logger;
/**
* BookmarkService constructor.
*/
public function __construct(RepositoryInterface $repository, BookmarkHandler $bookmarkHandler, LoggerInterface $logger = null)
{
$this->repository = $repository;
$this->bookmarkHandler = $bookmarkHandler;
$this->logger = $logger ?? new NullLogger();
}
/**
* {@inheritdoc}
*/
public function createBookmark(Location $location): void
{
$loadedLocation = $this->repository->getLocationService()->loadLocation($location->id);
if ($this->isBookmarked($loadedLocation)) {
throw new InvalidArgumentException('$location', 'Location is already bookmarked.');
}
$createStruct = new CreateStruct();
$createStruct->name = $loadedLocation->contentInfo->name;
$createStruct->locationId = $loadedLocation->id;
$createStruct->userId = $this->getCurrentUserId();
$this->repository->beginTransaction();
try {
$this->bookmarkHandler->create($createStruct);
$this->repository->commit();
} catch (Exception $ex) {
$this->repository->rollback();
throw $ex;
}
}
/**
* {@inheritdoc}
*/
public function deleteBookmark(Location $location): void
{
$loadedLocation = $this->repository->getLocationService()->loadLocation($location->id);
$bookmarks = $this->bookmarkHandler->loadByUserIdAndLocationId(
$this->getCurrentUserId(),
[$loadedLocation->id]
);
if (empty($bookmarks)) {
throw new InvalidArgumentException('$location', 'Location is not bookmarked.');
}
$this->repository->beginTransaction();
try {
$this->bookmarkHandler->delete(reset($bookmarks)->id);
$this->repository->commit();
} catch (Exception $ex) {
$this->repository->rollback();
throw $ex;
}
}
/**
* {@inheritdoc}
*/
public function loadBookmarks(int $offset = 0, int $limit = 25): BookmarkList
{
$currentUserId = $this->getCurrentUserId();
$filter = new Filter();
try {
$filter
->withCriterion(new Criterion\IsBookmarked($currentUserId))
->withSortClause(new SortClause\BookmarkId(Query::SORT_DESC))
->sliceBy($limit, $offset);
$result = $this->repository->getlocationService()->find($filter, []);
} catch (BadStateException $e) {
$this->logger->debug($e);
return new BookmarkList();
}
$list = new BookmarkList();
$list->totalCount = $result->totalCount;
$list->items = $result->locations;
return $list;
}
/**
* {@inheritdoc}
*/
public function isBookmarked(Location $location): bool
{
$bookmarks = $this->bookmarkHandler->loadByUserIdAndLocationId(
$this->getCurrentUserId(),
[$location->id]
);
return !empty($bookmarks);
}
private function getCurrentUserId(): int
{
return $this->repository
->getPermissionResolver()
->getCurrentUserReference()
->getUserId();
}
}