PurchaseRecord   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 218
ccs 65
cts 65
cp 1
rs 10
c 0
b 0
f 0
wmc 22

19 Methods

Rating   Name   Duplication   Size   Complexity  
A hasDiscountCode() 0 3 1
A getPurchaserEmail() 0 3 1
A pay() 0 4 1
A addTicketRecord() 0 5 1
A cancelTicket() 0 6 1
A shouldBeCancelled() 0 3 1
A setTotalCost() 0 3 1
A __construct() 0 6 1
A getTickets() 0 3 1
A getDiscountCode() 0 3 1
A isPaid() 0 3 1
A getCreatedAt() 0 3 1
A getTicketCount() 0 3 1
A hasTimedout() 0 3 1
A getTotalCost() 0 3 1
A getPurchaseId() 0 3 1
A getTicketSummary() 0 19 3
A applyDiscountCode() 0 3 1
A getTicketRecord() 0 7 2
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\ReadModel\TicketRecord;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use ConferenceTools\Tickets\Domain\ValueObject\DiscountCode;
9
use ConferenceTools\Tickets\Domain\ValueObject\Money;
10
use ConferenceTools\Tickets\Domain\ValueObject\Price;
11
use ConferenceTools\Tickets\Domain\ValueObject\TaxRate;
12
use ConferenceTools\Tickets\Domain\ValueObject\TicketType;
13
14
/**
15
 * Class PurchaseRecord
16
 * @package ConferenceTools\Tickets\Domain\ReadModel\TicketRecord
17
 * @ORM\Entity()
18
 */
