2016-04-30 20:18:42 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-19 19:01:39 +03:00
|
|
|
namespace Shlinkio\Shlink\Core\Service;
|
2016-04-30 20:18:42 +03:00
|
|
|
|
2017-12-27 19:22:51 +03:00
|
|
|
use Doctrine\ORM;
|
2016-08-09 09:52:06 +03:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2016-07-20 11:13:53 +03:00
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2016-07-21 10:36:38 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2016-07-19 19:01:39 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2016-07-21 10:36:38 +03:00
|
|
|
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
2016-04-30 20:18:42 +03:00
|
|
|
|
|
|
|
class VisitsTracker implements VisitsTrackerInterface
|
|
|
|
{
|
|
|
|
/**
|
2017-12-27 19:22:51 +03:00
|
|
|
* @var ORM\EntityManagerInterface
|
2016-04-30 20:18:42 +03:00
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
2017-12-27 19:22:51 +03:00
|
|
|
public function __construct(ORM\EntityManagerInterface $em)
|
2016-04-30 20:18:42 +03:00
|
|
|
{
|
|
|
|
$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 09:52:06 +03:00
|
|
|
* @param ServerRequestInterface $request
|
2017-12-27 19:22:51 +03:00
|
|
|
* @throws ORM\ORMInvalidArgumentException
|
|
|
|
* @throws ORM\OptimisticLockException
|
2016-04-30 20:18:42 +03:00
|
|
|
*/
|
2016-08-09 09:52:06 +03:00
|
|
|
public function track($shortCode, ServerRequestInterface $request)
|
2016-04-30 20:18:42 +03:00
|
|
|
{
|
|
|
|
/** @var ShortUrl $shortUrl */
|
|
|
|
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$visit = new Visit();
|
|
|
|
$visit->setShortUrl($shortUrl)
|
2016-08-09 10:13:39 +03:00
|
|
|
->setUserAgent($request->getHeaderLine('User-Agent'))
|
|
|
|
->setReferer($request->getHeaderLine('Referer'))
|
|
|
|
->setRemoteAddr($this->findOutRemoteAddr($request));
|
2017-10-22 10:00:32 +03:00
|
|
|
|
2017-12-27 19:22:51 +03:00
|
|
|
/** @var ORM\EntityManager $em */
|
|
|
|
$em = $this->em;
|
|
|
|
$em->persist($visit);
|
|
|
|
$em->flush($visit);
|
2016-04-30 20:18:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-09 10:13:39 +03:00
|
|
|
* @param ServerRequestInterface $request
|
2017-10-22 10:00:32 +03:00
|
|
|
* @return string|null
|
2016-04-30 20:18:42 +03:00
|
|
|
*/
|
2017-10-22 10:00:32 +03:00
|
|
|
private function findOutRemoteAddr(ServerRequestInterface $request)
|
2016-04-30 20:18:42 +03:00
|
|
|
{
|
2016-08-09 10:13:39 +03:00
|
|
|
$forwardedFor = $request->getHeaderLine('X-Forwarded-For');
|
|
|
|
if (empty($forwardedFor)) {
|
|
|
|
$serverParams = $request->getServerParams();
|
2017-10-22 10:00:32 +03:00
|
|
|
return $serverParams['REMOTE_ADDR'] ?? null;
|
2016-08-09 10:13:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$ips = explode(',', $forwardedFor);
|
2017-10-22 10:00:32 +03:00
|
|
|
return $ips[0] ?? null;
|
2016-04-30 20:18:42 +03:00
|
|
|
}
|
2016-06-12 22:51:06 +03:00
|
|
|
|
|
|
|
/**
|
2016-07-21 10:36:38 +03:00
|
|
|
* Returns the visits on certain short code
|
2016-06-12 22:51:06 +03:00
|
|
|
*
|
2017-12-27 18:23:54 +03:00
|
|
|
* @param string $shortCode
|
2016-07-21 10:36:38 +03:00
|
|
|
* @param DateRange $dateRange
|
|
|
|
* @return Visit[]
|
2017-10-22 10:00:32 +03:00
|
|
|
* @throws InvalidArgumentException
|
2016-06-12 22:51:06 +03:00
|
|
|
*/
|
2017-12-27 18:23:54 +03:00
|
|
|
public function info(string $shortCode, DateRange $dateRange = null): array
|
2016-06-12 22:51:06 +03:00
|
|
|
{
|
2017-12-27 18:23:54 +03:00
|
|
|
/** @var ShortUrl|null $shortUrl */
|
2016-06-12 22:51:06 +03:00
|
|
|
$shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
2017-10-22 10:00:32 +03:00
|
|
|
if ($shortUrl === null) {
|
2016-06-12 22:51:06 +03:00
|
|
|
throw new InvalidArgumentException(sprintf('Short code "%s" not found', $shortCode));
|
|
|
|
}
|
|
|
|
|
2016-07-21 10:36:38 +03:00
|
|
|
/** @var VisitRepository $repo */
|
|
|
|
$repo = $this->em->getRepository(Visit::class);
|
|
|
|
return $repo->findVisitsByShortUrl($shortUrl, $dateRange);
|
2016-06-12 22:51:06 +03:00
|
|
|
}
|
2016-04-30 20:18:42 +03:00
|
|
|
}
|