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\Event\OnFlushEventArgs; |
10
|
|
|
use Doctrine\ORM\UnitOfWork; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
12
|
|
|
|
13
|
|
|
class TicketStatusListener |
14
|
|
|
{ |
15
|
|
|
protected $tokenStorage; |
16
|
|
|
|
17
|
|
|
public function __construct(TokenStorageInterface $tokenStorage) |
18
|
|
|
{ |
19
|
|
|
$this->tokenStorage = $tokenStorage; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function onFlush(OnFlushEventArgs $args) |
23
|
|
|
{ |
24
|
|
|
$em = $args->getEntityManager(); |
25
|
|
|
$uow = $em->getUnitOfWork(); |
26
|
|
|
|
27
|
|
|
if (!$ticket = $this->checkTicketStatusUpdate($uow)) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$oldStatus = $uow->getEntityChangeSet($ticket)['status'][0]; |
32
|
|
|
$newStatus = $uow->getEntityChangeSet($ticket)['status'][1]; |
33
|
|
|
|
34
|
|
|
if (!in_array($newStatus, Ticket::getStatuses())) { |
35
|
|
|
throw new \InvalidArgumentException("Invalid ticket status"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($oldStatus === Ticket::STATUS_PAID) { |
39
|
|
|
throw new TicketStatusConflictException("Invalid status. Ticket already paid."); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($oldStatus === Ticket::STATUS_OFFLINE) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
//$user = $this->tokenStorage->getToken()->getUser(); |
|
|
|
|
47
|
|
|
$user = $em->getRepository('AppBundle:Customer')->find(1); |
48
|
|
|
/** @var CustomerOrderRepository $customerOrderRepository */ |
49
|
|
|
$customerOrderRepository = $em->getRepository('AppBundle:CustomerOrder'); |
50
|
|
|
$order = $customerOrderRepository->findLastOpenOrder($user); |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creating order if isn't exist |
54
|
|
|
*/ |
55
|
|
|
if (!$order) { |
56
|
|
|
$order = new CustomerOrder($user); |
|
|
|
|
57
|
|
|
$ticketMetadata = $em->getClassMetadata(CustomerOrder::class); |
58
|
|
|
$em->persist($order); |
59
|
|
|
$uow->computeChangeSet($ticketMetadata, $order); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set an order to a ticket |
64
|
|
|
* |
65
|
|
|
*/ |
66
|
|
|
if ($oldStatus === Ticket::STATUS_FREE && $newStatus === Ticket::STATUS_BOOKED) { |
67
|
|
|
$ticket->setCustomerOrder($order); |
68
|
|
|
/** |
69
|
|
|
* Unset an order from a ticket |
70
|
|
|
*/ |
71
|
|
|
} elseif ($oldStatus === Ticket::STATUS_BOOKED && $newStatus === Ticket::STATUS_FREE) { |
72
|
|
|
$ticket->setCustomerOrder(null); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks if ticket status was updated |
78
|
|
|
* |
79
|
|
|
* @param UnitOfWork $uow |
80
|
|
|
* @return Ticket|false |
81
|
|
|
*/ |
82
|
|
|
private function checkTicketStatusUpdate(UnitOfWork $uow) |
83
|
|
|
{ |
84
|
|
|
if (!$uow->getScheduledEntityUpdates()) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) { |
89
|
|
|
if (!$entity instanceof Ticket && !key_exists('status', $uow->getEntityChangeSet($entity))) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
return $entity; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.