From 240d2588f9aee75571f053588b8161a627d82d3f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 28 Jan 2020 09:41:48 +0100 Subject: [PATCH] Extracted some private functions ase helper global functions --- module/Core/functions/functions.php | 23 +++++++++++++++ module/Core/src/Model/ShortUrlMeta.php | 28 ++++--------------- module/Core/src/Model/VisitsParams.php | 13 ++------- .../Validation/ShortUrlMetaInputFilter.php | 6 ++-- .../Action/ShortUrl/ListShortUrlsAction.php | 8 ------ 5 files changed, 33 insertions(+), 45 deletions(-) diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php index 92d40fe1..1384549f 100644 --- a/module/Core/functions/functions.php +++ b/module/Core/functions/functions.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core; +use Cake\Chronos\Chronos; +use DateTimeInterface; use PUGX\Shortid\Factory as ShortIdFactory; function generateRandomShortCode(int $length = 5): string @@ -16,3 +18,24 @@ function generateRandomShortCode(int $length = 5): string $alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; return $shortIdFactory->generate($length, $alphabet)->serialize(); } + +function parseDateFromQuery(array $query, string $dateName): ?Chronos +{ + return ! isset($query[$dateName]) || empty($query[$dateName]) ? null : Chronos::parse($query[$dateName]); +} + +/** + * @param string|DateTimeInterface|Chronos|null $date + */ +function parseDateField($date): ?Chronos +{ + if ($date === null || $date instanceof Chronos) { + return $date; + } + + if ($date instanceof DateTimeInterface) { + return Chronos::instance($date); + } + + return Chronos::parse($date); +} diff --git a/module/Core/src/Model/ShortUrlMeta.php b/module/Core/src/Model/ShortUrlMeta.php index f0c487b7..27c8e624 100644 --- a/module/Core/src/Model/ShortUrlMeta.php +++ b/module/Core/src/Model/ShortUrlMeta.php @@ -5,11 +5,11 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Model; use Cake\Chronos\Chronos; -use DateTimeInterface; use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter; use function array_key_exists; +use function Shlinkio\Shlink\Core\parseDateField; final class ShortUrlMeta { @@ -34,30 +34,28 @@ final class ShortUrlMeta } /** - * @param array $data * @throws ValidationException */ public static function fromRawData(array $data): self { $instance = new self(); - $instance->validate($data); + $instance->validateAndInit($data); return $instance; } /** - * @param array $data * @throws ValidationException */ - private function validate(array $data): void + private function validateAndInit(array $data): void { $inputFilter = new ShortUrlMetaInputFilter($data); if (! $inputFilter->isValid()) { throw ValidationException::fromInputFilter($inputFilter); } - $this->validSince = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE)); + $this->validSince = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_SINCE)); $this->validSincePropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_SINCE, $data); - $this->validUntil = $this->parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL)); + $this->validUntil = parseDateField($inputFilter->getValue(ShortUrlMetaInputFilter::VALID_UNTIL)); $this->validUntilPropWasProvided = array_key_exists(ShortUrlMetaInputFilter::VALID_UNTIL, $data); $this->customSlug = $inputFilter->getValue(ShortUrlMetaInputFilter::CUSTOM_SLUG); $maxVisits = $inputFilter->getValue(ShortUrlMetaInputFilter::MAX_VISITS); @@ -67,22 +65,6 @@ final class ShortUrlMeta $this->domain = $inputFilter->getValue(ShortUrlMetaInputFilter::DOMAIN); } - /** - * @param string|DateTimeInterface|Chronos|null $date - */ - private function parseDateField($date): ?Chronos - { - if ($date === null || $date instanceof Chronos) { - return $date; - } - - if ($date instanceof DateTimeInterface) { - return Chronos::instance($date); - } - - return Chronos::parse($date); - } - public function getValidSince(): ?Chronos { return $this->validSince; diff --git a/module/Core/src/Model/VisitsParams.php b/module/Core/src/Model/VisitsParams.php index 98fcbe82..041aed9f 100644 --- a/module/Core/src/Model/VisitsParams.php +++ b/module/Core/src/Model/VisitsParams.php @@ -4,9 +4,10 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Model; -use Cake\Chronos\Chronos; use Shlinkio\Shlink\Common\Util\DateRange; +use function Shlinkio\Shlink\Core\parseDateFromQuery; + final class VisitsParams { private const FIRST_PAGE = 1; @@ -34,21 +35,13 @@ final class VisitsParams public static function fromRawData(array $query): self { - $startDate = self::getDateQueryParam($query, 'startDate'); - $endDate = self::getDateQueryParam($query, 'endDate'); - return new self( - new DateRange($startDate, $endDate), + new DateRange(parseDateFromQuery($query, 'startDate'), parseDateFromQuery($query, 'endDate')), (int) ($query['page'] ?? 1), isset($query['itemsPerPage']) ? (int) $query['itemsPerPage'] : null, ); } - private static function getDateQueryParam(array $query, string $key): ?Chronos - { - return ! isset($query[$key]) || empty($query[$key]) ? null : Chronos::parse($query[$key]); - } - public function getDateRange(): DateRange { return $this->dateRange; diff --git a/module/Core/src/Validation/ShortUrlMetaInputFilter.php b/module/Core/src/Validation/ShortUrlMetaInputFilter.php index eca97a13..187ec66f 100644 --- a/module/Core/src/Validation/ShortUrlMetaInputFilter.php +++ b/module/Core/src/Validation/ShortUrlMetaInputFilter.php @@ -20,12 +20,10 @@ class ShortUrlMetaInputFilter extends InputFilter public const FIND_IF_EXISTS = 'findIfExists'; public const DOMAIN = 'domain'; - public function __construct(?array $data = null) + public function __construct(array $data) { $this->initialize(); - if ($data !== null) { - $this->setData($data); - } + $this->setData($data); } private function initialize(): void diff --git a/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php b/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php index a50493db..11a09a76 100644 --- a/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php +++ b/module/Rest/src/Action/ShortUrl/ListShortUrlsAction.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Action\ShortUrl; use Cake\Chronos\Chronos; -use InvalidArgumentException; use Laminas\Diactoros\Response\JsonResponse; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -36,9 +35,6 @@ class ListShortUrlsAction extends AbstractRestAction $this->domainConfig = $domainConfig; } - /** - * @throws InvalidArgumentException - */ public function handle(Request $request): Response { $params = $this->queryToListParams($request->getQueryParams()); @@ -48,10 +44,6 @@ class ListShortUrlsAction extends AbstractRestAction ))]); } - /** - * @param array $query - * @return array - */ private function queryToListParams(array $query): array { return [