Completed
Push — master ( 18696f...4ea9b4 )
by Serhii
04:47 queued 11s
created

AddSeasonSubscriber::onFlush()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 47
    public function getSubscribedEvents()
15
    {
16
        return [
17 47
            Events::onFlush
18
        ];
19
    }
20
21 11
    public function onFlush(OnFlushEventArgs $args)
22
    {
23 11
        $em = $args->getEntityManager();
24 11
        $uow = $em->getUnitOfWork();
25
26 11
        foreach ($uow->getScheduledEntityInsertions() as $keyEntity => $entity) {
27 1
            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 11
    }
37
}
38