|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Report; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Doctrine\ORM\EntityRepository; |
|
7
|
|
|
use ConferenceTools\Tickets\Domain\ReadModel\TicketRecord\PurchaseRecord; |
|
8
|
|
|
use ConferenceTools\Tickets\Domain\ReadModel\TicketRecord\TicketRecord; |
|
9
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType\Fixed; |
|
10
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType\FixedPerTicket; |
|
11
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\DiscountType\Percentage; |
|
12
|
|
|
|
|
13
|
|
|
final class TicketSales implements ReportInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var EntityManagerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $em; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* DelegateInformation constructor. |
|
22
|
|
|
* @param EntityManagerInterface $em |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(EntityManagerInterface $em) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->em = $em; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function produceReport(): array |
|
30
|
|
|
{ |
|
31
|
|
|
$report = []; |
|
32
|
|
|
$repo = $this->em->getRepository(PurchaseRecord::class); |
|
33
|
|
|
/** @var EntityRepository $repo */ |
|
34
|
|
|
$qb = $repo->createQueryBuilder('p'); |
|
35
|
|
|
$qb->join('p.tickets', 't'); |
|
36
|
|
|
$purchases = $qb->getQuery()->execute(); |
|
37
|
|
|
foreach ($purchases as $purchase) |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var PurchaseRecord $purchase */ |
|
40
|
|
|
$discountUsed = false; |
|
41
|
|
|
|
|
42
|
|
|
foreach ($purchase->getTickets() as $ticket) { |
|
43
|
|
|
/** @var TicketRecord $ticket */ |
|
44
|
|
|
switch (true) { |
|
45
|
|
|
case !$purchase->hasDiscountCode(): |
|
46
|
|
|
$report = $this->recordTicketPurchase( |
|
47
|
|
|
$report, |
|
48
|
|
|
$ticket->getTicketType()->getIdentifier(), |
|
49
|
|
|
'-' |
|
50
|
|
|
); |
|
51
|
|
|
break; |
|
52
|
|
|
case !$discountUsed && ($purchase->getDiscountCode()->getDiscountType() instanceof Fixed): |
|
53
|
|
|
$report = $this->recordTicketPurchase( |
|
54
|
|
|
$report, |
|
55
|
|
|
$ticket->getTicketType()->getIdentifier(), |
|
56
|
|
|
$purchase->getDiscountCode()->getCode() |
|
57
|
|
|
); |
|
58
|
|
|
$discountUsed = true; |
|
59
|
|
|
break; |
|
60
|
|
|
case ($purchase->getDiscountCode()->getDiscountType() instanceof FixedPerTicket): |
|
61
|
|
|
$report = $this->recordTicketPurchase( |
|
62
|
|
|
$report, |
|
63
|
|
|
$ticket->getTicketType()->getIdentifier(), |
|
64
|
|
|
$purchase->getDiscountCode()->getCode() |
|
65
|
|
|
); |
|
66
|
|
|
break; |
|
67
|
|
|
case ($purchase->getDiscountCode()->getDiscountType() instanceof Percentage): |
|
68
|
|
|
$report = $this->recordTicketPurchase( |
|
69
|
|
|
$report, |
|
70
|
|
|
$ticket->getTicketType()->getIdentifier(), |
|
71
|
|
|
$purchase->getDiscountCode()->getCode() |
|
72
|
|
|
); |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$flattened = []; |
|
79
|
|
|
|
|
80
|
|
|
foreach ($report as $ticketType => $codes) { |
|
81
|
|
|
foreach ($codes as $code => $count) { |
|
82
|
|
|
$flattened[] = [ |
|
83
|
|
|
'ticket_type' => $ticketType, |
|
84
|
|
|
'discount_code' => $code, |
|
85
|
|
|
'count' => $count |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $flattened; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $report |
|
95
|
|
|
* @param $ticket |
|
96
|
|
|
*/ |
|
97
|
|
|
private function recordTicketPurchase($report, $ticket, $discount) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!isset($report[$ticket])) { |
|
100
|
|
|
$report[$ticket] = []; |
|
101
|
|
|
} |
|
102
|
|
|
if (!isset($report[$ticket][$discount])) { |
|
103
|
|
|
$report[$ticket][$discount] = 0; |
|
104
|
|
|
} |
|
105
|
|
|
$report[$ticket][$discount]++; |
|
106
|
|
|
return $report; |
|
107
|
|
|
} |
|
108
|
|
|
} |