Completed
Push — master ( 18696f...4ea9b4 )
by Serhii
04:47 queued 11s
created

RepertoireSeasonRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\RepertoireSeason;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
9
/**
10
 * @method RepertoireSeason|null find($id, $lockMode = null, $lockVersion = null)
11
 * @method RepertoireSeason|null findOneBy(array $criteria, array $orderBy = null)
12
 * @method RepertoireSeason[]    findAll()
13
 * @method RepertoireSeason[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
14
 */
15
class RepertoireSeasonRepository extends ServiceEntityRepository
16
{
17
    const MULTI_SEASON = -1;
18
    const CURRENT_SEASON = 0;
19
    private $performanceRepository;
20
21 6
    public function __construct(ManagerRegistry $registry, PerformanceRepository $performanceRepository)
22
    {
23 6
        parent::__construct($registry, RepertoireSeason::class);
24 6
        $this->performanceRepository = $performanceRepository;
25 6
    }
26
27
    /**
28
     * @return array|RepertoireSeason[]
29
     */
30 1
    public function findAllNotEmpty(): array
31
    {
32 1
        $result = $this->createQueryBuilder('r')
33 1
            ->addSelect('COUNT(rp.id) as performanceCount')
34 1
            ->leftJoin('r.performances', 'rp')
35 1
            ->addGroupBy('r.id')
36 1
            ->having('performanceCount > 0')
37 1
            ->orderBy('r.number', 'DESC')
38 1
            ->getQuery()
39 1
            ->execute()
40
        ;
41
42 1
        if (!$result || false === is_array($result)) return [];
43
44
        // todo: Use ResultSetMapping instead
45
        return array_map(function(array $el) {
46 1
            $el[0]->performanceCount = $el['performanceCount'];
47 1
            return $el[0];
48 1
        }, $result);
49
    }
50
51 2
    public function findSeasonByDate(\DateTime $dateTime): ?RepertoireSeason
52
    {
53 2
        $season = $this->createQueryBuilder('rs')
54 2
            ->where('rs.startDate < :startDate')
55 2
            ->andWhere('rs.endDate > :endDate')
56 2
            ->setParameter('startDate', $dateTime)
57 2
            ->setParameter('endDate', $dateTime)
58 2
            ->setMaxResults(1)
59 2
            ->getQuery()
60 2
            ->getSingleResult();
61
62 2
        if ($season) return $season;
63
64
        $season = $this->createQueryBuilder('rs')
65
            ->where('rs.startDate > :startDate')
66
            ->setParameter('startDate', $dateTime)
67
            ->orderBy('rs.startDate', 'ASC')
68
            ->setMaxResults(1)
69
            ->getQuery()
70
            ->getSingleResult();
71
72
        return $season;
73
    }
74
75 1
    public function findCurrentSeason(): ?RepertoireSeason
76
    {
77 1
        $season = $this->findSeasonByDate(new \DateTime());
78 1
        if ($season) return $season;
79
80
        $all = $this->findAllNotEmpty();
81
        if (empty($all)) return null;
82
        return array_shift($all);
83
    }
84
85
    public function getMultiSeason(): RepertoireSeason
86
    {
87
        $multiSeason = new RepertoireSeason();
88
        $multiSeason->setNumber(self::MULTI_SEASON);
89
        $multiSeason->setPerformances($this->performanceRepository->findAllWithinSeasons());
90
91
        return $multiSeason;
92
    }
93
94
    public function findOneByNumber($number): ?RepertoireSeason
95
    {
96
        if (self::CURRENT_SEASON == $number) return $this->findCurrentSeason();
97
        if (self::MULTI_SEASON == $number) return $this->getMultiSeason();
98
99
        return $this->findOneBy(['number' => $number]);
100
    }
101
}
102