Completed
Pull Request — master (#168)
by Serhii
02:19
created

RepertoireSeasonRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 69.57%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 74
ccs 16
cts 23
cp 0.6957
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findAllNotEmpty() 0 20 3
A findCurrentSeason() 0 6 2
A findOneByNumber() 0 6 2
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
use Doctrine\ORM\Query\ResultSetMappingBuilder;
9
10
/**
11
 * @method RepertoireSeason|null find($id, $lockMode = null, $lockVersion = null)
12
 * @method RepertoireSeason|null findOneBy(array $criteria, array $orderBy = null)
13
 * @method RepertoireSeason[]    findAll()
14
 * @method RepertoireSeason[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15
 */
16
class RepertoireSeasonRepository extends ServiceEntityRepository
17
{
18 4
    public function __construct(ManagerRegistry $registry)
19
    {
20 4
        parent::__construct($registry, RepertoireSeason::class);
21 4
    }
22
23
    /**
24
     * @return array|RepertoireSeason[]
25
     */
26 1
    public function findAllNotEmpty(): array
27
    {
28 1
        $result = $this->createQueryBuilder('r')
29 1
            ->addSelect('COUNT(rp.id) as performanceCount')
30 1
            ->leftJoin('r.performances', 'rp')
31 1
            ->addGroupBy('r.id')
32 1
            ->having('performanceCount > 0')
33 1
            ->orderBy('r.number', 'DESC')
34 1
            ->getQuery()
35 1
            ->execute()
36
        ;
37
38 1
        if (!$result || false === is_array($result)) return [];
39
40
        // todo: Use ResultSetMapping instead
41
        return array_map(function(array $el) {
42 1
            $el[0]->performanceCount = $el['performanceCount'];
43 1
            return $el[0];
44 1
        }, $result);
45
    }
46
47
    public function findCurrentSeason(): ?RepertoireSeason
48
    {
49
        $all = $this->findAllNotEmpty();
50
        if (empty($all)) return null;
51
        return array_shift($all);
52
    }
53
54
    public function findOneByNumber($number)
55
    {
56
        if ('current' === $number) return $this->findCurrentSeason();
57
58
        return $this->findOneBy(['number' => $number]);
59
    }
60
61
    // /**
62
    //  * @return RepertoireSeason[] Returns an array of RepertoireSeason objects
63
    //  */
64
    /*
65
    public function findByExampleField($value)
66
    {
67
        return $this->createQueryBuilder('r')
68
            ->andWhere('r.exampleField = :val')
69
            ->setParameter('val', $value)
70
            ->orderBy('r.id', 'ASC')
71
            ->setMaxResults(10)
72
            ->getQuery()
73
            ->getResult()
74
        ;
75
    }
76
    */
77
78
    /*
79
    public function findOneBySomeField($value): ?RepertoireSeason
80
    {
81
        return $this->createQueryBuilder('r')
82
            ->andWhere('r.exampleField = :val')
83
            ->setParameter('val', $value)
84
            ->getQuery()
85
            ->getOneOrNullResult()
86
        ;
87
    }
88
    */
89
}
90