Completed
Push — master ( ccb9d5...d7e68d )
by Alejandro
09:04
created

VisitRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
rs 10
c 1
b 0
f 0
ccs 20
cts 21
cp 0.9524
wmc 8
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findUnlocatedVisits() 0 7 1
C findVisitsByShortUrl() 0 28 7
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Core\Repository;
5
6
use Doctrine\ORM\EntityRepository;
7
use Shlinkio\Shlink\Common\Util\DateRange;
8
use Shlinkio\Shlink\Core\Entity\ShortUrl;
9
use Shlinkio\Shlink\Core\Entity\Visit;
10
11
class VisitRepository extends EntityRepository implements VisitRepositoryInterface
12
{
13
    /**
14
     * @return Visit[]
15
     */
16 1
    public function findUnlocatedVisits(): array
17
    {
18 1
        $qb = $this->createQueryBuilder('v');
19 1
        $qb->where($qb->expr()->isNull('v.visitLocation'));
20
21 1
        return $qb->getQuery()->getResult();
22
    }
23
24
    /**
25
     * @param ShortUrl|int $shortUrlOrId
26
     * @param DateRange|null $dateRange
27
     * @return Visit[]
28
     */
29 1
    public function findVisitsByShortUrl($shortUrlOrId, DateRange $dateRange = null): array
30
    {
31
        /** @var ShortUrl|null $shortUrl */
32 1
        $shortUrl = $shortUrlOrId instanceof ShortUrl
33
            ? $shortUrlOrId
34 1
            : $this->getEntityManager()->find(ShortUrl::class, $shortUrlOrId);
35
36 1
        if ($shortUrl === null) {
37 1
            return [];
38
        }
39
40 1
        $qb = $this->createQueryBuilder('v');
41 1
        $qb->where($qb->expr()->eq('v.shortUrl', ':shortUrl'))
42 1
           ->setParameter('shortUrl', $shortUrl)
43 1
           ->orderBy('v.date', 'DESC') ;
44
45
        // Apply date range filtering
46 1
        if ($dateRange !== null && $dateRange->getStartDate() !== null) {
47 1
            $qb->andWhere($qb->expr()->gte('v.date', ':startDate'))
48 1
               ->setParameter('startDate', $dateRange->getStartDate());
49
        }
50 1
        if ($dateRange !== null && $dateRange->getEndDate() !== null) {
51 1
            $qb->andWhere($qb->expr()->lte('v.date', ':endDate'))
52 1
               ->setParameter('endDate', $dateRange->getEndDate());
53
        }
54
55 1
        return $qb->getQuery()->getResult();
56
    }
57
}
58