Completed
Pull Request — master (#147)
by
unknown
11:39
created

Ticket::removeUserOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use AppBundle\Exception\TicketStatusConflictException;
6
use AppBundle\Traits\TimestampableTrait;
7
use Doctrine\ORM\Mapping as ORM;
8
use JMS\Serializer\Annotation as Serializer;
9
use JMS\Serializer\Annotation\ExclusionPolicy;
10
use JMS\Serializer\Annotation\Expose;
11
use JMS\Serializer\Annotation\Type;
12
use Ramsey\Uuid\Uuid;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
/**
16
 * @ORM\Table(name="ticket")
17
 * @ORM\Entity(repositoryClass="AppBundle\Repository\TicketRepository")
18
 * @ExclusionPolicy("all")
19
 */
20
class Ticket
21
{
22
    use TimestampableTrait;
23
24
    const STATUS_FREE    = 'free';
25
    const STATUS_BOOKED  = 'booked';
26
    const STATUS_PAID    = 'paid';
27
    const STATUS_OFFLINE = 'offline';
28
29
    /**
30
     * @var Uuid
31
     *
32
     * @ORM\Column(name="id", type="uuid_binary")
33
     * @ORM\Id
34
     * @ORM\GeneratedValue(strategy="UUID")
35
     *
36
     * @Serializer\Groups({"get_ticket"})
37
     * @Type("string")
38
     * @Expose
39
     */
40
    private $id;
41
42
    /**
43
     * @var \DateTime
44
     * @Assert\DateTime()
45
     * @ORM\Column(name="series_date", type="datetime", nullable=false)
46
     *
47
     * @Serializer\Groups({"get_ticket"})
48
     * @Type("DateTime")
49
     * @Expose
50
     */
51
    private $seriesDate;
52
53
    /**
54
     * @var string
55
     * @Assert\NotBlank()
56
     * @ORM\Column(name="series_number", type="string", length=10, nullable=false)
57
     *
58
     * @Serializer\Groups({"get_ticket"})
59
     * @Type("string")
60
     * @Expose
61
     */
62
    private $seriesNumber;
63
64
    /**
65
     * @var integer
66
     * @Assert\NotBlank()
67
     * @ORM\Column(type="integer", nullable=false)
68
     *
69
     * @Serializer\Groups({"get_ticket"})
70
     * @Type("integer")
71
     * @Expose
72
     */
73
    private $price;
74
75
    /**
76
     * @var Seat
77
     *
78
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Seat", fetch="EAGER")
79
     * @ORM\JoinColumn(name="seat_id", referencedColumnName="id", nullable=false)
80
     *
81
     * @Serializer\Groups({"get_ticket"})
82
     * @Type("AppBundle\Entity\Seat")
83
     * @Expose()
84
     */
85
    protected $seat;
86
87
    /**
88
     * @var PerformanceEvent
89
     *
90
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\PerformanceEvent",  fetch="EAGER")
91
     * @ORM\JoinColumn(name="performance_event_id", referencedColumnName="id", nullable=false)
92
     * @Type("AppBundle\Entity\PerformanceEvent")
93
     */
94
    protected $performanceEvent;
95
96
    /**
97
     * @var UserOrder
98
     *
99
     * @ORM\ManyToOne(targetEntity="UserOrder", inversedBy="tickets", fetch="EAGER")
100
     * @ORM\JoinColumn(name="user_order_id", referencedColumnName="id", nullable=true)
101
     */
102
    protected $userOrder;
103
104
    /**
105
     * @var string
106
     * @Assert\Choice(callback="getStatuses")
107
     * @ORM\Column(name="status", type="string", length=15)
108
     * @Serializer\Groups({"get_ticket"})
109
     * @Type("string")
110
     * @Expose()
111
     */
112
    protected $status;
113
114
    /**
115
     * Ticket constructor.
116
     *
117
     * @param Seat $seat
118
     * @param PerformanceEvent $performanceEvent
119
     * @param int $ticketPrice
120
     * @param \DateTime $seriesDate
121
     * @param string $seriesNumber
122
     */
123
    public function __construct(
124
        Seat $seat,
125
        PerformanceEvent $performanceEvent,
126
        int $ticketPrice,
127
        \DateTime $seriesDate,
128
        string $seriesNumber
129
    ) {
130
        $this->id = Uuid::uuid4();
131
        $this->status = self::STATUS_FREE;
132
        $this->seat = $seat;
133
        $this->performanceEvent = $performanceEvent;
134
        $this->price = $ticketPrice;
135
        $this->seriesDate = $seriesDate;
136
        $this->seriesNumber = $seriesNumber;
137
    }
138
139
    /**
140
     * @return Uuid
141 3
     */
142
    public function getId(): Uuid
143 3
    {
144
        return $this->id;
145
    }
146
147
    /**
148
     * @return int
149
     */
150
    public function getPrice(): int
151
    {
152
        return $this->price;
153
    }
154
155
    /**
156
     * @return Seat
157
     */
158
    public function getSeat(): Seat
159
    {
160
        return $this->seat;
161
    }
162
163
    /**
164
     * @return PerformanceEvent
165
     */
166
    public function getPerformanceEvent(): PerformanceEvent
167
    {
168
        return $this->performanceEvent;
169
    }
170
171
    /**
172
     * Get PerformanceEvent Id.
173
     *
174
     * @Serializer\VirtualProperty()
175
     * @Serializer\SerializedName("performance_event_id")
176
     * @Type("integer")
177
     * @Serializer\Groups({"get_ticket"})
178
     *
179
     * @return integer
180 2
     */
181
    public function getPerformanceEventId(): int
182 2
    {
183
        return $this->performanceEvent->getId();
184
    }
185
186
    /**
187
     * Get PriceCategory Id.
188
     *
189
     * @Serializer\VirtualProperty()
190
     * @Serializer\SerializedName("price_category_id")
191
     * @Type("integer")
192
     * @Serializer\Groups({"get_ticket"})
193
     *
194
     * @return integer
195 2
     */
196
    public function getPriceCategoryId(): int
197
    {
198 2
        //TODO
199
        return 0;
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getStatus(): string
206
    {
207
        return $this->status;
208
    }
209
210
    /**
211
     * @param String $status
212
     *
213
     * @return Ticket
214 1
     */
215
    public function setStatus($status)
216 1
    {
217
        if (!in_array($status, $this->getStatuses())) {
218 1
            throw new TicketStatusConflictException('Invalid status');
219
        }
220
221
        if ($this->getStatus() === Ticket::STATUS_PAID) {
222
            throw new TicketStatusConflictException("Invalid status. Ticket already paid.");
223
        }
224
225
        if ($this->getStatus() === $status) {
226
            return $this;
227
        }
228
229
        $this->status = $status;
230
231
        return $this;
232
    }
233
234
    /**
235
     * @return array
236
     */
237
    public static function getStatuses(): array
238
    {
239
        return [
240
            self::STATUS_FREE,
241
            self::STATUS_BOOKED,
242
            self::STATUS_PAID,
243
            self::STATUS_OFFLINE,
244
        ];
245
    }
246
247
    /**
248
     * @return \DateTime
249
     */
250
    public function getSeriesDate(): \DateTime
251
    {
252
        return $this->seriesDate;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function getSeriesNumber(): string
259
    {
260
        return $this->seriesNumber;
261
    }
262
263
    /**
264
     * Set userOrder
265
     *
266
     * @param UserOrder $userOrder
267
     *
268
     * @return Ticket
269
     */
270
    public function setUserOrder(UserOrder $userOrder)
271
    {
272
        if ($this->getStatus() !== Ticket::STATUS_FREE) {
273
            throw new TicketStatusConflictException('Ticket is not free');
274
        }
275
        $this->setStatus(self::STATUS_BOOKED);
276
277
        $this->userOrder = $userOrder;
278
279
        return $this;
280
    }
281
282
    /**
283
     * Remove userOrder from ticket
284
     *
285
     * @return $this
286
     */
287
    public function removeUserOrder()
288
    {
289
        $this->userOrder = null;
290
        $this->status = self::STATUS_FREE;
291
292
        return $this;
293
    }
294
295
    /**
296
     * Get userOrder
297
     *
298
     * @return UserOrder
299
     */
300
    public function getUserOrder(): ?UserOrder
301
    {
302
        return $this->userOrder;
303
    }
304
}
305