Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-31097: Introduced strict types for URLService #2854

Merged
merged 1 commit into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eZ/Publish/API/Repository/Tests/URLServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ public function testUpdateUrlStatus()
$urlService = $repository->getURLService();

$urlBeforeUpdate = $urlService->loadById($id);

$updateStruct = $urlService->createUpdateStruct();
$updateStruct->isValid = false;
$updateStruct->lastChecked = $checked;
Expand Down
29 changes: 19 additions & 10 deletions eZ/Publish/API/Repository/URLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
* @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\URL\SearchResult;
use eZ\Publish\API\Repository\Values\URL\URL;
use eZ\Publish\API\Repository\Values\URL\URLQuery;
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct;
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;

/**
* URL Service.
Expand All @@ -20,17 +24,18 @@ interface URLService
*
* @return \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct
*/
public function createUpdateStruct();
public function createUpdateStruct(): URLUpdateStruct;

/**
* Find URLs.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
adamwojs marked this conversation as resolved.
Show resolved Hide resolved
*
* @param \eZ\Publish\API\Repository\Values\URL\URLQuery $query
*
* @return \eZ\Publish\API\Repository\Values\URL\SearchResult
*/
public function findUrls(URLQuery $query);
public function findUrls(URLQuery $query): SearchResult;

/**
* Find content objects using URL.
Expand All @@ -40,42 +45,46 @@ public function findUrls(URLQuery $query);
* @param \eZ\Publish\API\Repository\Values\URL\URL $url
* @param int $offset
* @param int $limit
*
* @return \eZ\Publish\API\Repository\Values\URL\UsageSearchResult
*/
public function findUsages(URL $url, $offset = 0, $limit = -1);
public function findUsages(URL $url, int $offset = 0, int $limit = -1): UsageSearchResult;

/**
* Load single URL (by ID).
*
* @param int $id ID of URL
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*
* @param int $id ID of URL
* @return \eZ\Publish\API\Repository\Values\URL\URL
*/
public function loadById($id);
public function loadById(int $id): URL;

/**
* Load single URL (by URL).
*
* @param string $url URL
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*
* @param string $url url
* @return \eZ\Publish\API\Repository\Values\URL\URL
*/
public function loadByUrl($url);
public function loadByUrl(string $url): URL;

/**
* Updates URL.
*
* @param \eZ\Publish\API\Repository\Values\URL\URL $url
* @param \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct $struct
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the url already exists
*
* @param \eZ\Publish\API\Repository\Values\URL\URL $url
* @param \eZ\Publish\API\Repository\Values\URL\URLUpdateStruct $struct
* @return \eZ\Publish\API\Repository\Values\URL\URL
*/
public function updateUrl(URL $url, URLUpdateStruct $struct);
public function updateUrl(URL $url, URLUpdateStruct $struct): URL;
}
2 changes: 1 addition & 1 deletion eZ/Publish/Core/Event/URLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
public function updateUrl(
URL $url,
URLUpdateStruct $struct
) {
): URL {
$eventData = [
$url,
$struct,
Expand Down
5 changes: 0 additions & 5 deletions eZ/Publish/Core/Repository/Tests/Service/Mock/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,6 @@ public function testUpdateUrlStatus()
]);

$urlService = $this->createUrlService(['isUnique']);
$urlService
->expects($this->once())
->method('isUnique')
->with($apiUrl->id, $apiStruct->url)
->willReturn(true);

