|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: imhotek |
|
5
|
|
|
* Date: 28/11/16 |
|
6
|
|
|
* Time: 21:37 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace ConferenceTools\Tickets\Domain\CommandHandler; |
|
10
|
|
|
|
|
11
|
|
|
use Carnage\Cqrs\Aggregate\Identity\GeneratorInterface; |
|
12
|
|
|
use Carnage\Cqrs\MessageHandler\AbstractMethodNameMessageHandler; |
|
13
|
|
|
use Carnage\Cqrs\Persistence\Repository\RepositoryInterface; |
|
14
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\AssignToDelegate; |
|
15
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\CancelTicket; |
|
16
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\CompletePurchase; |
|
17
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\MakePayment; |
|
18
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\ReserveTickets; |
|
19
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\TimeoutPurchase; |
|
20
|
|
|
use ConferenceTools\Tickets\Domain\Model\Ticket\TicketPurchase; |
|
21
|
|
|
use ConferenceTools\Tickets\Domain\Service\Basket\Factory; |
|
22
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
|
23
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\Basket; |
|
24
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservation; |
|
25
|
|
|
|
|
26
|
|
|
class Ticket extends AbstractMethodNameMessageHandler |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var GeneratorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $identityGenerator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var RepositoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $repository; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Factory |
|
40
|
|
|
*/ |
|
41
|
|
|
private $basketFactory; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct( |
|
44
|
|
|
GeneratorInterface $identityGenerator, |
|
45
|
|
|
RepositoryInterface $repository, |
|
46
|
|
|
Factory $basketFactory |
|
47
|
|
|
) { |
|
48
|
|
|
|
|
49
|
|
|
$this->identityGenerator = $identityGenerator; |
|
50
|
|
|
$this->repository = $repository; |
|
51
|
|
|
$this->basketFactory = $basketFactory; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @TODO can we refactor the command to pass a basket directly? |
|
56
|
|
|
* @param ReserveTickets $command |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function handleReserveTickets(ReserveTickets $command) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($command->hasDiscountCode()) { |
|
61
|
|
|
$basket = $this->basketFactory->basketWithDiscount( |
|
62
|
|
|
$command->getDiscountCode(), |
|
63
|
|
|
...$command->getReservationRequests() |
|
64
|
|
|
); |
|
65
|
|
|
} else { |
|
66
|
|
|
$basket = $this->basketFactory->basket(...$command->getReservationRequests()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$purchase = TicketPurchase::create($this->identityGenerator->generateIdentity(), $basket); |
|
70
|
|
|
|
|
71
|
|
|
$this->repository->save($purchase); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function handleMakePayment(MakePayment $command) |
|
75
|
|
|
{ |
|
76
|
|
|
$purchase = $this->loadPurchase($command->getPurchaseId()); |
|
77
|
|
|
$purchase->markAsPaid($command->getPurchaserEmail()); |
|
78
|
|
|
$this->repository->save($purchase); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function handleTimeoutPurchase(TimeoutPurchase $command) |
|
82
|
|
|
{ |
|
83
|
|
|
$purchase = $this->loadPurchase($command->getPurchaseId()); |
|
84
|
|
|
$purchase->timeoutPurchase(); |
|
85
|
|
|
$this->repository->save($purchase); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function handleAssignToDelegate(AssignToDelegate $command) |
|
89
|
|
|
{ |
|
90
|
|
|
$purchase = $this->loadPurchase($command->getPurchaseId()); |
|
91
|
|
|
$purchase->assignTicketToDelegate($command->getTicketId(), $command->getDelegate()); |
|
92
|
|
|
$this->repository->save($purchase); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function handleCompletePurchase(CompletePurchase $command) |
|
96
|
|
|
{ |
|
97
|
|
|
$purchase = $this->loadPurchase($command->getPurchaseId()); |
|
98
|
|
|
$purchase->completePurchase($command->getPurchaseEmail(), ...$command->getDelegateInformation()); |
|
99
|
|
|
$this->repository->save($purchase); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function handleCancelTicket(CancelTicket $command) |
|
103
|
|
|
{ |
|
104
|
|
|
$purchase = $this->loadPurchase($command->getPurchaseId()); |
|
105
|
|
|
$purchase->cancelTicket($command->getTicketId()); |
|
106
|
|
|
$this->repository->save($purchase); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $purchaseId |
|
111
|
|
|
* @return TicketPurchase |
|
112
|
|
|
*/ |
|
113
|
|
|
private function loadPurchase(string $purchaseId): TicketPurchase |
|
114
|
|
|
{ |
|
115
|
|
|
$purchase = $this->repository->load($purchaseId); |
|
116
|
|
|
return $purchase; |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
} |