Updated VisitsTracker so that the track method expects a Request object to be provided

This commit is contained in:
Alejandro Celaya 2016-08-09 08:52:06 +02:00
parent 34753ca7d3
commit 73a236b3d0
4 changed files with 11 additions and 7 deletions

View file

@ -85,7 +85,7 @@ class RedirectAction implements MiddlewareInterface
} }
// Track visit to this short code // Track visit to this short code
$this->visitTracker->track($shortCode); $this->visitTracker->track($shortCode, $request);
// Return a redirect response to the long URL. // Return a redirect response to the long URL.
// Use a temporary redirect to make sure browsers always hit the server for analytics purposes // Use a temporary redirect to make sure browsers always hit the server for analytics purposes

View file

@ -3,6 +3,7 @@ namespace Shlinkio\Shlink\Core\Service;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\ShortUrl; 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 * Tracks a new visit to provided short code, using an array of data to look up information
* *
* @param string $shortCode * @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 */ /** @var ShortUrl $shortUrl */
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([ $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([

View file

@ -1,6 +1,7 @@
<?php <?php
namespace Shlinkio\Shlink\Core\Service; namespace Shlinkio\Shlink\Core\Service;
use Psr\Http\Message\ServerRequestInterface;
use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
@ -10,9 +11,10 @@ interface VisitsTrackerInterface
* Tracks a new visit to provided short code, using an array of data to look up information * Tracks a new visit to provided short code, using an array of data to look up information
* *
* @param string $shortCode * @param string $shortCode
* @param array $visitorData Defaults to global $_SERVER * @param ServerRequestInterface $request
* @return
*/ */
public function track($shortCode, array $visitorData = null); public function track($shortCode, ServerRequestInterface $request);
/** /**
* Returns the visits on certain short code * Returns the visits on certain short code

View file

@ -10,6 +10,7 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Repository\VisitRepository; use Shlinkio\Shlink\Core\Repository\VisitRepository;
use Shlinkio\Shlink\Core\Service\VisitsTracker; use Shlinkio\Shlink\Core\Service\VisitsTracker;
use Zend\Diactoros\ServerRequestFactory;
class VisitsTrackerTest extends TestCase class VisitsTrackerTest extends TestCase
{ {
@ -41,7 +42,7 @@ class VisitsTrackerTest extends TestCase
$this->em->persist(Argument::any())->shouldBeCalledTimes(1); $this->em->persist(Argument::any())->shouldBeCalledTimes(1);
$this->em->flush()->shouldBeCalledTimes(1); $this->em->flush()->shouldBeCalledTimes(1);
$this->visitsTracker->track($shortCode); $this->visitsTracker->track($shortCode, ServerRequestFactory::fromGlobals());
} }
/** /**