AddSeasonSubscriber::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App\EventListener;
4
5
use App\Entity\PerformanceEvent;
6
use App\Entity\RepertoireSeason;
7
use App\Repository\RepertoireSeasonRepository;
8
use Doctrine\Common\EventSubscriber;
9
use Doctrine\ORM\Event\OnFlushEventArgs;
10
use Doctrine\ORM\Events;
11
12
class AddSeasonSubscriber implements EventSubscriber
13
{
14 48
    public function getSubscribedEvents()
15
    {
16
        return [
17 48
            Events::onFlush
18
        ];
19
    }
20
21 12
    public function onFlush(OnFlushEventArgs $args)
22
    {
23 12
        $em = $args->getEntityManager();
24 12
        $uow = $em->getUnitOfWork();
25
26 12
        foreach ($uow->getScheduledEntityInsertions() as $keyEntity => $entity) {
27 2
            if ($entity instanceof PerformanceEvent) {
28 1
                $performance = $entity->getPerformance();
29 1
                $currentSeason = $em->getRepository(RepertoireSeason::class)
30 1
                    ->findSeasonByDate($entity->getDateTime());
31 1
                $performance->addSeason($currentSeason);
32 1
                $classMetadata = $em->getClassMetadata(get_class($performance));
33 1
                $uow->computeChangeSet($classMetadata, $performance);
34
            }
35
        }
36 12
    }
37
}
38