|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\CustomerOrder; |
|
6
|
|
|
use AppBundle\Entity\Ticket; |
|
7
|
|
|
use AppBundle\Exception\TicketStatusConflictException; |
|
8
|
|
|
use AppBundle\Repository\CustomerOrderRepository; |
|
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
10
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
|
11
|
|
|
use Doctrine\ORM\UnitOfWork; |
|
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
13
|
|
|
|
|
14
|
|
|
class TicketStatusListener |
|
15
|
|
|
{ |
|
16
|
|
|
protected $tokenStorage; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(TokenStorageInterface $tokenStorage) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->tokenStorage = $tokenStorage; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function onFlush(OnFlushEventArgs $args) |
|
24
|
|
|
{ |
|
25
|
|
|
$em = $args->getEntityManager(); |
|
26
|
|
|
$uow = $em->getUnitOfWork(); |
|
27
|
|
|
|
|
28
|
|
|
if (!$ticket = $this->checkTicketStatusUpdate($uow)) { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$oldStatus = $uow->getEntityChangeSet($ticket)['status'][0]; |
|
33
|
|
|
$newStatus = $uow->getEntityChangeSet($ticket)['status'][1]; |
|
34
|
|
|
|
|
35
|
|
|
if (!in_array($newStatus, Ticket::getStatuses())) { |
|
36
|
|
|
throw new \InvalidArgumentException("Invalid ticket status"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if ($oldStatus === Ticket::STATUS_PAID) { |
|
40
|
|
|
throw new TicketStatusConflictException("Invalid status. Ticket already paid."); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Checks if ticket status was updated |
|
46
|
|
|
* |
|
47
|
|
|
* @param UnitOfWork $uow |
|
48
|
|
|
* @return Ticket|false |
|
49
|
|
|
*/ |
|
50
|
|
|
private function checkTicketStatusUpdate(UnitOfWork $uow) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$uow->getScheduledEntityUpdates()) { |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) { |
|
57
|
|
|
if (!$entity instanceof Ticket && !key_exists('status', $uow->getEntityChangeSet($entity))) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
return $entity; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|