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); |
|
|
|
|
50
|
|
|
$lostPosition->setOldRank(($oldNbEqual == 1 && $oldRank == 1) ? 0 : $oldRank); //----- zero for losing platinum medal |
|
|
|
|
51
|
|
|
$lostPosition->setPlayer($this->em->getReference(Player::class, $playerChart->getPlayer()->getId())); |
|
|
|
|
52
|
|
|
$lostPosition->setChart($this->em->getReference(Chart::class, $playerChart->getChart()->getId())); |
|
|
|
|
53
|
|
|
$this->em->persist($lostPosition); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|