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\TicketRepository; |
11
|
|
|
use BCRM\BackendBundle\Event\Event\TicketDeletedEvent; |
12
|
|
|
use BCRM\BackendBundle\Event\Event\TicketMailSentEvent; |
13
|
|
|
use BCRM\BackendBundle\Event\Payment\RegistrationPaidEvent; |
14
|
|
|
use BCRM\BackendBundle\Service\Mail\SendTemplateMailCommand; |
15
|
|
|
use LiteCQRS\Bus\CommandBus; |
16
|
|
|
use LiteCQRS\Plugin\CRUD\Model\Commands\UpdateResourceCommand; |
17
|
|
|
use PhpOption\Option; |
18
|
|
|
use Psr\Log\LoggerAwareInterface; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
|
21
|
|
|
class Ticket implements LoggerAwareInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \LiteCQRS\Bus\CommandBus |
25
|
|
|
*/ |
26
|
|
|
private $commandBus; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TicketRepository |
30
|
|
|
*/ |
31
|
|
|
private $ticketRepo; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
private $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param CommandBus $commandBus |
40
|
|
|
* @param TicketRepository $ticketRepo |
41
|
|
|
*/ |
42
|
|
|
public function __construct(CommandBus $commandBus, TicketRepository $ticketRepo) |
43
|
|
|
{ |
44
|
|
|
$this->commandBus = $commandBus; |
45
|
|
|
$this->ticketRepo = $ticketRepo; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets a logger instance on the object |
50
|
|
|
* |
51
|
|
|
* @param LoggerInterface $logger |
52
|
|
|
* |
53
|
|
|
* @return null |
54
|
|
|
*/ |
55
|
|
|
public function setLogger(LoggerInterface $logger) |
56
|
|
|
{ |
57
|
|
|
$this->logger = $logger; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
public function onTicketMailSent(TicketMailSentEvent $event) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$updateCommand = new UpdateResourceCommand(); |
63
|
|
|
$updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Ticket'; |
64
|
|
|
$updateCommand->id = $event->ticket->getId(); |
65
|
|
|
$updateCommand->data = array('notified' => '1'); |
66
|
|
|
$this->commandBus->handle($updateCommand); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function onTicketDeleted(TicketDeletedEvent $event) |
70
|
|
|
{ |
71
|
|
|
$emailCommand = new SendTemplateMailCommand(); |
72
|
|
|
$emailCommand->email = $event->ticket->getEmail(); |
73
|
|
|
$emailCommand->template = 'TicketDelete'; |
74
|
|
|
$emailCommand->templateData = array( |
75
|
|
|
'ticket' => $event->ticket, |
76
|
|
|
); |
77
|
|
|
$this->commandBus->handle($emailCommand); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Register payment on tickets |
82
|
|
|
* |
83
|
|
|
* @param RegistrationPaidEvent $event |
84
|
|
|
*/ |
85
|
|
|
public function onRegistrationPaid(RegistrationPaidEvent $event) |
86
|
|
|
{ |
87
|
|
|
$payment = $event->payment; |
88
|
|
|
$registration = $event->registration; |
89
|
|
|
Option::fromValue($this->logger)->map(function (LoggerInterface $logger) use ($registration, $payment) { |
90
|
|
|
$logger->alert(sprintf('Registration "%s" has been paid with payment "%s".', $registration->getId(), $payment->getId())); |
91
|
|
|
}); |
92
|
|
|
foreach ($this->ticketRepo->getTicketsForEmail( |
93
|
|
|
$registration->getEvent(), |
94
|
|
|
$registration->getEmail() |
95
|
|
|
) as $ticket) { |
96
|
|
|
$updateCommand = new UpdateResourceCommand(); |
97
|
|
|
$updateCommand->class = '\BCRM\BackendBundle\Entity\Event\Ticket'; |
98
|
|
|
$updateCommand->id = $ticket->getId(); |
99
|
|
|
$updateCommand->data = array('payment' => $payment); |
100
|
|
|
$this->commandBus->handle($updateCommand); |
101
|
|
|
Option::fromValue($this->logger)->map(function (LoggerInterface $logger) use ($payment, $ticket) { |
102
|
|
|
$logger->alert(sprintf('Assigning payment "%s" to ticket "%s"', $payment->getId(), $ticket->getCode())); |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.