Passed
Push — master ( c541cb...64a56e )
by Chris
33s
created

TicketSales   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 22.92 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 22
loc 96
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C produceReport() 22 63 10
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 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 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...
53
                        $report = $this->recordTicketPurchase(
54
                            $report,
55
                            $ticket->getTicketType()->getIdentifier(),
56
                            $purchase->getDiscountCode()->getCode()
57
                        );
58
                        $discountUsed = true;
59
                        break;
60 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...
61
                        $report = $this->recordTicketPurchase(
62
                            $report,
63
                            $ticket->getTicketType()->getIdentifier(),
64
                            $purchase->getDiscountCode()->getCode()
65
                        );
66
                        break;
67 View Code Duplication
                    case ($purchase->getDiscountCode()->getDiscountType() instanceof Percentage):
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...
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
}