19
class PurchaseRecord
20
{
21
    /**
22
     * @var integer
23
     * @ORM\Id @ORM\GeneratedValue @ORM\Column(type="integer")
24
     */
25
    private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @var string
29
     * @ORM\Column(type="string")
30
     */
31
    private $purchaseId;
32
33
    /**
34
     * @var string
35
     * @ORM\Column(type="string")
36
     */
37
    private $purchaserEmail = '';
38
39
    /**
40
     * @var Price
41
     * @ORM\Embedded(class="ConferenceTools\Tickets\Domain\ValueObject\Price")
42
     */
43
    private $totalCost;
44
45
    /**
46
     * @var Collection
47
     * @ORM\OneToMany(
48
     *     targetEntity="ConferenceTools\Tickets\Domain\ReadModel\TicketRecord\TicketRecord",
49
     *     mappedBy="purchase",
50
     *     indexBy="ticketId",
51
     *     cascade={"persist", "remove"},
52
     *     orphanRemoval=true
53
     *     )
54
     */
55
    private $tickets;
56
57
    /**
58
     * @var integer
59
     * @ORM\Column(type="integer")
60
     */
61
    private $ticketCount = 0;
62
63
    /**
64
     * @var bool
65
     * @ORM\Column(type="boolean")
66
     */
67
    private $paid = false;
68
69
    /**
70
     * @var \DateTime
71
     * @ORM\Column(type="datetime")
72
     */
73
    private $createdAt;
74
75
    /**
76
     * @ORM\Column(type="json_object", nullable=true)
77
     * @var DiscountCode
78
     */
79
    private $discountCode;
80
81
    /**
82
     * PurchaseRecord constructor.
83
     * @param string $purchaseId
84
     */
85 10
    public function __construct(string $purchaseId)
86
    {
87 10
        $this->purchaseId = $purchaseId;
88 10
        $this->totalCost = Price::fromNetCost(new Money(0, 'GBP'), new TaxRate(0));
89 10
        $this->createdAt = new \DateTime();
90 10
        $this->tickets = new ArrayCollection();
91 10
    }
92
93
    /**
94
     * @return string
95
     */
96 1
    public function getPurchaseId(): string
97
    {
98 1
        return $this->purchaseId;
99
    }
100
101
    /**
102
     * @return Price
103
     */
104 2
    public function getTotalCost(): Price
105
    {
106 2
        return $this->totalCost;
107
    }
108
109
    /**
110
     * @return Collection
111
     */
112 1
    public function getTickets(): Collection
113
    {
114 1
        return $this->tickets;
115
    }
116
117
    /**
118
     * @return int
119
     */
120 3
    public function getTicketCount(): int
121
    {
122 3
        return $this->ticketCount;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 1
    public function getPurchaserEmail(): string
129
    {
130 1
        return $this->purchaserEmail;
131
    }
132
133
    /**
134
     * @return boolean
135
     */
136 2
    public function isPaid(): bool
137
    {
138 2
        return $this->paid;
139
    }
140
141
    /**
142
     * @return \DateTime
143
     */
144 1
    public function getCreatedAt(): \DateTime
145
    {
146 1
        return $this->createdAt;
147
    }
148
149 2
    public function hasDiscountCode(): bool
150
    {
151 2
        return !($this->discountCode === null);
152
    }
153
154
    /**
155
     * @return DiscountCode
156
     */
157 1
    public function getDiscountCode(): DiscountCode
158
    {
159 1
        return $this->discountCode;
160
    }
161
162 3
    public function getTicketRecord($ticketId): TicketRecord
163
    {
164 3
        if (!isset($this->tickets[$ticketId])) {
165 1
            throw new \InvalidArgumentException("Invalid Ticket Id");
166
        }
167
168 2
        return $this->tickets[$ticketId];
169
    }
170
171 1
    public function getTicketSummary()
172
    {
173 1
        $tickets = [];
174 1
        foreach ($this->tickets as $ticket) {
175
            /** @var TicketRecord $ticket */
176 1
            $ticketTypeIdentifier = $ticket->getTicketType()->getIdentifier();
177
178 1
            if (!isset($tickets[$ticketTypeIdentifier])) {
179 1
                $tickets[$ticketTypeIdentifier]['quantity'] = 1;
180 1
                $tickets[$ticketTypeIdentifier]['lineTotal'] = $ticket->getTicketType()->getPrice();
181 1
                $tickets[$ticketTypeIdentifier]['ticketType'] = $ticket->getTicketType();
182
            } else {
183 1
                $tickets[$ticketTypeIdentifier]['quantity']++;
184 1
                $tickets[$ticketTypeIdentifier]['lineTotal'] =
185 1
                    $tickets[$ticketTypeIdentifier]['lineTotal']->add($ticket->getTicketType()->getPrice());
186
            }
187
        }
188
189 1
        return $tickets;
190
    }
191
192 3
    public function addTicketRecord(TicketType $ticketType, string $ticketId)
193
    {
194 3
        $ticketRecord = new TicketRecord($ticketType, $this, $ticketId);
195 3
        $this->tickets->set($ticketId, $ticketRecord);
196 3
        $this->ticketCount++;
197 3
    }
198
199
    /**
200
     * @param string $email
201
     */
202 1
    public function pay($email)
203
    {
204 1
        $this->purchaserEmail = $email;
205 1
        $this->paid = true;
206 1
    }
207
208
    /**
209
     * @param Price $totalCost
210
     */
211 1
    public function setTotalCost(Price $totalCost)
212
    {
213 1
        $this->totalCost = $totalCost;
214 1
    }
215
216 1
    public function hasTimedout()
217
    {
218 1
        return ($this->createdAt < new \DateTime('-30 minutes'));
219
    }
220
221 1
    public function applyDiscountCode(DiscountCode $discountCode)
222
    {
223 1
        $this->discountCode = $discountCode;
224 1
    }
225
226 1
    public function cancelTicket(string $ticketId)
227
    {
228 1
        $ticket = $this->getTicketRecord($ticketId);
229 1
        $ticket->cancel();
230 1
        $this->tickets->remove($ticketId);
231 1
        $this->ticketCount--;
232 1
    }
233
234 1
    public function shouldBeCancelled(): bool
235
    {
236 1
        return $this->ticketCount <= 0;
237
    }
238
}