getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\EventSubscriber;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Exception\ORMException;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use VideoGamesRecords\CoreBundle\Entity\Chart;
11
use VideoGamesRecords\CoreBundle\Entity\LostPosition;
12
use VideoGamesRecords\CoreBundle\Entity\Player;
13
use VideoGamesRecords\CoreBundle\Event\PlayerChartEvent;
14
use VideoGamesRecords\CoreBundle\VideoGamesRecordsCoreEvents;
15
16
final class InsertLostPositionSubscriber implements EventSubscriberInterface
17
{
18
    private EntityManagerInterface $em;
19
20
    public function __construct(EntityManagerInterface $em)
21
    {
22
        $this->em = $em;
23
    }
24
25
    public static function getSubscribedEvents(): array
26
    {
27
        return [
28
            VideoGamesRecordsCoreEvents::PLAYER_CHART_MAJ_COMPLETED => 'insertLostPosition',
29
        ];
30
    }
31
32
    /**
33
     * @param PlayerChartEvent $event
34
     * @throws ORMException
35
     */
36
    public function insertLostPosition(PlayerChartEvent $event): void
37
    {
38
        $playerChart = $event->getPlayerChart();
39
        $oldRank = $event->getOldRank();
40
        $oldNbEqual = $event->getOldNbEqual();
41
        $newRank = $playerChart->getRank();
42
        $newNbEqual = $playerChart->getNbEqual();
43
44
        if (
45
            (($oldRank >= 1) && ($oldRank <= 3) && ($newRank > $oldRank)) ||
46
            (($oldRank === 1) && ($oldNbEqual === 1) && ($newRank === 1) && ($newNbEqual > 1))
47
        ) {
48
            $lostPosition = new LostPosition();
49
            $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

49
            $lostPosition->setNewRank(/** @scrutinizer ignore-type */ $newRank);
Loading history...
50
            $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

50
            $lostPosition->setOldRank(/** @scrutinizer ignore-type */ ($oldNbEqual == 1 && $oldRank == 1) ? 0 : $oldRank); //----- zero for losing platinum medal
Loading history...
51
            $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

51
            $lostPosition->setPlayer(/** @scrutinizer ignore-type */ $this->em->getReference(Player::class, $playerChart->getPlayer()->getId()));
Loading history...
52
            $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

52
            $lostPosition->setChart(/** @scrutinizer ignore-type */ $this->em->getReference(Chart::class, $playerChart->getChart()->getId()));
Loading history...
53
            $this->em->persist($lostPosition);
54
        }
55
    }
56
}
57