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
|
|
|
|