Passed
Push — develop ( d93fae...fdcb02 )
by BENARD
04:10
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace VideoGamesRecords\CoreBundle\EventSubscriber\PlayerChart;
3
4
use Doctrine\ORM\EntityManagerInterface;
5
use Doctrine\ORM\Exception\ORMException;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use VideoGamesRecords\CoreBundle\Entity\Chart;
8
use VideoGamesRecords\CoreBundle\Entity\LostPosition;
9
use VideoGamesRecords\CoreBundle\Entity\Player;
10
use VideoGamesRecords\CoreBundle\Event\PlayerChartEvent;
11
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
12
13
final class InsertLostPositionSubscriber implements EventSubscriberInterface
14
{
15
    private EntityManagerInterface $em;
16
17
    public function __construct(EntityManagerInterface $em)
18
    {
19
        $this->em = $em;
20
    }
21
22
    public static function getSubscribedEvents(): array
23
    {
24
        return [
25
            VideoGamesRecordsCoreEvents::PLAYER_CHART_MAJ_COMPLETED => 'insertLostPosition',
26
        ];
27
    }
28
29
    /**
30
     * @param PlayerChartEvent $event
31
     * @throws ORMException
32
     */
33
    public function insertLostPosition(PlayerChartEvent $event): void
34
    {
35
        $playerChart = $event->getPlayerChart();
36
        $oldRank = $event->getOldRank();
37
        $oldNbEqual = $event->getOldNbEqual();
38
        $newRank = $playerChart->getRank();
39
        $newNbEqual = $playerChart->getNbEqual();
40
41
        if ((($oldRank >= 1) && ($oldRank <= 3) && ($newRank > $oldRank)) ||
42
            (($oldRank === 1) && ($oldNbEqual === 1) && ($newRank === 1) && ($newNbEqual > 1))
43
        ) {
44
            $lostPosition = new LostPosition();
45
            $lostPosition->setNewRank($newRank);
0 ignored issues
show
Bug introduced by
It seems like $newRank can also be of type null; however, parameter $newRank of VideoGamesRecords\CoreBu...tPosition::setNewRank() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            $lostPosition->setNewRank(/** @scrutinizer ignore-type */ $newRank);
Loading history...
46
            $lostPosition->setOldRank(($oldNbEqual == 1 && $oldRank == 1) ? 0 : $oldRank); //----- zero for losing platinum medal
0 ignored issues
show
Bug introduced by
It seems like $oldNbEqual == 1 && $oldRank == 1 ? 0 : $oldRank can also be of type null; however, parameter $oldRank of VideoGamesRecords\CoreBu...tPosition::setOldRank() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $lostPosition->setOldRank(/** @scrutinizer ignore-type */ ($oldNbEqual == 1 && $oldRank == 1) ? 0 : $oldRank); //----- zero for losing platinum medal
Loading history...
47
            $lostPosition->setPlayer($this->em->getReference(Player::class, $playerChart->getPlayer()->getId()));
0 ignored issues
show
Bug introduced by
It seems like $this->em->getReference(...->getPlayer()->getId()) can also be of type null; however, parameter $player of VideoGamesRecords\CoreBu...stPosition::setPlayer() does only seem to accept VideoGamesRecords\CoreBundle\Entity\Player, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
            $lostPosition->setPlayer(/** @scrutinizer ignore-type */ $this->em->getReference(Player::class, $playerChart->getPlayer()->getId()));
Loading history...
48
            $lostPosition->setChart($this->em->getReference(Chart::class, $playerChart->getChart()->getId()));
0 ignored issues
show
Bug introduced by
It seems like $this->em->getReference(...t->getChart()->getId()) can also be of type null; however, parameter $chart of VideoGamesRecords\CoreBu...ostPosition::setChart() does only seem to accept VideoGamesRecords\CoreBundle\Entity\Chart, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            $lostPosition->setChart(/** @scrutinizer ignore-type */ $this->em->getReference(Chart::class, $playerChart->getChart()->getId()));
Loading history...
49
            $this->em->persist($lostPosition);
50
        }
51
    }
52
}
53