Skip to content

Commit

Permalink
EZP-31093: Introduced strict types for TrashService (#2851)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs authored Nov 7, 2019
1 parent 36bc4b2 commit 0685d5f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 61 deletions.
37 changes: 20 additions & 17 deletions eZ/Publish/API/Repository/TrashService.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php

/**
* File containing the eZ\Publish\API\Repository\TrashService class.
*
* @copyright Copyright (C) eZ Systems 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\API\Repository;

use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\Trash\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList;
use eZ\Publish\API\Repository\Values\Content\TrashItem;
use eZ\Publish\API\Repository\Values\Content\Query;

Expand All @@ -22,42 +25,42 @@ interface TrashService
*
* Note that $id is identical to original location, which has been previously trashed
*
* @param int $trashItemId
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist
*
* @param mixed $trashItemId
*
* @return \eZ\Publish\API\Repository\Values\Content\TrashItem
*/
public function loadTrashItem($trashItemId);
public function loadTrashItem(int $trashItemId): TrashItem;

/**
* Sends $location and all its children to trash and returns the corresponding trash item.
*
* The current user may not have access to the returned trash item, check before using it.
* Content is left untouched.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location
*
* @return \eZ\Publish\API\Repository\Values\Content\TrashItem|null null if location was deleted, otherwise TrashItem
*/
public function trash(Location $location);
public function trash(Location $location): ?TrashItem;

/**
* Recovers the $trashedLocation at its original place if possible.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
*
* If $newParentLocation is provided, $trashedLocation will be restored under it.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
*
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
*
* @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location
*/
public function recover(TrashItem $trashItem, Location $newParentLocation = null);
public function recover(TrashItem $trashItem, Location $newParentLocation = null): Location;

/**
* Empties trash.
Expand All @@ -69,20 +72,20 @@ public function recover(TrashItem $trashItem, Location $newParentLocation = null
*
* @return \eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList
*/
public function emptyTrash();
public function emptyTrash(): TrashItemDeleteResultList;

/**
* Deletes a trash item.
*
* The corresponding content object will be removed
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item
*
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item
*
* @return \eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult
*/
public function deleteTrashItem(TrashItem $trashItem);
public function deleteTrashItem(TrashItem $trashItem): TrashItemDeleteResult;

/**
* Returns a collection of Trashed locations contained in the trash, which are readable by the current user.
Expand All @@ -93,5 +96,5 @@ public function deleteTrashItem(TrashItem $trashItem);
*
* @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult
*/
public function findTrashItems(Query $query);
public function findTrashItems(Query $query): SearchResult;
}
10 changes: 6 additions & 4 deletions eZ/Publish/Core/Event/TrashService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use eZ\Publish\API\Repository\TrashService as TrashServiceInterface;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\TrashItem;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList;
use eZ\Publish\API\Repository\Events\Trash\BeforeDeleteTrashItemEvent;
use eZ\Publish\API\Repository\Events\Trash\BeforeEmptyTrashEvent;
use eZ\Publish\API\Repository\Events\Trash\BeforeRecoverEvent;
Expand All @@ -36,7 +38,7 @@ public function __construct(
$this->eventDispatcher = $eventDispatcher;
}

