From 68b4cfbae024573a80604d4973d9661ff32d3ef8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 21 Oct 2017 11:39:27 +0200 Subject: [PATCH] Added valid_since and valid_until columns to shoirt_urls table --- data/migrations/Version20171021093246.php | 41 +++++++++++++++++++ module/Core/config/dependencies.config.php | 2 +- module/Core/src/Action/RedirectAction.php | 14 +------ module/Core/src/Service/UrlShortener.php | 6 +-- .../src/Service/UrlShortenerInterface.php | 4 +- 5 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 data/migrations/Version20171021093246.php diff --git a/data/migrations/Version20171021093246.php b/data/migrations/Version20171021093246.php new file mode 100644 index 00000000..a9c39ebb --- /dev/null +++ b/data/migrations/Version20171021093246.php @@ -0,0 +1,41 @@ +getTable('short_urls'); + $shortUrls->addColumn('valid_since', Type::DATETIME, [ + 'notnull' => false, + ]); + $shortUrls->addColumn('valid_until', Type::DATETIME, [ + 'notnull' => false, + ]); + } + + /** + * @param Schema $schema + * @throws SchemaException + */ + public function down(Schema $schema) + { + $shortUrls = $schema->getTable('short_urls'); + $shortUrls->dropColumn('valid_since'); + $shortUrls->dropColumn('valid_until'); + } +} diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index d8009880..9fbd2766 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -49,7 +49,7 @@ return [ Service\Tag\TagService::class => ['em'], // Middleware - Action\RedirectAction::class => [Service\UrlShortener::class, Service\VisitsTracker::class, 'Logger_Shlink'], + Action\RedirectAction::class => [Service\UrlShortener::class, Service\VisitsTracker::class], Action\QrCodeAction::class => [RouterInterface::class, Service\UrlShortener::class, 'Logger_Shlink'], Action\PreviewAction::class => [PreviewGenerator::class, Service\UrlShortener::class], Middleware\QrCodeCacheMiddleware::class => [Cache::class], diff --git a/module/Core/src/Action/RedirectAction.php b/module/Core/src/Action/RedirectAction.php index 42560e5f..e22639c7 100644 --- a/module/Core/src/Action/RedirectAction.php +++ b/module/Core/src/Action/RedirectAction.php @@ -7,8 +7,6 @@ use Interop\Http\ServerMiddleware\DelegateInterface; use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -use Psr\Log\LoggerInterface; -use Psr\Log\NullLogger; use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait; use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; @@ -28,19 +26,11 @@ class RedirectAction implements MiddlewareInterface * @var VisitsTrackerInterface */ private $visitTracker; - /** - * @var null|LoggerInterface - */ - private $logger; - public function __construct( - UrlShortenerInterface $urlShortener, - VisitsTrackerInterface $visitTracker, - LoggerInterface $logger = null - ) { + public function __construct(UrlShortenerInterface $urlShortener, VisitsTrackerInterface $visitTracker) + { $this->urlShortener = $urlShortener; $this->visitTracker = $visitTracker; - $this->logger = $logger ?: new NullLogger(); } /** diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index b30b649a..148279e8 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -60,13 +60,13 @@ class UrlShortener implements UrlShortenerInterface * @throws InvalidUrlException * @throws RuntimeException */ - public function urlToShortCode(UriInterface $url, array $tags = []) + public function urlToShortCode(UriInterface $url, array $tags = []): string { // If the url already exists in the database, just return its short code $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([ 'originalUrl' => $url, ]); - if (isset($shortUrl)) { + if ($shortUrl !== null) { return $shortUrl->getShortCode(); } @@ -147,7 +147,7 @@ class UrlShortener implements UrlShortenerInterface * @throws InvalidShortCodeException * @throws EntityDoesNotExistException */ - public function shortCodeToUrl($shortCode): string + public function shortCodeToUrl(string $shortCode): string { $cacheKey = sprintf('%s_longUrl', $shortCode); // Check if the short code => URL map is already cached diff --git a/module/Core/src/Service/UrlShortenerInterface.php b/module/Core/src/Service/UrlShortenerInterface.php index 3cd2c35c..6fc0c24a 100644 --- a/module/Core/src/Service/UrlShortenerInterface.php +++ b/module/Core/src/Service/UrlShortenerInterface.php @@ -20,7 +20,7 @@ interface UrlShortenerInterface * @throws InvalidUrlException * @throws RuntimeException */ - public function urlToShortCode(UriInterface $url, array $tags = []); + public function urlToShortCode(UriInterface $url, array $tags = []): string; /** * Tries to find the mapped URL for provided short code. Returns null if not found @@ -30,5 +30,5 @@ interface UrlShortenerInterface * @throws InvalidShortCodeException * @throws EntityDoesNotExistException */ - public function shortCodeToUrl($shortCode): string; + public function shortCodeToUrl(string $shortCode): string; }