Test Failed
Pull Request — master (#28)
by Chris
12:42
created

TicketSales::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OpenTickets\Tickets\Report;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\EntityRepository;
7
use OpenTickets\Tickets\Domain\ReadModel\TicketRecord\PurchaseRecord;
8
use OpenTickets\Tickets\Domain\ReadModel\TicketRecord\TicketRecord;
9
use OpenTickets\Tickets\Domain\ValueObject\DiscountType\Fixed;
10
use OpenTickets\Tickets\Domain\ValueObject\DiscountType\FixedPerTicket;
11
12
final class TicketSales implements ReportInterface
13
{
14
    /**
15
     * @var EntityManagerInterface
16
     */
17
    private $em;
18
19
    /**
20
     * DelegateInformation constructor.
21
     * @param EntityManagerInterface $em
22
     */
23
    public function __construct(EntityManagerInterface $em)
24
    {
25
        $this->em = $em;
26
    }
27
28
    public function produceReport(): array
29
    {
30
        $report = [];
31
        $repo = $this->em->getRepository(PurchaseRecord::class);
32
        /** @var EntityRepository $repo */
33
        $qb = $repo->createQueryBuilder('p');
34
        $qb->join('p.tickets', 't');
35
        $purchases = $qb->getQuery()->execute();
36
        foreach ($purchases as $purchase)
37
        {
38
            /** @var PurchaseRecord $purchase */
39
            $discountUsed = false;
40
41
            foreach ($purchase->getTickets() as $ticket) {
42
                /** @var TicketRecord $ticket */
43
                switch (true) {
44
                    case !$purchase->hasDiscountCode():
45
                        $report = $this->recordTicketPurchase(
46
                            $report,
47
                            $ticket->getTicketType()->getIdentifier(),
48
                            '-'
49
                        );
50
                        break;
51 View Code Duplication
                    case !$discountUsed && ($purchase->getDiscountCode()->getDiscountType() instanceof Fixed):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
52
                        $report = $this->recordTicketPurchase(
53
                            $report,
54
                            $ticket->getTicketType()->getIdentifier(),
55
                            $purchase->getDiscountCode()->getCode()
56
                        );
57
                        $discountUsed = true;
58
                        break;
59 View Code Duplication
                    case ($purchase->getDiscountCode()->getDiscountType() instanceof FixedPerTicket):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
60
                        $report = $this->recordTicketPurchase(
61
                            $report,
62
                            $ticket->getTicketType()->getIdentifier(),
63
                            $purchase->getDiscountCode()->getCode()
64
                        );
65
                        break;
66
                }
67
            }
68
        }
69
70
        $flattened = [];
71
72
        foreach ($report as $ticketType => $codes) {
73
            foreach ($codes as $code => $count) {
74
                $flattened[] = [
75
                    'ticket_type' => $ticketType,
76
                    'discount_code' => $code,
77
                    'count' => $count
78
                ];
79
            }
80
        }
81
82
        return $flattened;
83
    }
84
85
    /**
86
     * @param $report
87
     * @param $ticket
88
     */
89
    private function recordTicketPurchase($report, $ticket, $discount)
90
    {
91
        if (!isset($report[$ticket])) {
92
            $report[$ticket] = [];
93
        }
94
        if (!isset($report[$ticket][$discount])) {
95
            $report[$ticket][$discount] = 0;
96
        }
97
        $report[$ticket][$discount]++;
98
        return $report;
99
    }
100
}