$this->urlHandler
->expects($this->once())
Expand Down
26 changes: 15 additions & 11 deletions eZ/Publish/Core/Repository/URLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
* @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 DateTime;
use DateTimeInterface;
use Exception;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\PermissionResolver;
Expand Down Expand Up @@ -55,7 +58,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function findUrls(URLQuery $query)
public function findUrls(URLQuery $query): SearchResult
{
if ($this->permissionResolver->hasAccess('url', 'view') === false) {
throw new UnauthorizedException('url', 'view');
Expand Down Expand Up @@ -85,13 +88,13 @@ public function findUrls(URLQuery $query)
/**
* {@inheritdoc}
*/
public function updateUrl(URL $url, URLUpdateStruct $struct)
public function updateUrl(URL $url, URLUpdateStruct $struct): URL
{
if (!$this->permissionResolver->canUser('url', 'update', $url)) {
throw new UnauthorizedException('url', 'update');
}

if (!$this->isUnique($url->id, $struct->url)) {
if ($struct->url !== null && !$this->isUnique($url->id, $struct->url)) {
throw new InvalidArgumentException('struct', 'url already exists');
}

Expand All @@ -112,7 +115,7 @@ public function updateUrl(URL $url, URLUpdateStruct $struct)
/**
* {@inheritdoc}
*/
public function loadById($id)
public function loadById(int $id): URL
{
$url = $this->buildDomainObject(
$this->urlHandler->loadById($id)
Expand All @@ -128,7 +131,7 @@ public function loadById($id)
/**
* {@inheritdoc}
*/
public function loadByUrl($url)
public function loadByUrl(string $url): URL
{
$apiUrl = $this->buildDomainObject(
$this->urlHandler->loadByUrl($url)
Expand All @@ -144,15 +147,15 @@ public function loadByUrl($url)
/**
* {@inheritdoc}
*/
public function createUpdateStruct()
public function createUpdateStruct(): URLUpdateStruct
{
return new URLUpdateStruct();
}

/**
* {@inheritdoc}
*/
public function findUsages(URL $url, $offset = 0, $limit = -1)
public function findUsages(URL $url, int $offset = 0, int $limit = -1): UsageSearchResult
{
$contentIds = $this->urlHandler->findUsages($url->id);
if (empty($contentIds)) {
Expand Down Expand Up @@ -188,7 +191,7 @@ public function findUsages(URL $url, $offset = 0, $limit = -1)
*
* @return \eZ\Publish\API\Repository\Values\URL\URL
*/
protected function buildDomainObject(SPIUrl $data)
protected function buildDomainObject(SPIUrl $data): URL
{
return new URL([
'id' => $data->id,
Expand All @@ -208,7 +211,7 @@ protected function buildDomainObject(SPIUrl $data)
*
* @return \eZ\Publish\SPI\Persistence\URL\URLUpdateStruct
*/
protected function buildUpdateStruct(URL $url, URLUpdateStruct $data)
protected function buildUpdateStruct(URL $url, URLUpdateStruct $data): SPIUrlUpdateStruct
{
$updateStruct = new SPIUrlUpdateStruct();

Expand Down Expand Up @@ -245,9 +248,10 @@ protected function buildUpdateStruct(URL $url, URLUpdateStruct $data)
* @param string $url
*
* @return bool
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
protected function isUnique($id, $url)
protected function isUnique(int $id, string $url): bool
{
try {
return $this->loadByUrl($url)->id === $id;
Expand All @@ -256,7 +260,7 @@ protected function isUnique($id, $url)
}
}

private function createDateTime($timestamp)
private function createDateTime(?int $timestamp): ?DateTimeInterface
{
if ($timestamp > 0) {
return new DateTime("@{$timestamp}");
Expand Down
18 changes: 10 additions & 8 deletions eZ/Publish/SPI/Repository/Decorator/URLServiceDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
namespace eZ\Publish\SPI\Repository\Decorator;

use eZ\Publish\API\Repository\URLService;
use eZ\Publish\API\Repository\Values\URL\SearchResult;
use eZ\Publish\API\Repository\Values\URL\URL;
use eZ\Publish\API\Repository\Values\URL\URLQuery;
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct;
use eZ\Publish\API\Repository\Values\URL\UsageSearchResult;

abstract class URLServiceDecorator implements URLService
{
Expand All @@ -23,38 +25,38 @@ public function __construct(URLService $innerService)
$this->innerService = $innerService;
}

public function createUpdateStruct()
public function createUpdateStruct(): URLUpdateStruct
{
return $this->innerService->createUpdateStruct();
}

public function findUrls(URLQuery $query)
public function findUrls(URLQuery $query): SearchResult
{
return $this->innerService->findUrls($query);
}

public function findUsages(
URL $url,
$offset = 0,
$limit = -1
) {
int $offset = 0,
int $limit = -1
): UsageSearchResult {
return $this->innerService->findUsages($url, $offset, $limit);
}

public function loadById($id)
public function loadById(int $id): URL
{
return $this->innerService->loadById($id);
}

public function loadByUrl($url)
public function loadByUrl(string $url): URL
{
return $this->innerService->loadByUrl($url);
}

public function updateUrl(
URL $url,
URLUpdateStruct $struct
) {
): URL {
return $this->innerService->updateUrl($url, $struct);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function testFindUsagesDecorator()

$parameters = [
$this->createMock(URL::class),
'random_value_5ced05ce172579.87753442',
'random_value_5ced05ce1725a8.96796907',
10,
100,
];

$serviceMock->expects($this->once())->method('findUsages')->with(...$parameters);
Expand All @@ -74,7 +74,7 @@ public function testLoadByIdDecorator()
$serviceMock = $this->createServiceMock();
$decoratedService = $this->createDecorator($serviceMock);

$parameters = ['random_value_5ced05ce172600.22330806'];
$parameters = [1];

$serviceMock->expects($this->once())->method('loadById')->with(...$parameters);

Expand Down