1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Repository; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\PerformanceEvent; |
6
|
|
|
use AppBundle\Entity\Seat; |
7
|
|
|
use AppBundle\Entity\Ticket; |
8
|
|
|
use AppBundle\Exception\Ticket\NotRemovableSetException; |
9
|
|
|
|
10
|
|
|
class TicketRepository extends AbstractRepository |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param PerformanceEvent $performanceEvent |
15
|
|
|
* |
16
|
|
|
* @return Ticket[] |
17
|
|
|
* @throws NotRemovableSetException |
18
|
|
|
*/ |
19
|
|
|
public function getRemovableTicketSet(PerformanceEvent $performanceEvent): array |
20
|
|
|
{ |
21
|
|
|
$ticket = $this->findOneBy([ |
22
|
|
|
'performanceEvent' => $performanceEvent, |
23
|
|
|
'status' => [Ticket::STATUS_BOOKED, Ticket::STATUS_PAID] |
24
|
|
|
]); |
25
|
|
|
|
26
|
|
|
if (!empty($ticket)) { |
27
|
|
|
throw new NotRemovableSetException( |
28
|
|
|
sprintf( |
29
|
|
|
'Impossible to remove tickets for PerformanceEvent: %s.', |
30
|
|
|
$performanceEvent |
31
|
|
|
) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$tickets = $this->findBy([ |
36
|
|
|
'performanceEvent' => $performanceEvent |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
return $tickets; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param PerformanceEvent $performanceEvent |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function isGeneratedSet(PerformanceEvent $performanceEvent) |
48
|
|
|
{ |
49
|
|
|
$ticket = $this->findOneBy([ |
50
|
|
|
'performanceEvent' => $performanceEvent |
51
|
|
|
]); |
52
|
|
|
return !empty($ticket); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Ticket[] $tickets |
57
|
|
|
*/ |
58
|
|
|
public function batchSave(array $tickets) |
59
|
|
|
{ |
60
|
|
|
foreach ($tickets as $ticket) { |
61
|
|
|
$this->save($ticket, false); |
62
|
|
|
} |
63
|
|
|
$this->getEntityManager()->flush(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Ticket[] $tickets |
68
|
|
|
*/ |
69
|
|
|
public function batchRemove(array $tickets) |
70
|
|
|
{ |
71
|
|
|
foreach ($tickets as $ticket) { |
72
|
|
|
$this->remove($ticket, false); |
73
|
|
|
} |
74
|
|
|
$this->getEntityManager()->flush(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Change Status in Ticket |
79
|
|
|
* form STATUS_OFFLINE to STATUS_FREE if Ticket is in RowsForSale |
80
|
|
|
* and vice versa |
81
|
|
|
* |
82
|
|
|
* @param PerformanceEvent $performanceEvent |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public function enableTicketsForSale(PerformanceEvent $performanceEvent) |
86
|
|
|
{ |
87
|
|
|
$seats = $this->getEntityManager()->getRepository(Seat::class)->getByVenue($performanceEvent->getVenue()); |
88
|
|
|
foreach ($seats as $seat) { |
89
|
|
|
$tickets[] = self::changeStatusInTicket( |
|
|
|
|
90
|
|
|
$performanceEvent, |
91
|
|
|
$seat, |
92
|
|
|
Ticket::STATUS_FREE, |
93
|
|
|
Ticket::STATUS_OFFLINE |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$tickets = []; |
98
|
|
|
|
99
|
|
|
foreach ($performanceEvent->getRowsForSale() as $forSale) { |
100
|
|
|
$seats = $this |
101
|
|
|
->getEntityManager() |
102
|
|
|
->getRepository(Seat::class) |
103
|
|
|
->getByVenueSectorAndRow( |
104
|
|
|
$forSale->getVenueSector(), |
105
|
|
|
$forSale->getRow() |
106
|
|
|
); |
107
|
|
|
foreach ($seats as $seat) { |
108
|
|
|
$tickets[] = self::changeStatusInTicket( |
109
|
|
|
$performanceEvent, |
110
|
|
|
$seat, |
111
|
|
|
Ticket::STATUS_OFFLINE, |
112
|
|
|
Ticket::STATUS_FREE |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$count = count($tickets); |
118
|
|
|
return $count; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param PerformanceEvent $performanceEvent |
123
|
|
|
* @param Seat $seat |
124
|
|
|
* @param string $oldStatus |
125
|
|
|
* @param string $newStatus |
126
|
|
|
* @return object |
127
|
|
|
*/ |
128
|
|
|
public function changeStatusInTicket(PerformanceEvent $performanceEvent, Seat $seat, $oldStatus, $newStatus) |
129
|
|
|
{ |
130
|
|
|
$ticket = $this->findOneBy([ |
131
|
|
|
'performanceEvent' => $performanceEvent, |
132
|
|
|
'seat' => $seat, |
133
|
|
|
'status' => $oldStatus, |
134
|
|
|
]); |
135
|
|
|
if ($ticket) { |
136
|
|
|
$ticket->setStatus($newStatus); |
137
|
|
|
$this->getEntityManager()->persist($ticket); |
138
|
|
|
|
139
|
|
|
$this->getEntityManager()->flush(); |
140
|
|
|
} |
141
|
|
|
return $ticket; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.