RepertoireSeasonRepository::findOneByNumber()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
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
            ->enableResultCache(60*60*24)
40 1
            ->execute()
41
        ;
42
43 1
        if (!$result || false === is_array($result)) return [];
44
45
        // todo: Use ResultSetMapping instead
46
        return array_map(function(array $el) {
47 1
            $el[0]->performanceCount = $el['performanceCount'];
48 1
            return $el[0];
49 1
        }, $result);
50
    }
51
52 2
    public function findSeasonByDate(\DateTime $dateTime): ?RepertoireSeason
53
    {
54 2
        $season = $this->createQueryBuilder('rs')
55 2
            ->where('rs.startDate < :startDate')
56 2
            ->andWhere('rs.endDate > :endDate')
57 2
            ->setParameter('startDate', $dateTime)
58 2
            ->setParameter('endDate', $dateTime)
59 2
            ->setMaxResults(1)
60 2
            ->getQuery()
61 2
            ->enableResultCache(60*60*24)
62 2
            ->getSingleResult();
63
64 2
        if ($season) return $season;
65
66
        $season = $this->createQueryBuilder('rs')
67
            ->where('rs.startDate > :startDate')
68
            ->setParameter('startDate', $dateTime)
69
            ->orderBy('rs.startDate', 'ASC')
70
            ->setMaxResults(1)
71
            ->getQuery()
72
            ->enableResultCache(60*60*24)
73
            ->getSingleResult();
74
75
        return $season;
76
    }
77
78 1
    public function findCurrentSeason(): ?RepertoireSeason
79
    {
80 1
        $season = $this->findSeasonByDate(new \DateTime());
81 1
        if ($season) return $season;
82
83
        $all = $this->findAllNotEmpty();
84
        if (empty($all)) return null;
85
        return array_shift($all);
86
    }
87
88
    public function getMultiSeason(): RepertoireSeason
89
    {
90
        $multiSeason = new RepertoireSeason();
91
        $multiSeason->setNumber(self::MULTI_SEASON);
92
        $multiSeason->setPerformances($this->performanceRepository->findAllWithinSeasons());
93
94
        return $multiSeason;
95
    }
96
97
    public function findOneByNumber($number): ?RepertoireSeason
98
    {
99
        if (self::CURRENT_SEASON == $number) return $this->findCurrentSeason();
100
        if (self::MULTI_SEASON == $number) return $this->getMultiSeason();
101
102
        return $this->findOneBy(['number' => $number]);
103
    }
104
}
105