From 73a236b3d02c0ed92ac3fa6a5c47c2a4bcec8264 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 9 Aug 2016 08:52:06 +0200 Subject: [PATCH] Updated VisitsTracker so that the track method expects a Request object to be provided --- module/Core/src/Action/RedirectAction.php | 2 +- module/Core/src/Service/VisitsTracker.php | 7 ++++--- module/Core/src/Service/VisitsTrackerInterface.php | 6 ++++-- module/Core/test/Service/VisitsTrackerTest.php | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/module/Core/src/Action/RedirectAction.php b/module/Core/src/Action/RedirectAction.php index 8aa3ddb7..a86a98c4 100644 --- a/module/Core/src/Action/RedirectAction.php +++ b/module/Core/src/Action/RedirectAction.php @@ -85,7 +85,7 @@ class RedirectAction implements MiddlewareInterface } // Track visit to this short code - $this->visitTracker->track($shortCode); + $this->visitTracker->track($shortCode, $request); // Return a redirect response to the long URL. // Use a temporary redirect to make sure browsers always hit the server for analytics purposes diff --git a/module/Core/src/Service/VisitsTracker.php b/module/Core/src/Service/VisitsTracker.php index 87187aad..6ae38e83 100644 --- a/module/Core/src/Service/VisitsTracker.php +++ b/module/Core/src/Service/VisitsTracker.php @@ -3,6 +3,7 @@ namespace Shlinkio\Shlink\Core\Service; use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Doctrine\ORM\EntityManagerInterface; +use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Core\Entity\ShortUrl; @@ -31,11 +32,11 @@ class VisitsTracker implements VisitsTrackerInterface * Tracks a new visit to provided short code, using an array of data to look up information * * @param string $shortCode - * @param array $visitorData Defaults to global $_SERVER + * @param ServerRequestInterface $request */ - public function track($shortCode, array $visitorData = null) + public function track($shortCode, ServerRequestInterface $request) { - $visitorData = $visitorData ?: $_SERVER; + $visitorData = $request->getServerParams(); /** @var ShortUrl $shortUrl */ $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([ diff --git a/module/Core/src/Service/VisitsTrackerInterface.php b/module/Core/src/Service/VisitsTrackerInterface.php index cec254d3..6aecabe4 100644 --- a/module/Core/src/Service/VisitsTrackerInterface.php +++ b/module/Core/src/Service/VisitsTrackerInterface.php @@ -1,6 +1,7 @@ em->persist(Argument::any())->shouldBeCalledTimes(1); $this->em->flush()->shouldBeCalledTimes(1); - $this->visitsTracker->track($shortCode); + $this->visitsTracker->track($shortCode, ServerRequestFactory::fromGlobals()); } /**