|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Domain\Projection; |
|
4
|
|
|
|
|
5
|
|
|
use Carnage\Cqrs\Event\Projection\ResettableInterface; |
|
6
|
|
|
use Carnage\Cqrs\MessageHandler\AbstractMethodNameMessageHandler; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use ConferenceTools\Tickets\Domain\Event\Ticket\TicketReleased; |
|
9
|
|
|
use ConferenceTools\Tickets\Domain\Event\Ticket\TicketReserved; |
|
10
|
|
|
use ConferenceTools\Tickets\Domain\ReadModel\TicketCounts\TicketCounter; |
|
11
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
|
12
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\Money; |
|
13
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\Price; |
|
14
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TaxRate; |
|
15
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TicketType; |
|
16
|
|
|
|
|
17
|
|
|
class TicketCounts extends AbstractMethodNameMessageHandler implements ResettableInterface |
|
18
|
|
|
{ |
|
19
|
|
|
private $em; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Configuration |
|
23
|
|
|
*/ |
|
24
|
|
|
private $ticketConfig; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(EntityManagerInterface $em, Configuration $ticketConfig) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->em = $em; |
|
29
|
|
|
$this->ticketConfig = $ticketConfig; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function reset() |
|
33
|
|
|
{ |
|
34
|
|
|
$em = $this->em; |
|
35
|
|
|
|
|
36
|
|
|
$q = $em->createQuery(sprintf('delete from %s', TicketCounter::class)); |
|
37
|
|
|
$q->execute(); |
|
38
|
|
|
|
|
39
|
|
|
foreach ($this->ticketConfig->getTicketTypes() as $handle => $ticketType) { |
|
40
|
|
|
$entity = new TicketCounter( |
|
41
|
|
|
$ticketType, |
|
42
|
|
|
$this->ticketConfig->getAvailableTickets($handle) |
|
43
|
|
|
); |
|
44
|
|
|
$em->persist($entity); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$em->flush(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
protected function handleTicketReserved(TicketReserved $event) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$counter = $this->em->getRepository(TicketCounter::class)->findOneBy( |
|
53
|
|
|
['ticketType.identifier' => $event->getTicketType()->getIdentifier()] |
|
54
|
|
|
); |
|
55
|
|
|
$counter->ticketsReserved(1); |
|
56
|
|
|
$this->em->flush(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
View Code Duplication |
protected function handleTicketReleased(TicketReleased $event) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$counter = $this->em->getRepository(TicketCounter::class)->findOneBy( |
|
62
|
|
|
['ticketType.identifier' => $event->getTicketType()->getIdentifier()] |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$counter->ticketsReleased(1); |
|
66
|
|
|
$this->em->flush(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
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.