Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LocationService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LocationService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class LocationService implements LocationServiceInterface |
||
| 47 | { |
||
| 48 | /** @var \eZ\Publish\Core\Repository\Repository */ |
||
| 49 | protected $repository; |
||
| 50 | |||
| 51 | /** @var \eZ\Publish\SPI\Persistence\Handler */ |
||
| 52 | protected $persistenceHandler; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | protected $settings; |
||
| 56 | |||
| 57 | /** @var \eZ\Publish\Core\Repository\Helper\DomainMapper */ |
||
| 58 | protected $domainMapper; |
||
| 59 | |||
| 60 | /** @var \eZ\Publish\Core\Repository\Helper\NameSchemaService */ |
||
| 61 | protected $nameSchemaService; |
||
| 62 | |||
| 63 | /** @var \eZ\Publish\API\Repository\PermissionCriterionResolver */ |
||
| 64 | protected $permissionCriterionResolver; |
||
| 65 | |||
| 66 | /** @var \Psr\Log\LoggerInterface */ |
||
| 67 | private $logger; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 71 | * |
||
| 72 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 73 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 74 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
| 75 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 76 | * @param \eZ\Publish\API\Repository\PermissionCriterionResolver $permissionCriterionResolver |
||
| 77 | * @param array $settings |
||
| 78 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 79 | */ |
||
| 80 | public function __construct( |
||
| 81 | RepositoryInterface $repository, |
||
| 82 | Handler $handler, |
||
| 83 | Helper\DomainMapper $domainMapper, |
||
| 84 | Helper\NameSchemaService $nameSchemaService, |
||
| 85 | PermissionCriterionResolver $permissionCriterionResolver, |
||
| 86 | array $settings = [], |
||
| 87 | LoggerInterface $logger = null |
||
| 88 | ) { |
||
| 89 | $this->repository = $repository; |
||
|
|
|||
| 90 | $this->persistenceHandler = $handler; |
||
| 91 | $this->domainMapper = $domainMapper; |
||
| 92 | $this->nameSchemaService = $nameSchemaService; |
||
| 93 | // Union makes sure default settings are ignored if provided in argument |
||
| 94 | $this->settings = $settings + [ |
||
| 95 | //'defaultSetting' => array(), |
||
| 96 | ]; |
||
| 97 | $this->permissionCriterionResolver = $permissionCriterionResolver; |
||
| 98 | $this->logger = null !== $logger ? $logger : new NullLogger(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Copies the subtree starting from $subtree as a new subtree of $targetLocation. |
||
| 103 | * |
||
| 104 | * Only the items on which the user has read access are copied. |
||
| 105 | * |
||
| 106 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location |
||
| 107 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
||
| 108 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location |
||
| 109 | * |
||
| 110 | * @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy |
||
| 111 | * @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation |
||
| 112 | * |
||
| 113 | * @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree |
||
| 114 | */ |
||
| 115 | public function copySubtree(APILocation $subtree, APILocation $targetParentLocation) |
||
| 116 | { |
||
| 117 | $loadedSubtree = $this->loadLocation($subtree->id); |
||
| 118 | $loadedTargetLocation = $this->loadLocation($targetParentLocation->id); |
||
| 119 | |||
| 120 | if (stripos($loadedTargetLocation->pathString, $loadedSubtree->pathString) !== false) { |
||
| 121 | throw new InvalidArgumentException('targetParentLocation', 'target parent location is a sub location of the given subtree'); |
||
| 122 | } |
||
| 123 | |||
| 124 | // check create permission on target |
||
| 125 | if (!$this->repository->canUser('content', 'create', $loadedSubtree->getContentInfo(), $loadedTargetLocation)) { |
||
| 126 | throw new UnauthorizedException('content', 'create', ['locationId' => $loadedTargetLocation->id]); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Check read access to whole source subtree |
||
| 130 | $contentReadCriterion = $this->permissionCriterionResolver->getPermissionsCriterion('content', 'read'); |
||
| 131 | View Code Duplication | if ($contentReadCriterion === false) { |
|
| 132 | throw new UnauthorizedException('content', 'read'); |
||
| 133 | } elseif ($contentReadCriterion !== true) { |
||
| 134 | // Query if there are any content in subtree current user don't have access to |
||
| 135 | $query = new Query( |
||
| 136 | [ |
||
| 137 | 'limit' => 0, |
||
| 138 | 'filter' => new CriterionLogicalAnd( |
||
| 139 | [ |
||
| 140 | new CriterionSubtree($loadedSubtree->pathString), |
||
| 141 | new CriterionLogicalNot($contentReadCriterion), |
||
| 142 | ] |
||
| 143 | ), |
||
| 144 | ] |
||
| 145 | ); |
||
| 146 | $result = $this->repository->getSearchService()->findContent($query, [], false); |
||
| 147 | if ($result->totalCount > 0) { |
||
| 148 | throw new UnauthorizedException('content', 'read'); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->repository->beginTransaction(); |
||
| 153 | try { |
||
| 154 | $newLocation = $this->persistenceHandler->locationHandler()->copySubtree( |
||
| 155 | $loadedSubtree->id, |
||
| 156 | $loadedTargetLocation->id, |
||
| 157 | $this->repository->getPermissionResolver()->getCurrentUserReference()->getUserId() |
||
| 158 | ); |
||
| 159 | |||
| 160 | $content = $this->repository->getContentService()->loadContent($newLocation->contentId); |
||
| 161 | $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
||
| 162 | View Code Duplication | foreach ($urlAliasNames as $languageCode => $name) { |
|
| 163 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
| 164 | $newLocation->id, |
||
| 165 | $loadedTargetLocation->id, |
||
| 166 | $name, |
||
| 167 | $languageCode, |
||
| 168 | $content->contentInfo->alwaysAvailable |
||
| 169 | ); |
||
| 170 | } |
||
| 171 | |||
| 172 | $this->persistenceHandler->urlAliasHandler()->locationCopied( |
||
| 173 | $loadedSubtree->id, |
||
| 174 | $newLocation->id, |
||
| 175 | $loadedTargetLocation->id |
||
| 176 | ); |
||
| 177 | |||
| 178 | $this->repository->commit(); |
||
| 179 | } catch (Exception $e) { |
||
| 180 | $this->repository->rollback(); |
||
| 181 | throw $e; |
||
| 182 | } |
||
| 183 | |||
| 184 | return $this->domainMapper->buildLocationWithContent($newLocation, $content); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | public function loadLocation($locationId, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null) |
||
| 191 | { |
||
| 192 | $spiLocation = $this->persistenceHandler->locationHandler()->load($locationId, $prioritizedLanguages, $useAlwaysAvailable ?? true); |
||
| 193 | $location = $this->domainMapper->buildLocation($spiLocation, $prioritizedLanguages ?: [], $useAlwaysAvailable ?? true); |
||
| 194 | if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
||
| 195 | throw new UnauthorizedException('content', 'read', ['locationId' => $location->id]); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $location; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public function loadLocationList(array $locationIds, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null): iterable |
||
| 205 | { |
||
| 206 | $spiLocations = $this->persistenceHandler->locationHandler()->loadList( |
||
| 207 | $locationIds, |
||
| 208 | $prioritizedLanguages, |
||
| 209 | $useAlwaysAvailable ?? true |
||
| 210 | ); |
||
| 211 | if (empty($spiLocations)) { |
||
| 212 | return []; |
||
| 213 | } |
||
| 214 | |||
| 215 | // Get content id's |
||
| 216 | $contentIds = []; |
||
| 217 | foreach ($spiLocations as $spiLocation) { |
||
| 218 | $contentIds[] = $spiLocation->contentId; |
||
| 219 | } |
||
| 220 | |||
| 221 | // Load content info and Get content proxy |
||
| 222 | $spiContentInfoList = $this->persistenceHandler->contentHandler()->loadContentInfoList($contentIds); |
||
| 223 | $contentProxyList = $this->domainMapper->buildContentProxyList( |
||
| 224 | $spiContentInfoList, |
||
| 225 | $prioritizedLanguages ?? [], |
||
| 226 | $useAlwaysAvailable ?? true |
||
| 227 | ); |
||
| 228 | |||
| 229 | // Build locations using the bulk retrieved content info and bulk lazy loaded content proxies. |
||
| 230 | $locations = []; |
||
| 231 | $permissionResolver = $this->repository->getPermissionResolver(); |
||
| 232 | foreach ($spiLocations as $spiLocation) { |
||
| 233 | $location = $this->domainMapper->buildLocationWithContent( |
||
| 234 | $spiLocation, |
||
| 235 | $contentProxyList[$spiLocation->contentId] ?? null, |
||
| 236 | $spiContentInfoList[$spiLocation->contentId] ?? null |
||
| 237 | ); |
||
| 238 | |||
| 239 | if ($permissionResolver->canUser('content', 'read', $location->getContentInfo(), [$location])) { |
||
| 240 | $locations[$spiLocation->id] = $location; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return $locations; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | public function loadLocationByRemoteId($remoteId, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null) |
||
| 251 | { |
||
| 252 | if (!is_string($remoteId)) { |
||
| 253 | throw new InvalidArgumentValue('remoteId', $remoteId); |
||
| 254 | } |
||
| 255 | |||
| 256 | $spiLocation = $this->persistenceHandler->locationHandler()->loadByRemoteId($remoteId, $prioritizedLanguages, $useAlwaysAvailable ?? true); |
||
| 257 | $location = $this->domainMapper->buildLocation($spiLocation, $prioritizedLanguages ?: [], $useAlwaysAvailable ?? true); |
||
| 258 | if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
||
| 259 | throw new UnauthorizedException('content', 'read', ['locationId' => $location->id]); |
||
| 260 | } |
||
| 261 | |||
| 262 | return $location; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function loadLocations(ContentInfo $contentInfo, APILocation $rootLocation = null, array $prioritizedLanguages = null) |
||
| 269 | { |
||
| 270 | if (!$contentInfo->published) { |
||
| 271 | throw new BadStateException('$contentInfo', 'ContentInfo has no published versions'); |
||
| 272 | } |
||
| 273 | |||
| 274 | $spiLocations = $this->persistenceHandler->locationHandler()->loadLocationsByContent( |
||
| 275 | $contentInfo->id, |
||
| 276 | $rootLocation !== null ? $rootLocation->id : null |
||
| 277 | ); |
||
| 278 | |||
| 279 | $locations = []; |
||
| 280 | $spiInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($contentInfo->id); |
||
| 281 | $content = $this->domainMapper->buildContentProxy($spiInfo, $prioritizedLanguages ?: []); |
||
| 282 | foreach ($spiLocations as $spiLocation) { |
||
| 283 | $location = $this->domainMapper->buildLocationWithContent($spiLocation, $content, $spiInfo); |
||
| 284 | if ($this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
||
| 285 | $locations[] = $location; |
||
| 286 | } |
||
| 287 | } |
||
| 288 | |||
| 289 | return $locations; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function loadLocationChildren(APILocation $location, $offset = 0, $limit = 25, array $prioritizedLanguages = null) |
||
| 296 | { |
||
| 297 | if (!$this->domainMapper->isValidLocationSortField($location->sortField)) { |
||
| 298 | throw new InvalidArgumentValue('sortField', $location->sortField, 'Location'); |
||
| 299 | } |
||
| 300 | |||
| 301 | if (!$this->domainMapper->isValidLocationSortOrder($location->sortOrder)) { |
||
| 302 | throw new InvalidArgumentValue('sortOrder', $location->sortOrder, 'Location'); |
||
| 303 | } |
||
| 304 | |||
| 305 | if (!is_int($offset)) { |
||
| 306 | throw new InvalidArgumentValue('offset', $offset); |
||
| 307 | } |
||
| 308 | |||
| 309 | if (!is_int($limit)) { |
||
| 310 | throw new InvalidArgumentValue('limit', $limit); |
||
| 311 | } |
||
| 312 | |||
| 313 | $childLocations = []; |
||
| 314 | $searchResult = $this->searchChildrenLocations($location, $offset, $limit, $prioritizedLanguages ?: []); |
||
| 315 | foreach ($searchResult->searchHits as $searchHit) { |
||
| 316 | $childLocations[] = $searchHit->valueObject; |
||
| 317 | } |
||
| 318 | |||
| 319 | return new LocationList( |
||
| 320 | [ |
||
| 321 | 'locations' => $childLocations, |
||
| 322 | 'totalCount' => $searchResult->totalCount, |
||
| 323 | ] |
||
| 324 | ); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | public function loadParentLocationsForDraftContent(VersionInfo $versionInfo, array $prioritizedLanguages = null) |
||
| 331 | { |
||
| 332 | if (!$versionInfo->isDraft()) { |
||
| 333 | throw new BadStateException( |
||
| 334 | '$contentInfo', |
||
| 335 | sprintf( |
||
| 336 | 'Content [%d] %s has been already published. Use LocationService::loadLocations instead.', |
||
| 337 | $versionInfo->contentInfo->id, |
||
| 338 | $versionInfo->contentInfo->name |
||
| 339 | ) |
||
| 340 | ); |
||
| 341 | } |
||
| 342 | |||
| 343 | $spiLocations = $this->persistenceHandler |
||
| 344 | ->locationHandler() |
||
| 345 | ->loadParentLocationsForDraftContent($versionInfo->contentInfo->id); |
||
| 346 | |||
| 347 | $contentIds = []; |
||
| 348 | foreach ($spiLocations as $spiLocation) { |
||
| 349 | $contentIds[] = $spiLocation->contentId; |
||
| 350 | } |
||
| 351 | |||
| 352 | $locations = []; |
||
| 353 | $permissionResolver = $this->repository->getPermissionResolver(); |
||
| 354 | $spiContentInfoList = $this->persistenceHandler->contentHandler()->loadContentInfoList($contentIds); |
||
| 355 | $contentList = $this->domainMapper->buildContentProxyList($spiContentInfoList, $prioritizedLanguages ?: []); |
||
| 356 | foreach ($spiLocations as $spiLocation) { |
||
| 357 | $location = $this->domainMapper->buildLocationWithContent( |
||
| 358 | $spiLocation, |
||
| 359 | $contentList[$spiLocation->contentId], |
||
| 360 | $spiContentInfoList[$spiLocation->contentId] |
||
| 361 | ); |
||
| 362 | |||
| 363 | if ($permissionResolver->canUser('content', 'read', $location->getContentInfo(), [$location])) { |
||
| 364 | $locations[] = $location; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | return $locations; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the number of children which are readable by the current user of a location object. |
||
| 373 | * |
||
| 374 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 375 | * |
||
| 376 | * @return int |
||
| 377 | */ |
||
| 378 | public function getLocationChildCount(APILocation $location) |
||
| 379 | { |
||
| 380 | $searchResult = $this->searchChildrenLocations($location, 0, 0); |
||
| 381 | |||
| 382 | return $searchResult->totalCount; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Searches children locations of the provided parent location id. |
||
| 387 | * |
||
| 388 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 389 | * @param int $offset |
||
| 390 | * @param int $limit |
||
| 391 | * |
||
| 392 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 393 | */ |
||
| 394 | protected function searchChildrenLocations(APILocation $location, $offset = 0, $limit = -1, array $prioritizedLanguages = null) |
||
| 395 | { |
||
| 396 | $query = new LocationQuery([ |
||
| 397 | 'filter' => new Criterion\ParentLocationId($location->id), |
||
| 398 | 'offset' => $offset >= 0 ? (int)$offset : 0, |
||
| 399 | 'limit' => $limit >= 0 ? (int)$limit : null, |
||
| 400 | 'sortClauses' => $location->getSortClauses(), |
||
| 401 | ]); |
||
| 402 | |||
| 403 | return $this->repository->getSearchService()->findLocations($query, ['languages' => $prioritizedLanguages]); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Creates the new $location in the content repository for the given content. |
||
| 408 | * |
||
| 409 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location |
||
| 410 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent |
||
| 411 | * or the parent is a sub location of the location of the content |
||
| 412 | * or if set the remoteId exists already |
||
| 413 | * |
||
| 414 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 415 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
| 416 | * |
||
| 417 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location |
||
| 418 | */ |
||
| 419 | public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) |
||
| 420 | { |
||
| 421 | $content = $this->domainMapper->buildContentDomainObjectFromPersistence( |
||
| 422 | $this->persistenceHandler->contentHandler()->load($contentInfo->id), |
||
| 423 | $this->persistenceHandler->contentTypeHandler()->load($contentInfo->contentTypeId) |
||
| 424 | ); |
||
| 425 | |||
| 426 | $parentLocation = $this->domainMapper->buildLocation( |
||
| 427 | $this->persistenceHandler->locationHandler()->load($locationCreateStruct->parentLocationId) |
||
| 428 | ); |
||
| 429 | |||
| 430 | $contentType = $content->getContentType(); |
||
| 431 | |||
| 432 | $locationCreateStruct->sortField = $locationCreateStruct->sortField |
||
| 433 | ?? ($contentType->defaultSortField ?? Location::SORT_FIELD_NAME); |
||
| 434 | $locationCreateStruct->sortOrder = $locationCreateStruct->sortOrder |
||
| 435 | ?? ($contentType->defaultSortOrder ?? Location::SORT_ORDER_ASC); |
||
| 436 | |||
| 437 | $contentInfo = $content->contentInfo; |
||
| 438 | |||
| 439 | if (!$this->repository->canUser('content', 'manage_locations', $contentInfo, $parentLocation)) { |
||
| 440 | throw new UnauthorizedException('content', 'manage_locations', ['contentId' => $contentInfo->id]); |
||
| 441 | } |
||
| 442 | |||
| 443 | if (!$this->repository->canUser('content', 'create', $contentInfo, $parentLocation)) { |
||
| 444 | throw new UnauthorizedException('content', 'create', ['locationId' => $parentLocation->id]); |
||
| 445 | } |
||
| 446 | |||
| 447 | // Check if the parent is a sub location of one of the existing content locations (this also solves the |
||
| 448 | // situation where parent location actually one of the content locations), |
||
| 449 | // or if the content already has location below given location create struct parent |
||
| 450 | $existingContentLocations = $this->loadLocations($contentInfo); |
||
| 451 | if (!empty($existingContentLocations)) { |
||
| 452 | foreach ($existingContentLocations as $existingContentLocation) { |
||
| 453 | if (stripos($parentLocation->pathString, $existingContentLocation->pathString) !== false) { |
||
| 454 | throw new InvalidArgumentException( |
||
| 455 | '$locationCreateStruct', |
||
| 456 | 'Specified parent is a sub location of one of the existing content locations.' |
||
| 457 | ); |
||
| 458 | } |
||
| 459 | if ($parentLocation->id == $existingContentLocation->parentLocationId) { |
||
| 460 | throw new InvalidArgumentException( |
||
| 461 | '$locationCreateStruct', |
||
| 462 | 'Content is already below the specified parent.' |
||
| 463 | ); |
||
| 464 | } |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | $spiLocationCreateStruct = $this->domainMapper->buildSPILocationCreateStruct( |
||
| 469 | $locationCreateStruct, |
||
| 470 | $parentLocation, |
||
| 471 | $contentInfo->mainLocationId ?? true, |
||
| 472 | $contentInfo->id, |
||
| 473 | $contentInfo->currentVersionNo |
||
| 474 | ); |
||
| 475 | |||
| 476 | $this->repository->beginTransaction(); |
||
| 477 | try { |
||
| 478 | $newLocation = $this->persistenceHandler->locationHandler()->create($spiLocationCreateStruct); |
||
| 479 | $urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
||
| 480 | foreach ($urlAliasNames as $languageCode => $name) { |
||
| 481 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
| 482 | $newLocation->id, |
||
| 483 | $newLocation->parentId, |
||
| 484 | $name, |
||
| 485 | $languageCode, |
||
| 486 | $contentInfo->alwaysAvailable, |
||
| 487 | // @todo: this is legacy storage specific for updating ezcontentobject_tree.path_identification_string, to be removed |
||
| 488 | $languageCode === $contentInfo->mainLanguageCode |
||
| 489 | ); |
||
| 490 | } |
||
| 491 | |||
| 492 | $this->repository->commit(); |
||
| 493 | } catch (Exception $e) { |
||
| 494 | $this->repository->rollback(); |
||
| 495 | throw $e; |
||
| 496 | } |
||
| 497 | |||
| 498 | return $this->domainMapper->buildLocationWithContent($newLocation, $content); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Updates $location in the content repository. |
||
| 503 | * |
||
| 504 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location |
||
| 505 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already |
||
| 506 | * |
||
| 507 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 508 | * @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct |
||
| 509 | * |
||
| 510 | * @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location |
||
| 511 | */ |
||
| 512 | public function updateLocation(APILocation $location, LocationUpdateStruct $locationUpdateStruct) |
||
| 513 | { |
||
| 514 | if (!$this->domainMapper->isValidLocationPriority($locationUpdateStruct->priority)) { |
||
| 515 | throw new InvalidArgumentValue('priority', $locationUpdateStruct->priority, 'LocationUpdateStruct'); |
||
| 516 | } |
||
| 517 | |||
| 518 | if ($locationUpdateStruct->remoteId !== null && (!is_string($locationUpdateStruct->remoteId) || empty($locationUpdateStruct->remoteId))) { |
||
| 519 | throw new InvalidArgumentValue('remoteId', $locationUpdateStruct->remoteId, 'LocationUpdateStruct'); |
||
| 520 | } |
||
| 521 | |||
| 522 | if ($locationUpdateStruct->sortField !== null && !$this->domainMapper->isValidLocationSortField($locationUpdateStruct->sortField)) { |
||
| 523 | throw new InvalidArgumentValue('sortField', $locationUpdateStruct->sortField, 'LocationUpdateStruct'); |
||
| 524 | } |
||
| 525 | |||
| 526 | if ($locationUpdateStruct->sortOrder !== null && !$this->domainMapper->isValidLocationSortOrder($locationUpdateStruct->sortOrder)) { |
||
| 527 | throw new InvalidArgumentValue('sortOrder', $locationUpdateStruct->sortOrder, 'LocationUpdateStruct'); |
||
| 528 | } |
||
| 529 | |||
| 530 | $loadedLocation = $this->loadLocation($location->id); |
||
| 531 | |||
| 532 | if ($locationUpdateStruct->remoteId !== null) { |
||
| 533 | try { |
||
| 534 | $existingLocation = $this->loadLocationByRemoteId($locationUpdateStruct->remoteId); |
||
| 535 | if ($existingLocation !== null && $existingLocation->id !== $loadedLocation->id) { |
||
| 536 | throw new InvalidArgumentException('locationUpdateStruct', 'location with provided remote ID already exists'); |
||
| 537 | } |
||
| 538 | } catch (APINotFoundException $e) { |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | if (!$this->repository->canUser('content', 'edit', $loadedLocation->getContentInfo(), $loadedLocation)) { |
||
| 543 | throw new UnauthorizedException('content', 'edit', ['locationId' => $loadedLocation->id]); |
||
| 544 | } |
||
| 545 | |||
| 546 | $updateStruct = new UpdateStruct(); |
||
| 547 | $updateStruct->priority = $locationUpdateStruct->priority !== null ? $locationUpdateStruct->priority : $loadedLocation->priority; |
||
| 548 | $updateStruct->remoteId = $locationUpdateStruct->remoteId !== null ? trim($locationUpdateStruct->remoteId) : $loadedLocation->remoteId; |
||
| 549 | $updateStruct->sortField = $locationUpdateStruct->sortField !== null ? $locationUpdateStruct->sortField : $loadedLocation->sortField; |
||
| 550 | $updateStruct->sortOrder = $locationUpdateStruct->sortOrder !== null ? $locationUpdateStruct->sortOrder : $loadedLocation->sortOrder; |
||
| 551 | |||
| 552 | $this->repository->beginTransaction(); |
||
| 553 | try { |
||
| 554 | $this->persistenceHandler->locationHandler()->update($updateStruct, $loadedLocation->id); |
||
| 555 | $this->repository->commit(); |
||
| 556 | } catch (Exception $e) { |
||
| 557 | $this->repository->rollback(); |
||
| 558 | throw $e; |
||
| 559 | } |
||
| 560 | |||
| 561 | return $this->loadLocation($loadedLocation->id); |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Swaps the contents held by $location1 and $location2. |
||
| 566 | * |
||
| 567 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content |
||
| 568 | * |
||
| 569 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location1 |
||
| 570 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location2 |
||
| 571 | */ |
||
| 572 | public function swapLocation(APILocation $location1, APILocation $location2) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Hides the $location and marks invisible all descendants of $location. |
||
| 603 | * |
||
| 604 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location |
||
| 605 | * |
||
| 606 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 607 | * |
||
| 608 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
| 609 | */ |
||
| 610 | View Code Duplication | public function hideLocation(APILocation $location) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Unhides the $location. |
||
| 630 | * |
||
| 631 | * This method and marks visible all descendants of $locations |
||
| 632 | * until a hidden location is found. |
||
| 633 | * |
||
| 634 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location |
||
| 635 | * |
||
| 636 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 637 | * |
||
| 638 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
| 639 | */ |
||
| 640 | View Code Duplication | public function unhideLocation(APILocation $location) |
|
| 657 | |||
| 658 | /** |
||
| 659 | * Moves the subtree to $newParentLocation. |
||
| 660 | * |
||
| 661 | * If a user has the permission to move the location to a target location |
||
| 662 | * he can do it regardless of an existing descendant on which the user has no permission. |
||
| 663 | * |
||
| 664 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target |
||
| 665 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
||
| 666 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new parent is in a subtree of the location |
||
| 667 | * |
||
| 668 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 669 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 670 | */ |
||
| 671 | public function moveSubtree(APILocation $location, APILocation $newParentLocation) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Deletes $location and all its descendants. |
||
| 744 | * |
||
| 745 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant |
||
| 746 | * |
||
| 747 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 748 | */ |
||
| 749 | public function deleteLocation(APILocation $location) |
||
| 750 | { |
||
| 751 | $location = $this->loadLocation($location->id); |
||
| 752 | |||
| 753 | if (!$this->repository->canUser('content', 'manage_locations', $location->getContentInfo())) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Instantiates a new location create class. |
||
| 796 | * |
||
| 797 | * @param mixed $parentLocationId the parent under which the new location should be created |
||
| 798 | * @param eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType |
||
| 799 | * |
||
| 800 | * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct |
||
| 801 | */ |
||
| 802 | View Code Duplication | public function newLocationCreateStruct($parentLocationId, ContentType $contentType = null) |
|
| 814 | |||
| 815 | /** |
||
| 816 | * Instantiates a new location update class. |
||
| 817 | * |
||
| 818 | * @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct |
||
| 819 | */ |
||
| 820 | public function newLocationUpdateStruct() |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Get the total number of all existing Locations. Can be combined with loadAllLocations. |
||
| 827 | * |
||
| 828 | * @see loadAllLocations |
||
| 829 | * |
||
| 830 | * @return int Total number of Locations |
||
| 831 | */ |
||
| 832 | public function getAllLocationsCount(): int |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Bulk-load all existing Locations, constrained by $limit and $offset to paginate results. |
||
| 839 | * |
||
| 840 | * @param int $offset |
||
| 841 | * @param int $limit |
||
| 842 | * |
||
| 843 | * @return \eZ\Publish\API\Repository\Values\Content\Location[] |
||
| 844 | * |
||
| 845 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
| 846 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 847 | */ |
||
| 848 | public function loadAllLocations(int $offset = 0, int $limit = 25): array |
||
| 900 | } |
||
| 901 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.