2016-04-30 19:18:42 +02:00
|
|
|
<?php
|
2016-07-19 18:01:39 +02:00
|
|
|
namespace Shlinkio\Shlink\Core\Service;
|
2016-04-30 19:18:42 +02:00
|
|
|
|
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2016-08-09 08:52:06 +02:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2016-07-20 10:13:53 +02:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2016-07-21 09:36:38 +02:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2016-07-19 18:01:39 +02:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2016-07-21 09:36:38 +02:00
|
|
|
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
2016-04-30 19:18:42 +02:00
|
|
|
|
|
|
|
class VisitsTracker implements VisitsTrackerInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var EntityManagerInterface
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VisitsTracker constructor.
|
|
|
|
* @param EntityManagerInterface $em
|
|
|
|
*
|
|
|
|
* @Inject({"em"})
|
|
|
|
*/
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks a new visit to provided short code, using an array of data to look up information
|
|
|
|
*
|
|
|
|
* @param string $shortCode
|
2016-08-09 08:52:06 +02:00
|
|
|
* @param ServerRequestInterface $request
|
2016-04-30 19:18:42 +02:00
|
|
|
*/
|
2016-08-09 08:52:06 +02:00
|
|
|
public function track($shortCode, ServerRequestInterface $request)
|
2016-04-30 19:18:42 +02:00
|
|
|
{
|
2016-08-09 08:52:06 +02:00
|
|
|
$visitorData = $request->getServerParams();
|
2016-04-30 19:18:42 +02:00
|
|
|
|
|
|
|
/** @var ShortUrl $shortUrl */
|
|
|
|
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$visit = new Visit();
|
|
|
|
$visit->setShortUrl($shortUrl)
|
|
|
|
->setUserAgent($this->getArrayValue($visitorData, 'HTTP_USER_AGENT'))
|
2016-05-01 17:54:56 +02:00
|
|
|
->setReferer($this->getArrayValue($visitorData, 'HTTP_REFERER'))
|
2016-04-30 19:18:42 +02:00
|
|
|
->setRemoteAddr($this->getArrayValue($visitorData, 'REMOTE_ADDR'));
|
|
|
|
$this->em->persist($visit);
|
|
|
|
$this->em->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $array
|
|
|
|
* @param $key
|
|
|
|
* @param null $default
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
protected function getArrayValue(array $array, $key, $default = null)
|
|
|
|
{
|
|
|
|
return isset($array[$key]) ? $array[$key] : $default;
|
|
|
|
}
|
2016-06-12 21:51:06 +02:00
|
|
|
|
|
|
|
/**
|
2016-07-21 09:36:38 +02:00
|
|
|
* Returns the visits on certain short code
|
2016-06-12 21:51:06 +02:00
|
|
|
*
|
|
|
|
* @param $shortCode
|
2016-07-21 09:36:38 +02:00
|
|
|
* @param DateRange $dateRange
|
|
|
|
* @return Visit[]
|
2016-06-12 21:51:06 +02:00
|
|
|
*/
|
2016-07-21 09:36:38 +02:00
|
|
|
public function info($shortCode, DateRange $dateRange = null)
|
2016-06-12 21:51:06 +02:00
|
|
|
{
|
|
|
|
/** @var ShortUrl $shortUrl */
|
|
|
|
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
|
|
|
if (! isset($shortUrl)) {
|
|
|
|
throw new InvalidArgumentException(sprintf('Short code "%s" not found', $shortCode));
|
|
|
|
}
|
|
|
|
|
2016-07-21 09:36:38 +02:00
|
|
|
/** @var VisitRepository $repo */
|
|
|
|
$repo = $this->em->getRepository(Visit::class);
|
|
|
|
return $repo->findVisitsByShortUrl($shortUrl, $dateRange);
|
2016-06-12 21:51:06 +02:00
|
|
|
}
|
2016-04-30 19:18:42 +02:00
|
|
|
}
|