Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

VisitsTracker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
namespace Shlinkio\Shlink\Core\Service;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Doctrine\ORM\EntityManagerInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
8
use Shlinkio\Shlink\Common\Util\DateRange;
9
use Shlinkio\Shlink\Core\Entity\ShortUrl;
10
use Shlinkio\Shlink\Core\Entity\Visit;
11
use Shlinkio\Shlink\Core\Repository\VisitRepository;
12
13
class VisitsTracker implements VisitsTrackerInterface
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $em;
19
20
    /**
21
     * VisitsTracker constructor.
22
     * @param EntityManagerInterface $em
23
     *
24
     * @Inject({"em"})
25
     */
26 3
    public function __construct(EntityManagerInterface $em)
27
    {
28 3
        $this->em = $em;
29 3
    }
30
31
    /**
32
     * Tracks a new visit to provided short code, using an array of data to look up information
33
     *
34
     * @param string $shortCode
35
     * @param ServerRequestInterface $request
36
     */
37 2
    public function track($shortCode, ServerRequestInterface $request)
38
    {
39
        /** @var ShortUrl $shortUrl */
40 2
        $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
41 2
            'shortCode' => $shortCode,
42 2
        ]);
43
44 2
        $visit = new Visit();
45 2
        $visit->setShortUrl($shortUrl)
46 2
              ->setUserAgent($request->getHeaderLine('User-Agent'))
47 2
              ->setReferer($request->getHeaderLine('Referer'))
48 2
              ->setRemoteAddr($this->findOutRemoteAddr($request));
49 2
        $this->em->persist($visit);
50 2
        $this->em->flush();
51 2
    }
52
53
    /**
54
     * @param ServerRequestInterface $request
55
     * @return string
56
     */
57 2
    protected function findOutRemoteAddr(ServerRequestInterface $request)
58
    {
59 2
        $forwardedFor = $request->getHeaderLine('X-Forwarded-For');
60 2
        if (empty($forwardedFor)) {
61 1
            $serverParams = $request->getServerParams();
62 1
            return isset($serverParams['REMOTE_ADDR']) ? $serverParams['REMOTE_ADDR'] : null;
63
        }
64
65 1
        $ips = explode(',', $forwardedFor);
66 1
        return $ips[0];
67
    }
68
69
    /**
70
     * Returns the visits on certain short code
71
     *
72
     * @param $shortCode
73
     * @param DateRange $dateRange
74
     * @return Visit[]
75
     */
76 1
    public function info($shortCode, DateRange $dateRange = null)
77
    {
78
        /** @var ShortUrl $shortUrl */
79 1
        $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
80 1
            'shortCode' => $shortCode,
81 1
        ]);
82 1
        if (! isset($shortUrl)) {
83
            throw new InvalidArgumentException(sprintf('Short code "%s" not found', $shortCode));
84
        }
85
86
        /** @var VisitRepository $repo */
87 1
        $repo = $this->em->getRepository(Visit::class);
88 1
        return $repo->findVisitsByShortUrl($shortUrl, $dateRange);
89
    }
90
}
91