public function trash(Location $location)
public function trash(Location $location): ?TrashItem
{
$eventData = [$location];

Expand All @@ -61,7 +63,7 @@ public function trash(Location $location)
public function recover(
TrashItem $trashItem,
?Location $newParentLocation = null
) {
): Location {
$eventData = [
$trashItem,
$newParentLocation,
Expand All @@ -85,7 +87,7 @@ public function recover(
return $location;
}

public function emptyTrash()
public function emptyTrash(): TrashItemDeleteResultList
{
$beforeEvent = new BeforeEmptyTrashEvent();

Expand All @@ -105,7 +107,7 @@ public function emptyTrash()
return $resultList;
}

public function deleteTrashItem(TrashItem $trashItem)
public function deleteTrashItem(TrashItem $trashItem): TrashItemDeleteResult
{
$eventData = [$trashItem];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use eZ\Publish\API\Repository\TrashService as APIService;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Trash\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList;
use eZ\Publish\Core\Repository\SiteAccessAware\TrashService;
use eZ\Publish\Core\Repository\Values\Content\Location;
use eZ\Publish\Core\Repository\Values\Content\TrashItem;
Expand All @@ -23,17 +26,21 @@ public function getSiteAccessAwareServiceClassName()
public function providerForPassTroughMethods()
{
$location = new Location();
$newLocation = new Location();
$trashItem = new TrashItem();
$query = new Query();
$searchResult = new SearchResult();
$trashItemDeleteResult = new TrashItemDeleteResult();
$trashItemDeleteResultList = new TrashItemDeleteResultList();

// string $method, array $arguments, bool $return = true
return [
['loadTrashItem', [22]],
['trash', [$location]],
['recover', [$trashItem, $location]],
['emptyTrash', []],
['deleteTrashItem', [$trashItem]],
['findTrashItems', [$query]],
['loadTrashItem', [22], $trashItem],
['trash', [$location], $trashItem],
['recover', [$trashItem, $location], $newLocation],
['emptyTrash', [], $trashItemDeleteResultList],
['deleteTrashItem', [$trashItem], $trashItemDeleteResult],
['findTrashItems', [$query], $searchResult],
];
}

Expand Down
19 changes: 11 additions & 8 deletions eZ/Publish/Core/Repository/SiteAccessAware/TrashService.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

/**
* TrashService class.
*
* @copyright Copyright (C) eZ Systems 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\SiteAccessAware;

use eZ\Publish\API\Repository\TrashService as TrashServiceInterface;
use eZ\Publish\API\Repository\Values\Content\TrashItem;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Trash\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList;

/**
* TrashService for SiteAccessAware layer.
Expand All @@ -34,32 +37,32 @@ public function __construct(
$this->service = $service;
}

public function loadTrashItem($trashItemId)
public function loadTrashItem(int $trashItemId): TrashItem
{
return $this->service->loadTrashItem($trashItemId);
}

public function trash(Location $location)
public function trash(Location $location): ?TrashItem
{
return $this->service->trash($location);
}

public function recover(TrashItem $trashItem, Location $newParentLocation = null)
public function recover(TrashItem $trashItem, Location $newParentLocation = null): Location
{
return $this->service->recover($trashItem, $newParentLocation);
}

public function emptyTrash()
public function emptyTrash(): TrashItemDeleteResultList
{
return $this->service->emptyTrash();
}

public function deleteTrashItem(TrashItem $trashItem)
public function deleteTrashItem(TrashItem $trashItem): TrashItemDeleteResult
{
return $this->service->deleteTrashItem($trashItem);
}

public function findTrashItems(Query $query)
public function findTrashItems(Query $query): SearchResult
{
return $this->service->findTrashItems($query);
}
Expand Down
40 changes: 21 additions & 19 deletions eZ/Publish/Core/Repository/TrashService.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/**
* File containing the eZ\Publish\Core\Repository\TrashService class.
*
* @copyright Copyright (C) eZ Systems 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 eZ\Publish\API\Repository\PermissionResolver;
Expand All @@ -29,6 +29,8 @@
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd as CriterionLogicalAnd;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalNot as CriterionLogicalNot;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Subtree as CriterionSubtree;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList;
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult;
use DateTime;
use Exception;

Expand Down Expand Up @@ -88,14 +90,14 @@ public function __construct(
*
* Note that $id is identical to original location, which has been previously trashed
*
* @param mixed $trashItemId
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist
*
* @param mixed $trashItemId
*
* @return \eZ\Publish\API\Repository\Values\Content\TrashItem
*/
public function loadTrashItem($trashItemId)
public function loadTrashItem(int $trashItemId): APITrashItem
{
$spiTrashItem = $this->persistenceHandler->trashHandler()->loadTrashItem($trashItemId);
$trash = $this->buildDomainTrashItemObject(
Expand All @@ -119,13 +121,13 @@ public function loadTrashItem($trashItemId)
* The current user may not have access to the returned trash item, check before using it.
* Content is left untouched.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location
*
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
*
* @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location
*
* @return \eZ\Publish\API\Repository\Values\Content\TrashItem|null null if location was deleted, otherwise TrashItem
*/
public function trash(Location $location)
public function trash(Location $location): ?APITrashItem
{
if (empty($location->id)) {
throw new InvalidArgumentValue('id', $location->id, 'Location');
Expand Down Expand Up @@ -161,16 +163,14 @@ public function trash(Location $location)
/**
* Recovers the $trashedLocation at its original place if possible.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
*
* If $newParentLocation is provided, $trashedLocation will be restored under it.
*
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location
*
* @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location
*/
public function recover(APITrashItem $trashItem, Location $newParentLocation = null)
public function recover(APITrashItem $trashItem, Location $newParentLocation = null): Location
{
if (!is_numeric($trashItem->id)) {
throw new InvalidArgumentValue('id', $trashItem->id, 'TrashItem');
Expand Down Expand Up @@ -231,8 +231,10 @@ public function recover(APITrashItem $trashItem, Location $newParentLocation = n
* if all locations of the content are gone.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash
*
* @return \eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList
*/
public function emptyTrash()
public function emptyTrash(): TrashItemDeleteResultList
{
if ($this->permissionResolver->hasAccess('content', 'cleantrash') === false) {
throw new UnauthorizedException('content', 'cleantrash');
Expand All @@ -256,13 +258,13 @@ public function emptyTrash()
*
* The corresponding content object will be removed
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item
*
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item
*
* @return \eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult
*/
public function deleteTrashItem(APITrashItem $trashItem)
public function deleteTrashItem(APITrashItem $trashItem): TrashItemDeleteResult
{
if (!$this->permissionResolver->canUser('content', 'cleantrash', $trashItem->getContentInfo())) {
throw new UnauthorizedException('content', 'cleantrash');
Expand Down Expand Up @@ -293,7 +295,7 @@ public function deleteTrashItem(APITrashItem $trashItem)
*
* @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult
*/
public function findTrashItems(Query $query)
public function findTrashItems(Query $query): SearchResult
{
if ($query->filter !== null && !$query->filter instanceof Criterion) {
throw new InvalidArgumentValue('query->filter', $query->filter, 'Query');
Expand Down
Loading

0 comments on commit 0685d5f

Please sign in to comment.