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

Core/Repository/URLService.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Repository;
8
9 use DateTime;
10 use Exception;
11 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12 use eZ\Publish\API\Repository\PermissionResolver;
13 use eZ\Publish\API\Repository\Repository as RepositoryInterface;
14 use eZ\Publish\API\Repository\URLService as URLServiceInterface;
15 use eZ\Publish\API\Repository\Values\Content\Query;
16 use eZ\Publish\API\Repository\Values\Content\Query\Criterion as ContentCriterion;
17 use eZ\Publish\API\Repository\Values\URL\SearchResult;
18 use eZ\Publish\API\Repository\Values\URL\URL;
19 use eZ\Publish\API\Repository\Values\URL\URLQuery;
20 use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct;
21 use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;
22 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
23 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue;
24 use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
25 use eZ\Publish\SPI\Persistence\URL\Handler as URLHandler;
26 use eZ\Publish\SPI\Persistence\URL\URL as SPIUrl;
27 use eZ\Publish\SPI\Persistence\URL\URLUpdateStruct as SPIUrlUpdateStruct;
28
29 class URLService implements URLServiceInterface
30 {
32 protected $repository;
33
35 protected $urlHandler;
36
38 private $permissionResolver;
39
45 public function __construct(
46 RepositoryInterface $repository,
47 URLHandler $urlHandler,
48 PermissionResolver $permissionResolver
49 ) {
50 $this->repository = $repository;
51 $this->urlHandler = $urlHandler;
52 $this->permissionResolver = $permissionResolver;
53 }
54
58 public function findUrls(URLQuery $query)
59 {
60 if ($this->repository->hasAccess('url', 'view') === false) {
61 throw new UnauthorizedException('url', 'view');
62 }
63
64 if ($query->offset !== null && !is_numeric($query->offset)) {
65 throw new InvalidArgumentValue('offset', $query->offset);
66 }
67
68 if ($query->limit !== null && !is_numeric($query->limit)) {
69 throw new InvalidArgumentValue('limit', $query->limit);
70 }
71
72 $results = $this->urlHandler->find($query);
73
74 $items = [];
75 foreach ($results['items'] as $url) {
76 $items[] = $this->buildDomainObject($url);
77 }
78
79 return new SearchResult([
80 'totalCount' => $results['count'],
81 'items' => $items,
82 ]);
83 }
84
88 public function updateUrl(URL $url, URLUpdateStruct $struct)
89 {
90 if (!$this->permissionResolver->canUser('url', 'update', $url)) {
91 throw new UnauthorizedException('url', 'update');
92 }
93
94 if (!$this->isUnique($url->id, $struct->url)) {
95 throw new InvalidArgumentException('struct', 'url already exists');
96 }
97
98 $updateStruct = $this->buildUpdateStruct($this->loadById($url->id), $struct);
99
100 $this->repository->beginTransaction();
101 try {
102 $this->urlHandler->updateUrl($url->id, $updateStruct);
103 $this->repository->commit();
104 } catch (Exception $e) {
105 $this->repository->rollback();
106 throw $e;
107 }
108
109 return $this->loadById($url->id);
110 }
111
115 public function loadById($id)
116 {
117 $url = $this->buildDomainObject(
118 $this->urlHandler->loadById($id)
119 );
120
121 if (!$this->permissionResolver->canUser('url', 'view', $url)) {
122 throw new UnauthorizedException('url', 'view');
123 }
124
125 return $url;
126 }
127
131 public function loadByUrl($url)
132 {
133 $apiUrl = $this->buildDomainObject(
134 $this->urlHandler->loadByUrl($url)
135 );
136
137 if (!$this->permissionResolver->canUser('url', 'view', $apiUrl)) {
138 throw new UnauthorizedException('url', 'view');
139 }
140
141 return $apiUrl;
142 }
143
147 public function createUpdateStruct()
148 {
149 return new URLUpdateStruct();
150 }
151
155 public function findUsages(URL $url, $offset = 0, $limit = -1)
156 {
157 $contentIds = $this->urlHandler->findUsages($url->id);
158 if (empty($contentIds)) {
159 return new UsageSearchResult();
160 }
161
162 $query = new Query();
163 $query->filter = new ContentCriterion\LogicalAnd([
164 new ContentCriterion\ContentId($contentIds),
165 new ContentCriterion\Visibility(ContentCriterion\Visibility::VISIBLE),
166 ]);
167
168 $query->offset = $offset;
169 if ($limit > -1) {
170 $query->limit = $limit;
171 }
172
173 $searchResults = $this->repository->getSearchService()->findContentInfo($query);
174
175 $usageResults = new UsageSearchResult();
176 $usageResults->totalCount = $searchResults->totalCount;
177 foreach ($searchResults->searchHits as $hit) {
178 $usageResults->items[] = $hit->valueObject;
179 }
180
181 return $usageResults;
182 }
183
191 protected function buildDomainObject(SPIUrl $data)
192 {
193 return new URL([
194 'id' => $data->id,
195 'url' => $data->url,
196 'isValid' => $data->isValid,
197 'lastChecked' => $this->createDateTime($data->lastChecked),
198 'created' => $this->createDateTime($data->created),
199 'modified' => $this->createDateTime($data->modified),
200 ]);
201 }
202
211 protected function buildUpdateStruct(URL $url, URLUpdateStruct $data)
212 {
213 $updateStruct = new SPIUrlUpdateStruct();
214
215 if ($data->url !== null && $url->url !== $data->url) {
216 $updateStruct->url = $data->url;
217 // Reset URL validity
218 $updateStruct->lastChecked = 0;
219 $updateStruct->isValid = true;
220 } else {
221 $updateStruct->url = $url->url;
222
223 if ($data->lastChecked !== null) {
224 $updateStruct->lastChecked = $data->lastChecked->getTimestamp();
225 } elseif ($data->lastChecked !== null) {
226 $updateStruct->lastChecked = $url->lastChecked->getTimestamp();
227 } else {
228 $updateStruct->lastChecked = 0;
229 }
230
231 if ($data->isValid !== null) {
232 $updateStruct->isValid = $data->isValid;
233 } else {
234 $updateStruct->isValid = $url->isValid;
235 }
236 }
237
238 return $updateStruct;
239 }
240
251 protected function isUnique($id, $url)
252 {
253 try {
254 return $this->loadByUrl($url)->id === $id;
255 } catch (NotFoundException $e) {
256 return true;
257 }
258 }
259
260 private function createDateTime($timestamp)
261 {
262 if ($timestamp > 0) {
263 return new DateTime("@{$timestamp}");
264 }
265
266 return null;
267 }
268 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\Content\Query\Criterion\ContentId
A criterion that matches content based on its id.
Definition: API/Repository/Values/Content/Query/Criterion/ContentId.php:20
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Visibility
A criterion that matches content based on its visibility.
Definition: API/Repository/Values/Content/Query/Criterion/Visibility.php:21
eZ\Publish\API\Repository\Values\Content\Query
This class is used to perform a Content query.
Definition: API/Repository/Values/Content/Query.php:15
eZ\Publish\API\Repository\Values\Content\Search\SearchResult
This class represents a search result.
Definition: Content/Search/SearchResult.php:15
eZ\Publish\API\Repository\Values\URL\SearchResult
Definition: URL/SearchResult.php:13
eZ\Publish\API\Repository\Values\URL\URLQuery
This class is used to perform a URL query.
Definition: URLQuery.php:15
eZ\Publish\API\Repository\Values\URL\URLUpdateStruct
Structure used to update URL data.
Definition: API/Repository/Values/URL/URLUpdateStruct.php:15
eZ\Publish\API\Repository\Values\URL\URL
Definition: API/Repository/Values/URL/URL.php:12
eZ\Publish\API\Repository\Values\URL\UsageSearchResult
Definition: UsageSearchResult.php:13
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue
Invalid Argument Type Exception implementation.
Definition: Base/Exceptions/InvalidArgumentValue.php:17
eZ\Publish\Core\Base\Exceptions\UnauthorizedException
UnauthorizedException Exception implementation.
Definition: Core/Base/Exceptions/UnauthorizedException.php:21
eZ\Publish\Core\Repository\URLService
Definition: Core/Repository/URLService.php:30
eZ\Publish\Core\Repository\URLService\__construct
__construct(RepositoryInterface $repository, URLHandler $urlHandler, PermissionResolver $permissionResolver)
Definition: Core/Repository/URLService.php:45
eZ\Publish\Core\Repository\URLService\isUnique
isUnique($id, $url)
Check if URL is unique.
Definition: Core/Repository/URLService.php:251
eZ\Publish\Core\Repository\URLService\findUrls
findUrls(URLQuery $query)
{Find URLs.}
Definition: Core/Repository/URLService.php:58
eZ\Publish\Core\Repository\URLService\buildDomainObject
buildDomainObject(SPIUrl $data)
Builds domain object from ValueObject returned by Persistence API.
Definition: Core/Repository/URLService.php:191
eZ\Publish\Core\Repository\URLService\createDateTime
createDateTime($timestamp)
Definition: Core/Repository/URLService.php:260
eZ\Publish\Core\Repository\URLService\createUpdateStruct
createUpdateStruct()
{Instantiates a new URL update struct.\eZ\Publish\API\Repository\Values\URL\URLUpdateStruct}
Definition: Core/Repository/URLService.php:147
eZ\Publish\Core\Repository\URLService\buildUpdateStruct
buildUpdateStruct(URL $url, URLUpdateStruct $data)
Builds SPI update structure used by Persistence API.
Definition: Core/Repository/URLService.php:211
eZ\Publish\Core\Repository\URLService\findUsages
findUsages(URL $url, $offset=0, $limit=-1)
{Find content objects using URL.Content is filter by user permissions.\eZ\Publish\API\Repository\Valu...
Definition: Core/Repository/URLService.php:155
eZ\Publish\Core\Repository\URLService\loadById
loadById($id)
{Load single URL (by ID).}
Definition: Core/Repository/URLService.php:115
eZ\Publish\Core\Repository\URLService\loadByUrl
loadByUrl($url)
{Load single URL (by URL).}
Definition: Core/Repository/URLService.php:131
eZ\Publish\Core\Repository\URLService\$repository
$repository
Definition: Core/Repository/URLService.php:32
eZ\Publish\Core\Repository\URLService\updateUrl
updateUrl(URL $url, URLUpdateStruct $struct)
{Updates URL.}
Definition: Core/Repository/URLService.php:88
eZ\Publish\Core\Repository\URLService\$urlHandler
$urlHandler
Definition: Core/Repository/URLService.php:35
eZ\Publish\Core\Repository\URLService\$permissionResolver
$permissionResolver
\eZ\Publish\API\Repository\PermissionResolver
Definition: Core/Repository/URLService.php:38
eZ\Publish\SPI\Persistence\URL\URLUpdateStruct
Definition: SPI/Persistence/URL/URLUpdateStruct.php:12
eZ\Publish\SPI\Persistence\URL\URL
Definition: SPI/Persistence/URL/URL.php:12
eZ\Publish\API\Repository\PermissionResolver
This service provides methods for resolving permissions.
Definition: API/Repository/PermissionResolver.php:17
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\API\Repository\URLService
URL Service.
Definition: API/Repository/URLService.php:17
eZ\Publish\SPI\Persistence\URL\Handler
The URL Handler interface defines operations on URLs in the storage engine.
Definition: SPI/Persistence/URL/Handler.php:15
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion/Ancestor.php:7
eZ\Publish\Core\Repository
Definition: Core/Repository/BookmarkService.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.