1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Domain\Service\Basket; |
4
|
|
|
|
5
|
|
|
use Carnage\Cqrs\Aggregate\Identity\GeneratorInterface; |
6
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
7
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\Basket; |
8
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountCode; |
9
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservation; |
10
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservationRequest; |
11
|
|
|
|
12
|
|
|
class Factory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var GeneratorInterface |
16
|
|
|
*/ |
17
|
|
|
private $ticketIdGenerator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Configuration |
21
|
|
|
*/ |
22
|
|
|
private $configuration; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var BasketValidator |
26
|
|
|
*/ |
27
|
|
|
private $basketValidator; |
28
|
|
|
|
29
|
2 |
|
public function __construct( |
30
|
|
|
GeneratorInterface $ticketIdGenerator, |
31
|
|
|
Configuration $configuration, |
32
|
|
|
BasketValidator $basketValidator |
33
|
|
|
) { |
34
|
2 |
|
$this->ticketIdGenerator = $ticketIdGenerator; |
35
|
2 |
|
$this->configuration = $configuration; |
36
|
2 |
|
$this->basketValidator = $basketValidator; |
37
|
2 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function basket(TicketReservationRequest ...$reservationRequests): Basket |
40
|
|
|
{ |
41
|
1 |
|
$tickets = $this->createTicketReservations(...$reservationRequests); |
42
|
|
|
|
43
|
1 |
|
return Basket::fromReservations( |
44
|
1 |
|
$this->configuration, |
45
|
1 |
|
$this->basketValidator, |
46
|
1 |
|
...$tickets |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function basketWithDiscount( |
51
|
|
|
DiscountCode $discountCode, |
52
|
|
|
TicketReservationRequest ...$reservationRequests |
53
|
|
|
): Basket { |
54
|
1 |
|
$tickets = $this->createTicketReservations(...$reservationRequests); |
55
|
|
|
|
56
|
1 |
|
return Basket::fromReservationsWithDiscount( |
57
|
1 |
|
$this->configuration, |
58
|
1 |
|
$this->basketValidator, |
59
|
1 |
|
$discountCode, |
60
|
1 |
|
...$tickets |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return TicketReservation[] |
66
|
|
|
*/ |
67
|
2 |
|
private function createTicketReservations(TicketReservationRequest ...$reservationRequests): array |
68
|
|
|
{ |
69
|
2 |
|
$tickets = []; |
70
|
2 |
|
foreach ($reservationRequests as $reservationRequest) { |
71
|
2 |
|
for ($i = 0; $i < $reservationRequest->getQuantity(); $i++) { |
72
|
2 |
|
$tickets[] = new TicketReservation($reservationRequest->getTicketType(), |
73
|
2 |
|
$this->ticketIdGenerator->generateIdentity()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return $tickets; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|