From 1ef10f11cb4a33acd50f51b94e8e9ee532ce2166 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 1 May 2020 11:40:02 +0200 Subject: [PATCH] Created new action to get default visit stats --- module/Core/config/dependencies.config.php | 2 ++ module/Core/src/Visit/Model/VisitsStats.php | 24 ++++++++++++++ module/Core/src/Visit/VisitsStatsHelper.php | 32 ++++++++++++++++++ .../src/Visit/VisitsStatsHelperInterface.php | 12 +++++++ module/Rest/config/dependencies.config.php | 3 ++ module/Rest/config/routes.config.php | 1 + .../src/Action/Visit/GlobalVisitsAction.php | 33 +++++++++++++++++++ .../Action/GlobalVisitsActionTest.php | 21 ++++++++++++ ...nTest.php => ShortUrlVisitsActionTest.php} | 2 +- 9 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 module/Core/src/Visit/Model/VisitsStats.php create mode 100644 module/Core/src/Visit/VisitsStatsHelper.php create mode 100644 module/Core/src/Visit/VisitsStatsHelperInterface.php create mode 100644 module/Rest/src/Action/Visit/GlobalVisitsAction.php create mode 100644 module/Rest/test-api/Action/GlobalVisitsActionTest.php rename module/Rest/test-api/Action/{GetVisitsActionTest.php => ShortUrlVisitsActionTest.php} (97%) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 63e6cfed..67d18c40 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -27,6 +27,7 @@ return [ Service\VisitsTracker::class => ConfigAbstractFactory::class, Service\ShortUrlService::class => ConfigAbstractFactory::class, Visit\VisitLocator::class => ConfigAbstractFactory::class, + Visit\VisitsStatsHelper::class => ConfigAbstractFactory::class, Service\Tag\TagService::class => ConfigAbstractFactory::class, Service\ShortUrl\DeleteShortUrlService::class => ConfigAbstractFactory::class, Service\ShortUrl\ShortUrlResolver::class => ConfigAbstractFactory::class, @@ -56,6 +57,7 @@ return [ Service\VisitsTracker::class => ['em', EventDispatcherInterface::class], Service\ShortUrlService::class => ['em', Service\ShortUrl\ShortUrlResolver::class, Util\UrlValidator::class], Visit\VisitLocator::class => ['em'], + Visit\VisitsStatsHelper::class => ['em'], Service\Tag\TagService::class => ['em'], Service\ShortUrl\DeleteShortUrlService::class => [ 'em', diff --git a/module/Core/src/Visit/Model/VisitsStats.php b/module/Core/src/Visit/Model/VisitsStats.php new file mode 100644 index 00000000..ac5083c7 --- /dev/null +++ b/module/Core/src/Visit/Model/VisitsStats.php @@ -0,0 +1,24 @@ +visitsCount = $visitsCount; + } + + public function jsonSerialize(): array + { + return [ + 'visitsCount' => $this->visitsCount, + ]; + } +} diff --git a/module/Core/src/Visit/VisitsStatsHelper.php b/module/Core/src/Visit/VisitsStatsHelper.php new file mode 100644 index 00000000..de3219ff --- /dev/null +++ b/module/Core/src/Visit/VisitsStatsHelper.php @@ -0,0 +1,32 @@ +em = $em; + } + + public function getVisitsStats(): VisitsStats + { + return new VisitsStats($this->getVisitsCount()); + } + + private function getVisitsCount(): int + { + /** @var VisitRepository $visitsRepo */ + $visitsRepo = $this->em->getRepository(Visit::class); + return $visitsRepo->count([]); + } +} diff --git a/module/Core/src/Visit/VisitsStatsHelperInterface.php b/module/Core/src/Visit/VisitsStatsHelperInterface.php new file mode 100644 index 00000000..81423cb0 --- /dev/null +++ b/module/Core/src/Visit/VisitsStatsHelperInterface.php @@ -0,0 +1,12 @@ + ConfigAbstractFactory::class, Action\ShortUrl\EditShortUrlTagsAction::class => ConfigAbstractFactory::class, Action\Visit\ShortUrlVisitsAction::class => ConfigAbstractFactory::class, + Action\Visit\GlobalVisitsAction::class => ConfigAbstractFactory::class, Action\Tag\ListTagsAction::class => ConfigAbstractFactory::class, Action\Tag\DeleteTagsAction::class => ConfigAbstractFactory::class, Action\Tag\CreateTagsAction::class => ConfigAbstractFactory::class, @@ -66,6 +68,7 @@ return [ 'config.url_shortener.domain', ], Action\Visit\ShortUrlVisitsAction::class => [Service\VisitsTracker::class, 'Logger_Shlink'], + Action\Visit\GlobalVisitsAction::class => [Visit\VisitsStatsHelper::class, 'Logger_Shlink'], Action\ShortUrl\ListShortUrlsAction::class => [ Service\ShortUrlService::class, 'config.url_shortener.domain', diff --git a/module/Rest/config/routes.config.php b/module/Rest/config/routes.config.php index 7498d1b3..d2795971 100644 --- a/module/Rest/config/routes.config.php +++ b/module/Rest/config/routes.config.php @@ -27,6 +27,7 @@ return [ // Visits Action\Visit\ShortUrlVisitsAction::getRouteDef([$dropDomainMiddleware]), + Action\Visit\GlobalVisitsAction::getRouteDef(), // Tags Action\Tag\ListTagsAction::getRouteDef(), diff --git a/module/Rest/src/Action/Visit/GlobalVisitsAction.php b/module/Rest/src/Action/Visit/GlobalVisitsAction.php new file mode 100644 index 00000000..1946e222 --- /dev/null +++ b/module/Rest/src/Action/Visit/GlobalVisitsAction.php @@ -0,0 +1,33 @@ +statsHelper = $statsHelper; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + return new JsonResponse([ + 'visits' => $this->statsHelper->getVisitsStats(), + ]); + } +} diff --git a/module/Rest/test-api/Action/GlobalVisitsActionTest.php b/module/Rest/test-api/Action/GlobalVisitsActionTest.php new file mode 100644 index 00000000..8e4f5e11 --- /dev/null +++ b/module/Rest/test-api/Action/GlobalVisitsActionTest.php @@ -0,0 +1,21 @@ +callApiWithKey(self::METHOD_GET, '/visits'); + $payload = $this->getJsonResponsePayload($resp); + + $this->assertArrayHasKey('visits', $payload); + $this->assertArrayHasKey('visitsCount', $payload['visits']); + $this->assertEquals(7, $payload['visits']['visitsCount']); + } +} diff --git a/module/Rest/test-api/Action/GetVisitsActionTest.php b/module/Rest/test-api/Action/ShortUrlVisitsActionTest.php similarity index 97% rename from module/Rest/test-api/Action/GetVisitsActionTest.php rename to module/Rest/test-api/Action/ShortUrlVisitsActionTest.php index cee466a3..ea39a267 100644 --- a/module/Rest/test-api/Action/GetVisitsActionTest.php +++ b/module/Rest/test-api/Action/ShortUrlVisitsActionTest.php @@ -11,7 +11,7 @@ use ShlinkioApiTest\Shlink\Rest\Utils\NotFoundUrlHelpersTrait; use function GuzzleHttp\Psr7\build_query; use function sprintf; -class GetVisitsActionTest extends ApiTestCase +class ShortUrlVisitsActionTest extends ApiTestCase { use NotFoundUrlHelpersTrait;