From 394d9ff4d2695bff17462fecf5ff1eb3a444e850 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 15 Sep 2018 11:01:23 +0200 Subject: [PATCH] Defined config and implementation to delete short URLs --- config/autoload/app_options.global.php | 1 + config/autoload/delete_short_urls.global.php | 13 +++++++ module/Core/config/dependencies.config.php | 1 + module/Core/src/Options/AppOptionsFactory.php | 2 +- .../src/Options/DeleteShortUrlsOptions.php | 34 +++++++++++++++++++ .../Options/DeleteShortUrlsOptionsFactory.php | 31 +++++++++++++++++ module/Core/src/Service/ShortUrlService.php | 23 +++++++------ .../src/Service/ShortUrlServiceInterface.php | 18 +++++----- .../Action/ShortCode/ListShortCodesAction.php | 2 +- 9 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 config/autoload/delete_short_urls.global.php create mode 100644 module/Core/src/Options/DeleteShortUrlsOptions.php create mode 100644 module/Core/src/Options/DeleteShortUrlsOptionsFactory.php diff --git a/config/autoload/app_options.global.php b/config/autoload/app_options.global.php index 12e22c0f..e280b4cf 100644 --- a/config/autoload/app_options.global.php +++ b/config/autoload/app_options.global.php @@ -9,6 +9,7 @@ return [ 'name' => 'Shlink', 'version' => '%SHLINK_VERSION%', 'secret_key' => env('SECRET_KEY'), + 'disable_track_param' => null, ], ]; diff --git a/config/autoload/delete_short_urls.global.php b/config/autoload/delete_short_urls.global.php new file mode 100644 index 00000000..e3d4ca64 --- /dev/null +++ b/config/autoload/delete_short_urls.global.php @@ -0,0 +1,13 @@ + [ + 'visits_threshold' => 15, + 'check_visits_threshold' => true, + ], + +]; diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 8a696525..8eb6e9b6 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -17,6 +17,7 @@ return [ 'dependencies' => [ 'factories' => [ Options\AppOptions::class => Options\AppOptionsFactory::class, + Options\DeleteShortUrlsOptions::class => Options\DeleteShortUrlsOptionsFactory::class, NotFoundHandler::class => ConfigAbstractFactory::class, // Services diff --git a/module/Core/src/Options/AppOptionsFactory.php b/module/Core/src/Options/AppOptionsFactory.php index d61d517e..0df25a2a 100644 --- a/module/Core/src/Options/AppOptionsFactory.php +++ b/module/Core/src/Options/AppOptionsFactory.php @@ -26,6 +26,6 @@ class AppOptionsFactory implements FactoryInterface public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { $config = $container->has('config') ? $container->get('config') : []; - return new AppOptions(isset($config['app_options']) ? $config['app_options'] : []); + return new AppOptions($config['app_options'] ?? []); } } diff --git a/module/Core/src/Options/DeleteShortUrlsOptions.php b/module/Core/src/Options/DeleteShortUrlsOptions.php new file mode 100644 index 00000000..65dffdc4 --- /dev/null +++ b/module/Core/src/Options/DeleteShortUrlsOptions.php @@ -0,0 +1,34 @@ +visitsThreshold; + } + + protected function setVisitsThreshold(int $visitsThreshold): self + { + $this->visitsThreshold = $visitsThreshold; + return $this; + } + + public function doCheckVisitsThreshold(): bool + { + return $this->checkVisitsThreshold; + } + + protected function setCheckVisitsThreshold(bool $checkVisitsThreshold): self + { + $this->checkVisitsThreshold = $checkVisitsThreshold; + return $this; + } +} diff --git a/module/Core/src/Options/DeleteShortUrlsOptionsFactory.php b/module/Core/src/Options/DeleteShortUrlsOptionsFactory.php new file mode 100644 index 00000000..fab7cfee --- /dev/null +++ b/module/Core/src/Options/DeleteShortUrlsOptionsFactory.php @@ -0,0 +1,31 @@ +has('config') ? $container->get('config') : []; + return new DeleteShortUrlsOptions($config['delete_short_urls'] ?? []); + } +} diff --git a/module/Core/src/Service/ShortUrlService.php b/module/Core/src/Service/ShortUrlService.php index 3a89b828..b96adff0 100644 --- a/module/Core/src/Service/ShortUrlService.php +++ b/module/Core/src/Service/ShortUrlService.php @@ -27,13 +27,11 @@ class ShortUrlService implements ShortUrlServiceInterface } /** - * @param int $page - * @param string $searchQuery - * @param array $tags - * @param null $orderBy + * @param string[] $tags + * @param array|string|null $orderBy * @return ShortUrl[]|Paginator */ - public function listShortUrls($page = 1, $searchQuery = null, array $tags = [], $orderBy = null) + public function listShortUrls(int $page = 1, string $searchQuery = null, array $tags = [], $orderBy = null) { /** @var ShortUrlRepository $repo */ $repo = $this->em->getRepository(ShortUrl::class); @@ -45,9 +43,7 @@ class ShortUrlService implements ShortUrlServiceInterface } /** - * @param string $shortCode * @param string[] $tags - * @return ShortUrl * @throws InvalidShortCodeException */ public function setTagsByShortCode(string $shortCode, array $tags = []): ShortUrl @@ -60,9 +56,6 @@ class ShortUrlService implements ShortUrlServiceInterface } /** - * @param string $shortCode - * @param ShortUrlMeta $shortCodeMeta - * @return ShortUrl * @throws InvalidShortCodeException */ public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl @@ -81,9 +74,19 @@ class ShortUrlService implements ShortUrlServiceInterface /** @var ORM\EntityManager $em */ $em = $this->em; $em->flush($shortUrl); + return $shortUrl; } + /** + * @throws InvalidShortCodeException + */ + public function deleteByShortCode(string $shortCode): void + { + $this->em->remove($this->findByShortCode($shortCode)); + $this->em->flush(); + } + /** * @param string $shortCode * @return ShortUrl diff --git a/module/Core/src/Service/ShortUrlServiceInterface.php b/module/Core/src/Service/ShortUrlServiceInterface.php index 2350105b..d616648c 100644 --- a/module/Core/src/Service/ShortUrlServiceInterface.php +++ b/module/Core/src/Service/ShortUrlServiceInterface.php @@ -11,27 +11,25 @@ use Zend\Paginator\Paginator; interface ShortUrlServiceInterface { /** - * @param int $page - * @param string $searchQuery - * @param array $tags - * @param null $orderBy + * @param string[] $tags + * @param array|string|null $orderBy * @return ShortUrl[]|Paginator */ - public function listShortUrls($page = 1, $searchQuery = null, array $tags = [], $orderBy = null); + public function listShortUrls(int $page = 1, string $searchQuery = null, array $tags = [], $orderBy = null); /** - * @param string $shortCode * @param string[] $tags - * @return ShortUrl * @throws InvalidShortCodeException */ public function setTagsByShortCode(string $shortCode, array $tags = []): ShortUrl; /** - * @param string $shortCode - * @param ShortUrlMeta $shortCodeMeta - * @return ShortUrl * @throws InvalidShortCodeException */ public function updateMetadataByShortCode(string $shortCode, ShortUrlMeta $shortCodeMeta): ShortUrl; + + /** + * @throws InvalidShortCodeException + */ + public function deleteByShortCode(string $shortCode): void; } diff --git a/module/Rest/src/Action/ShortCode/ListShortCodesAction.php b/module/Rest/src/Action/ShortCode/ListShortCodesAction.php index e69840a5..f95be789 100644 --- a/module/Rest/src/Action/ShortCode/ListShortCodesAction.php +++ b/module/Rest/src/Action/ShortCode/ListShortCodesAction.php @@ -75,7 +75,7 @@ class ListShortCodesAction extends AbstractRestAction private function queryToListParams(array $query): array { return [ - $query['page'] ?? 1, + (int) ($query['page'] ?? 1), $query['searchTerm'] ?? null, $query['tags'] ?? [], $query['orderBy'] ?? null,