1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Markus Tacker <[email protected]> |
5
|
|
|
* @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BCRM\BackendBundle\Service; |
9
|
|
|
|
10
|
|
|
use BCRM\BackendBundle\Entity\Event\Registration; |
11
|
|
|
use BCRM\BackendBundle\Entity\Event\RegistrationRepository; |
12
|
|
|
use BCRM\BackendBundle\Entity\Event\TicketRepository; |
13
|
|
|
use BCRM\BackendBundle\Entity\Event\UnregistrationRepository; |
14
|
|
|
use BCRM\BackendBundle\Event\Event\TicketDeletedEvent; |
15
|
|
|
use BCRM\BackendBundle\Event\Event\TicketMailSentEvent; |
16
|
|
|
use BCRM\BackendBundle\Event\Payment\PaymentVerifiedEvent; |
17
|
|
|
use BCRM\BackendBundle\Event\Payment\RegistrationPaidEvent; |
18
|
|
|
use BCRM\BackendBundle\Service\Event\ConfirmUnregistrationCommand; |
19
|
|
|
use BCRM\BackendBundle\Service\Event\CreateTicketCommand; |
20
|
|
|
use BCRM\BackendBundle\Service\Event\RegisterCommand; |
21
|
|
|
use BCRM\BackendBundle\Service\Event\SendPaymentNotificationMailCommand; |
22
|
|
|
use BCRM\BackendBundle\Service\Event\SendTicketMailCommand; |
23
|
|
|
use BCRM\BackendBundle\Service\Event\SendUnregistrationConfirmationMailCommand; |
24
|
|
|
use BCRM\BackendBundle\Service\Event\UnregisterCommand; |
25
|
|
|
use BCRM\BackendBundle\Service\Event\UnregisterTicketCommand; |
26
|
|
|
use BCRM\BackendBundle\Service\Mail\SendTemplateMailCommand; |
27
|
|
|
use BCRM\BackendBundle\Service\Payment\PayRegistrationCommand; |
28
|
|
|
use LiteCQRS\Bus\CommandBus; |
29
|
|
|
use LiteCQRS\Bus\EventMessageBus; |
30
|
|
|
use LiteCQRS\Plugin\CRUD\Model\Commands\CreateResourceCommand; |
31
|
|
|
use LiteCQRS\Plugin\CRUD\Model\Commands\DeleteResourceCommand; |
32
|
|
|
use LiteCQRS\Plugin\CRUD\Model\Commands\UpdateResourceCommand; |
33
|
|
|
use PhpOption\Option; |
34
|
|
|
use Psr\Log\LoggerAwareInterface; |
35
|
|
|
use Psr\Log\LoggerInterface; |
36
|
|
|
use Symfony\Component\Routing\RouterInterface; |
37
|
|
|
use Symfony\Component\Security\Core\Util\SecureRandom; |
38
|
|
|
use Endroid\QrCode\QrCode; |
39
|
|
|
|
40
|
|
|
class Event implements LoggerAwareInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var \LiteCQRS\Bus\CommandBus |
44
|
|
|
*/ |
45
|
|
|
private $commandBus; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
49
|
|
|
*/ |
50
|
|
|
private $router; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \BCRM\BackendBundle\Entity\Event\RegistrationRepository |
54
|
|
|
*/ |
55
|
|
|
private $registrationRepo; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \LiteCQRS\Bus\EventMessageBus |
59
|
|
|
*/ |
60
|
|
|
private $eventMessageBus; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \BCRM\BackendBundle\Entity\Event\UnregistrationRepository |
64
|
|
|
*/ |
65
|
|
|
private $unregistrationRepo; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \BCRM\BackendBundle\Entity\Event\TicketRepository |
69
|
|
|
*/ |
70
|
|
|
private $ticketRepo; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var LoggerInterface |
74
|
|
|
*/ |
75
|
|
|
private $logger; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param CommandBus $commandBus |
79
|
|
|
* @param RouterInterface $router |
80
|
|
|
*/ |
81
|
|
|
public function __construct(CommandBus $commandBus, EventMessageBus $eventMessageBus, RouterInterface $router, RegistrationRepository $registrationRepo, UnregistrationRepository $unregistrationRepo, TicketRepository $ticketRepo) |
82
|
|
|
{ |
83
|
|
|
$this->commandBus = $commandBus; |
84
|
|
|
$this->eventMessageBus = $eventMessageBus; |
85
|
|
|
$this->router = $router; |
86
|
|
|
$this->registrationRepo = $registrationRepo; |
87
|
|
|
$this->unregistrationRepo = $unregistrationRepo; |
88
|
|
|
$this->ticketRepo = $ticketRepo; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets a logger instance on the object |
93
|
|
|
* |
94
|
|
|
* @param LoggerInterface $logger |
95
|
|
|
* |
96
|
|
|
* @return null |
97
|
|
|
*/ |
98
|
|
|
public function setLogger(LoggerInterface $logger) |
99
|
|
|
{ |
100
|
|
|
$this->logger = $logger; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
public function register(RegisterCommand $command) |
105
|
|
|
{ |
106
|
|
|
$createRegistrationCommand = new CreateResourceCommand(); |
107
|
|
|
$createRegistrationCommand->class = '\BCRM\BackendBundle\Entity\Event\Registration'; |
108
|
|
|
$createRegistrationCommand->data = array( |
109
|
|
|
'event' => $command->event, |
110
|
|
|
'email' => $command->email, |
111
|
|
|
'name' => $command->name, |
112
|
|
|
'twitter' => $command->twitter, |
113
|
|
|
'saturday' => $command->saturday, |
114
|
|
|
'sunday' => $command->sunday, |
115
|
|
|
'food' => $command->food, |
116
|
|
|
'tags' => $command->tags, |
117
|
|
|
'type' => $command->type, |
118
|
|
|
'participantList' => $command->participantList, |
119
|
|
|
'uuid' => $command->uuid, |
120
|
|
|
'paymentMethod' => $command->payment, |
121
|
|
|
'donation' => (int)$command->donation |
122
|
|
|
); |
123
|
|
|
$this->commandBus->handle($createRegistrationCommand); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function sendPaymentNotificationMail(SendPaymentNotificationMailCommand $command) |
127
|
|
|
{ |
128
|
|
|
$updateCommand = new UpdateResourceCommand(); |
129
|
|
|
$updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Registration'; |
130
|
|
|
$updateCommand->id = $command->registration->getId(); |
131
|
|
|
$updateCommand->data = array('paymentNotified' => new \DateTime()); |
132
|
|
|
$this->commandBus->handle($updateCommand); |
133
|
|
|
|
134
|
|
|
$emailCommand = new SendTemplateMailCommand(); |
135
|
|
|
$emailCommand->email = $command->registration->getEmail(); |
136
|
|
|
$emailCommand->template = 'RegistrationPayment'; |
137
|
|
|
$emailCommand->templateData = array( |
138
|
|
|
'registration' => $command->registration, |
139
|
|
|
'payment_link' => rtrim($command->schemeAndHost, '/') . $this->router->generate('bcrmweb_registration_payment', array('id' => $command->registration->getUuid())) |
140
|
|
|
); |
141
|
|
|
$this->commandBus->handle($emailCommand); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function createTicket(CreateTicketCommand $command) |
145
|
|
|
{ |
146
|
|
|
$sr = new SecureRandom(); |
147
|
|
|
$code = ''; |
148
|
|
|
$max = 6; |
149
|
|
|
while (strlen($code) < $max) { |
150
|
|
|
$seq = preg_replace('/[^A-Z0-9]/', '', $sr->nextBytes(256)); |
151
|
|
|
for ($i = 0; $i < strlen($seq) && strlen($code) < $max; $i++) { |
152
|
|
|
$code .= $seq[$i]; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
$createCommand = new CreateResourceCommand(); |
156
|
|
|
$createCommand->class = '\BCRM\BackendBundle\Entity\Event\Ticket'; |
157
|
|
|
$createCommand->data = array( |
158
|
|
|
'event' => $command->event, |
159
|
|
|
'email' => $command->registration->getEmail(), |
160
|
|
|
'name' => $command->registration->getName(), |
161
|
|
|
'day' => $command->day, |
162
|
|
|
'type' => $command->registration->getType(), |
163
|
|
|
'code' => $code, |
164
|
|
|
); |
165
|
|
|
$this->commandBus->handle($createCommand); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function sendTicketMail(SendTicketMailCommand $command) |
169
|
|
|
{ |
170
|
|
|
$qrCode = new QrCode(); |
171
|
|
|
$qrCode->setText( |
172
|
|
|
rtrim($command->schemeAndHost, '/') . $this->router->generate( |
173
|
|
|
'bcrmweb_event_checkin', |
174
|
|
|
array('id' => $command->ticket->getId(), 'code' => $command->ticket->getCode()) |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
$qrCode->setSize(300); |
179
|
|
|
$qrCode->setPadding(10); |
180
|
|
|
$qrfile = tempnam(sys_get_temp_dir(), 'qrcode-') . '.png'; |
181
|
|
|
$qrCode->render($qrfile); |
182
|
|
|
|
183
|
|
|
$emailCommand = new SendTemplateMailCommand(); |
184
|
|
|
$emailCommand->email = $command->ticket->getEmail(); |
185
|
|
|
$emailCommand->template = 'Ticket'; |
186
|
|
|
$emailCommand->templateData = array( |
187
|
|
|
'ticket' => $command->ticket, |
188
|
|
|
'event' => $command->event, |
189
|
|
|
'cancel_link' => rtrim($command->schemeAndHost, '/') . $this->router->generate( |
190
|
|
|
'bcrmweb_event_cancel_ticket', |
191
|
|
|
array('id' => $command->ticket->getId(), 'code' => $command->ticket->getCode()) |
192
|
|
|
) |
193
|
|
|
); |
194
|
|
|
$registrationOptional = $this->registrationRepo->getRegistrationForEmail($command->event, $command->ticket->getEmail()); |
|
|
|
|
195
|
|
|
if ($registrationOptional->isDefined()) { |
196
|
|
|
$emailCommand->templateData['registration'] = $registrationOptional->get(); |
197
|
|
|
} |
198
|
|
|
$emailCommand->image = $qrfile; |
199
|
|
|
$emailCommand->format = 'text/html'; |
200
|
|
|
$this->commandBus->handle($emailCommand); |
201
|
|
|
|
202
|
|
|
$event = new TicketMailSentEvent(); |
203
|
|
|
$event->ticket = $command->ticket; |
204
|
|
|
$this->eventMessageBus->publish($event); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function unregister(UnregisterCommand $command) |
208
|
|
|
{ |
209
|
|
|
$createUnregistrationCommand = new CreateResourceCommand(); |
210
|
|
|
$createUnregistrationCommand->class = '\BCRM\BackendBundle\Entity\Event\Unregistration'; |
211
|
|
|
$createUnregistrationCommand->data = array( |
212
|
|
|
'event' => $command->event, |
213
|
|
|
'email' => $command->email, |
214
|
|
|
'saturday' => $command->saturday, |
215
|
|
|
'sunday' => $command->sunday, |
216
|
|
|
'confirmed' => $command->confirmed, |
217
|
|
|
); |
218
|
|
|
$this->commandBus->handle($createUnregistrationCommand); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function sendUnregistrationConfirmationMail(SendUnregistrationConfirmationMailCommand $command) |
222
|
|
|
{ |
223
|
|
|
$sr = new SecureRandom(); |
224
|
|
|
$key = sha1($sr->nextBytes(256), false); |
225
|
|
|
$updateCommand = new UpdateResourceCommand(); |
226
|
|
|
$updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Unregistration'; |
227
|
|
|
$updateCommand->id = $command->unregistration->getId(); |
228
|
|
|
$updateCommand->data = array('confirmationKey' => $key); |
229
|
|
|
$this->commandBus->handle($updateCommand); |
230
|
|
|
|
231
|
|
|
$emailCommand = new SendTemplateMailCommand(); |
232
|
|
|
$emailCommand->email = $command->unregistration->getEmail(); |
233
|
|
|
$emailCommand->template = 'UnregistrationConfirmation'; |
234
|
|
|
$emailCommand->templateData = array( |
235
|
|
|
'unregistration' => $command->unregistration, |
236
|
|
|
'confirmation_link' => rtrim($command->schemeAndHost, '/') . $this->router->generate('bcrm_unregistration_confirm', array('id' => $command->unregistration->getId(), 'key' => $key)) |
237
|
|
|
); |
238
|
|
|
$this->commandBus->handle($emailCommand); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
View Code Duplication |
public function confirmUnregistration(ConfirmUnregistrationCommand $command) |
|
|
|
|
242
|
|
|
{ |
243
|
|
|
$updateCommand = new UpdateResourceCommand(); |
244
|
|
|
$updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Unregistration'; |
245
|
|
|
$updateCommand->id = $command->unregistration->getId(); |
246
|
|
|
$updateCommand->data = array('confirmed' => 1); |
247
|
|
|
$this->commandBus->handle($updateCommand); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function unregisterTicket(UnregisterTicketCommand $command) |
251
|
|
|
{ |
252
|
|
|
// Create a new registration matching the unregistration |
253
|
|
|
$registration = $this->registrationRepo->getRegistrationForEmail($command->event, $command->unregistration->getEmail()); |
254
|
|
|
if ($registration->isDefined()) { |
255
|
|
|
/** @var Registration $r */ |
256
|
|
|
$r = $registration->get(); |
257
|
|
|
$registrationData = array( |
258
|
|
|
'event' => $command->event, |
259
|
|
|
'email' => $command->unregistration->getEmail(), |
260
|
|
|
'name' => $r->getName(), |
261
|
|
|
'twitter' => $r->getTwitter(), |
262
|
|
|
'food' => $r->getFood(), |
263
|
|
|
'tags' => $r->getTags(), |
264
|
|
|
'confirmed' => 1, |
265
|
|
|
'saturday' => $r->getSaturday(), |
266
|
|
|
'sunday' => $r->getSunday(), |
267
|
|
|
'participantList' => $r->isParticipantList(), |
268
|
|
|
'uuid' => $r->getUuid(), |
269
|
|
|
'donation' => $r->getDonation(), |
270
|
|
|
'payment' => $r->getPayment(), |
271
|
|
|
'paymentMethod' => $r->getPaymentMethod(), |
272
|
|
|
); |
273
|
|
|
if ($command->unregistration->getSaturday()) { |
274
|
|
|
$registrationData['saturday'] = false; |
275
|
|
|
} |
276
|
|
|
if ($command->unregistration->getSunday()) { |
277
|
|
|
$registrationData['sunday'] = false; |
278
|
|
|
} |
279
|
|
|
$createRegistrationCommand = new CreateResourceCommand(); |
280
|
|
|
$createRegistrationCommand->class = '\BCRM\BackendBundle\Entity\Event\Registration'; |
281
|
|
|
$createRegistrationCommand->data = $registrationData; |
282
|
|
|
$this->commandBus->handle($createRegistrationCommand); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// Delete tickets |
286
|
|
|
foreach ($this->ticketRepo->getTicketsForEmail($command->event, $command->unregistration->getEmail()) as $ticket) { |
287
|
|
|
if ( |
288
|
|
|
($ticket->isSaturday() && $command->unregistration->getSaturday()) |
289
|
|
|
|| ($ticket->isSunday() && $command->unregistration->getSunday()) |
290
|
|
|
) { |
291
|
|
|
$deleteTicketCommand = new DeleteResourceCommand(); |
292
|
|
|
$deleteTicketCommand->class = '\BCRM\BackendBundle\Entity\Event\Ticket'; |
293
|
|
|
$deleteTicketCommand->id = $ticket->getId(); |
294
|
|
|
$this->commandBus->handle($deleteTicketCommand); |
295
|
|
|
|
296
|
|
|
$event = new TicketDeletedEvent(); |
297
|
|
|
$event->ticket = $ticket; |
298
|
|
|
$this->eventMessageBus->publish($event); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
// Mark unregistration as processed |
303
|
|
|
$updateCommand = new UpdateResourceCommand(); |
304
|
|
|
$updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Unregistration'; |
305
|
|
|
$updateCommand->id = $command->unregistration->getId(); |
306
|
|
|
$updateCommand->data = array('processed' => true); |
307
|
|
|
$this->commandBus->handle($updateCommand); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Once a payment has been verified, find the registration it belongs to and mark it as paid |
312
|
|
|
* so the tickets can be assigned. |
313
|
|
|
* |
314
|
|
|
* @param PaymentVerifiedEvent $event |
315
|
|
|
*/ |
316
|
|
|
public function onPaymentVerified(PaymentVerifiedEvent $event) |
317
|
|
|
{ |
318
|
|
|
$payment = $event->payment; |
319
|
|
|
$registrationOptional = $this->registrationRepo->findByUuid($payment->getPayload()->get('item_number')); |
320
|
|
View Code Duplication |
if ($registrationOptional->isEmpty()) { |
|
|
|
|
321
|
|
|
Option::fromValue($this->logger)->map(function (LoggerInterface $logger) use ($payment) { |
322
|
|
|
$logger->alert(sprintf('No registration found with uuid "%s"', $payment->getPayload()->get('item_number')), array($payment)); |
323
|
|
|
}); |
324
|
|
|
return; |
325
|
|
|
} |
326
|
|
|
$registration = $registrationOptional->get(); |
327
|
|
|
|
328
|
|
|
$command = new PayRegistrationCommand(); |
329
|
|
|
$command->registration = $registration; |
330
|
|
|
$command->payment = $payment; |
331
|
|
|
$this->payRegistration($command); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function payRegistration(PayRegistrationCommand $command) |
335
|
|
|
{ |
336
|
|
|
$updateCommand = new UpdateResourceCommand(); |
337
|
|
|
$updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Registration'; |
338
|
|
|
$updateCommand->id = $command->registration->getId(); |
339
|
|
|
$updateCommand->data = array('payment' => $command->payment); |
340
|
|
|
$this->commandBus->handle($updateCommand); |
341
|
|
|
|
342
|
|
|
$event = new RegistrationPaidEvent(); |
343
|
|
|
$event->registration = $command->registration; |
344
|
|
|
$event->payment = $command->payment; |
345
|
|
|
$this->eventMessageBus->publish($event); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: