Passed
Pull Request — master (#553)
by Alejandro
07:03
created

VisitsTracker   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 52
rs 10
ccs 18
cts 19
cp 0.9474
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A track() 0 13 1
A info() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core\Service;
6
7
use Doctrine\ORM;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Shlinkio\Shlink\Core\Entity\ShortUrl;
10
use Shlinkio\Shlink\Core\Entity\Visit;
11
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited;
12
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
13
use Shlinkio\Shlink\Core\Model\Visitor;
14
use Shlinkio\Shlink\Core\Model\VisitsParams;
15
use Shlinkio\Shlink\Core\Paginator\Adapter\VisitsPaginatorAdapter;
16
use Shlinkio\Shlink\Core\Repository\VisitRepository;
17
use Zend\Paginator\Paginator;
18
19
use function sprintf;
20
21
class VisitsTracker implements VisitsTrackerInterface
22
{
23
    /** @var ORM\EntityManagerInterface */
24
    private $em;
25
    /** @var EventDispatcherInterface */
26
    private $eventDispatcher;
27
28 3
    public function __construct(ORM\EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher)
29
    {
30 3
        $this->em = $em;
31 3
        $this->eventDispatcher = $eventDispatcher;
32
    }
33
34
    /**
35
     * Tracks a new visit to provided short code from provided visitor
36
     */
37 2
    public function track(string $shortCode, Visitor $visitor): void
38
    {
39
        /** @var ShortUrl $shortUrl */
40 2
        $shortUrl = $this->em->getRepository(ShortUrl::class)->findOneBy([
41 2
            'shortCode' => $shortCode,
42
        ]);
43
44 2
        $visit = new Visit($shortUrl, $visitor);
45
46 2
        $this->em->persist($visit);
47 2
        $this->em->flush();
48
49 2
        $this->eventDispatcher->dispatch(new ShortUrlVisited($visit->getId()));
50
    }
51
52
    /**
53
     * Returns the visits on certain short code
54
     *
55
     * @return Visit[]|Paginator
56
     * @throws InvalidArgumentException
57
     */
58 1
    public function info(string $shortCode, VisitsParams $params): Paginator
59
    {
60
        /** @var ORM\EntityRepository $repo */
61 1
        $repo = $this->em->getRepository(ShortUrl::class);
62 1
        if ($repo->count(['shortCode' => $shortCode]) < 1) {
63
            throw new InvalidArgumentException(sprintf('Short code "%s" not found', $shortCode));
64
        }
65
66
        /** @var VisitRepository $repo */
67 1
        $repo = $this->em->getRepository(Visit::class);
68 1
        $paginator = new Paginator(new VisitsPaginatorAdapter($repo, $shortCode, $params));
69 1
        $paginator->setItemCountPerPage($params->getItemsPerPage())
70 1
                  ->setCurrentPageNumber($params->getPage());
71
72 1
        return $paginator;
73
    }
74
}
75