Completed
Pull Request — master (#168)
by Serhii
04:34
created

RepertoireSeasonRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 56.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 60
ccs 17
cts 30
cp 0.5667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findAllNotEmpty() 0 20 3
A findCurrentSeason() 0 6 2
A getMultiSeason() 0 8 1
A findOneByNumber() 0 7 3
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 4
    public function __construct(ManagerRegistry $registry, PerformanceRepository $performanceRepository)
22
    {
23 4
        parent::__construct($registry, RepertoireSeason::class);
24 4
        $this->performanceRepository = $performanceRepository;
25 4
    }
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
    public function findCurrentSeason(): ?RepertoireSeason
52
    {
53
        $all = $this->findAllNotEmpty();
54
        if (empty($all)) return null;
55
        return array_shift($all);
56
    }
57
58
    public function getMultiSeason(): RepertoireSeason
59
    {
60
        $multiSeason = new RepertoireSeason();
61
        $multiSeason->setNumber(self::MULTI_SEASON);
62
        $multiSeason->setPerformances($this->performanceRepository->findAllWithinSeasons());
63
64
        return $multiSeason;
65
    }
66
67
    public function findOneByNumber($number): ?RepertoireSeason
68
    {
69
        if (self::CURRENT_SEASON == $number) return $this->findCurrentSeason();
70
        if (self::MULTI_SEASON == $number) return $this->getMultiSeason();
71
72
        return $this->findOneBy(['number' => $number]);
73
    }
74
}
75