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

TicketSales   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 16.85 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 15
loc 89
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B produceReport() 15 56 9
A recordTicketPurchase() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}