1 | <?php |
||
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) |
|
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 | ]); |
||
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) |
|
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) |
|
90 | } |
||
91 |