Passed
Pull Request — master (#25)
by Chris
03:05
created

Basket   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
dl 0
loc 108
ccs 31
cts 36
cp 0.8611
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A fromReservations() 0 13 1
A fromReservationsWithDiscount() 0 18 1
A getTotal() 0 4 1
A hasDiscountCode() 0 4 1
A getDiscountCode() 0 4 1
A calculateTotal() 0 8 2
A getPreDiscountTotal() 0 4 1
A getTickets() 0 4 1
1
<?php
2
3
namespace OpenTickets\Tickets\Domain\ValueObject;
4
5
use OpenTickets\Tickets\Domain\Service\Configuration;
6
7
class Basket
8
{
9
    private $tickets;
10
    /**
11
     * @var Price
12
     */
13
    private $preDiscountTotal;
14
15
    /**
16
     * @var Price
17
     */
18
    private $total;
19
20
    /**
21
     * @var DiscountCode
22
     */
23
    private $discountCode;
24
25 6
    private function __construct(TicketReservation ...$tickets)
26
    {
27 6
        if (count($tickets) === 0) {
28
            throw new \InvalidArgumentException('Must put at least one Ticket reservation into a basket');
29
        }
30
31 6
        $this->tickets = $tickets;
32
33 6
    }
34
35 3
    public static function fromReservations(Configuration $config, TicketReservation ...$tickets)
36
    {
37 3
        $instance = new self(
38 3
            ...$tickets
39
        );
40
41 3
        $zero = Price::fromNetCost(new Money(0, $config->getCurrency()), $config->getTaxRate());
42 3
        $total = $instance->calculateTotal($zero);
43
44 3
        $instance->preDiscountTotal = $total;
45 3
        $instance->total = $total;
46 3
        return $instance;
47
    }
48
49 3
    public static function fromReservationsWithDiscount(
50
        Configuration $config,
51
        DiscountCode $discountCode,
52
        TicketReservation ...$tickets)
53
    {
54 3
        $instance = new self(
55 3
            ...$tickets
56
        );
57
58 3
        $zero = Price::fromNetCost(new Money(0, $config->getCurrency()), $config->getTaxRate());
59 3
        $total = $instance->calculateTotal($zero);
60
61 3
        $instance->preDiscountTotal = $total;
62 3
        $instance->total = $total->subtract($discountCode->apply($instance));
63 3
        $instance->discountCode = $discountCode;
64
65 3
        return $instance;
66
    }
67
68
    /**
69
     * @return TicketReservation[]
70
     */
71 2
    public function getTickets(): array
72
    {
73 2
        return $this->tickets;
74
    }
75
76
    /**
77
     * @return Price
78
     */
79 6
    public function getTotal(): Price
80
    {
81 6
        return $this->total;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    public function hasDiscountCode(): bool
88
    {
89
        return !($this->discountCode === null);
90
    }
91
92
    /**
93
     * @TODO make nullable (PHP 7.1)
94
     * @return DiscountCode
95
     */
96
    public function getDiscountCode(): DiscountCode
97
    {
98
        return $this->discountCode;
99
    }
100
101 6
    private function calculateTotal(Price $total): Price
102
    {
103 6
        foreach ($this->tickets as $ticket) {
104 6
            $total = $total->add($ticket->getTicketType()->getPrice());
105
        }
106
107 6
        return $total;
108
    }
109
110 5
    public function getPreDiscountTotal(): Price
111
    {
112 5
        return $this->preDiscountTotal;
113
    }
114
}
115