Migrated from standard datetime objects to chronos objects

This commit is contained in:
Alejandro Celaya
2018-09-29 12:52:32 +02:00
parent 9a2ca35e6e
commit 0183c8a4b7
31 changed files with 271 additions and 198 deletions

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Cake\Chronos\Chronos;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
@@ -40,8 +41,8 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction
);
}
private function getOptionalDate(array $postData, string $fieldName)
private function getOptionalDate(array $postData, string $fieldName): ?Chronos
{
return isset($postData[$fieldName]) ? new \DateTime($postData[$fieldName]) : null;
return isset($postData[$fieldName]) ? Chronos::parse($postData[$fieldName]) : null;
}
}

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Visit;
use Cake\Chronos\Chronos;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
@@ -75,18 +76,9 @@ class GetVisitsAction extends AbstractRestAction
}
}
/**
* @param Request $request
* @param string $key
* @return \DateTime|null
*/
private function getDateQueryParam(Request $request, string $key)
private function getDateQueryParam(Request $request, string $key): ?Chronos
{
$query = $request->getQueryParams();
if (! isset($query[$key]) || empty($query[$key])) {
return null;
}
return new \DateTime($query[$key]);
return ! isset($query[$key]) || empty($query[$key]) ? null : Chronos::parse($query[$key]);
}
}

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Entity;
use Cake\Chronos\Chronos;
use Doctrine\ORM\Mapping as ORM;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
@@ -25,8 +26,8 @@ class ApiKey extends AbstractEntity
*/
private $key;
/**
* @var \DateTime|null
* @ORM\Column(name="expiration_date", nullable=true, type="datetime")
* @var Chronos|null
* @ORM\Column(name="expiration_date", nullable=true, type="chronos_datetime")
*/
private $expirationDate;
/**
@@ -52,12 +53,12 @@ class ApiKey extends AbstractEntity
return $this;
}
public function getExpirationDate(): ?\DateTime
public function getExpirationDate(): ?Chronos
{
return $this->expirationDate;
}
public function setExpirationDate(\DateTime $expirationDate): self
public function setExpirationDate(Chronos $expirationDate): self
{
$this->expirationDate = $expirationDate;
return $this;
@@ -69,7 +70,7 @@ class ApiKey extends AbstractEntity
return false;
}
return $this->expirationDate < new \DateTime();
return $this->expirationDate->lt(Chronos::now());
}
public function isEnabled(): bool

View File

@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Service;
use Cake\Chronos\Chronos;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
@@ -20,13 +21,7 @@ class ApiKeyService implements ApiKeyServiceInterface
$this->em = $em;
}
/**
* Creates a new ApiKey with provided expiration date
*
* @param \DateTime $expirationDate
* @return ApiKey
*/
public function create(\DateTime $expirationDate = null): ApiKey
public function create(?Chronos $expirationDate = null): ApiKey
{
$key = new ApiKey();
if ($expirationDate !== null) {
@@ -39,12 +34,6 @@ class ApiKeyService implements ApiKeyServiceInterface
return $key;
}
/**
* Checks if provided key is a valid api key
*
* @param string $key
* @return bool
*/
public function check(string $key): bool
{
/** @var ApiKey|null $apiKey */
@@ -53,10 +42,6 @@ class ApiKeyService implements ApiKeyServiceInterface
}
/**
* Disables provided api key
*
* @param string $key
* @return ApiKey
* @throws InvalidArgumentException
*/
public function disable(string $key): ApiKey
@@ -72,12 +57,6 @@ class ApiKeyService implements ApiKeyServiceInterface
return $apiKey;
}
/**
* Lists all existing api keys
*
* @param bool $enabledOnly Tells if only enabled keys should be returned
* @return ApiKey[]
*/
public function listKeys(bool $enabledOnly = false): array
{
$conditions = $enabledOnly ? ['enabled' => true] : [];
@@ -86,12 +65,6 @@ class ApiKeyService implements ApiKeyServiceInterface
return $apiKeys;
}
/**
* Tries to find one API key by its key string
*
* @param string $key
* @return ApiKey|null
*/
public function getByKey(string $key): ?ApiKey
{
/** @var ApiKey|null $apiKey */

View File

@@ -3,49 +3,22 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Service;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface ApiKeyServiceInterface
{
/**
* Creates a new ApiKey with provided expiration date
*
* @param \DateTime $expirationDate
* @return ApiKey
*/
public function create(\DateTime $expirationDate = null): ApiKey;
public function create(?Chronos $expirationDate = null): ApiKey;
/**
* Checks if provided key is a valid api key
*
* @param string $key
* @return bool
*/
public function check(string $key): bool;
/**
* Disables provided api key
*
* @param string $key
* @return ApiKey
* @throws InvalidArgumentException
*/
public function disable(string $key): ApiKey;
/**
* Lists all existing api keys
*
* @param bool $enabledOnly Tells if only enabled keys should be returned
* @return ApiKey[]
*/
public function listKeys(bool $enabledOnly = false): array;
/**
* Tries to find one API key by its key string
*
* @param string $key
* @return ApiKey|null
*/
public function getByKey(string $key): ?ApiKey;
}