1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Entity; |
4
|
|
|
|
5
|
|
|
use AppBundle\Traits\TimestampableTrait; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
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="user_order") |
17
|
|
|
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserOrderRepository") |
18
|
|
|
* @ExclusionPolicy("all") |
19
|
|
|
*/ |
20
|
|
|
class UserOrder |
21
|
|
|
{ |
22
|
|
|
use TimestampableTrait; |
23
|
|
|
|
24
|
|
|
const STATUS_OPENED = 'opened'; |
25
|
|
|
const STATUS_CLOSED = 'closed'; |
26
|
|
|
const STATUS_PENDING = 'pending'; |
27
|
|
|
const STATUS_PAID = 'paid'; |
28
|
|
|
const STATUS_REJECTED = 'rejected'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Uuid |
32
|
|
|
* |
33
|
|
|
* @ORM\Column(name="id", type="uuid_binary") |
34
|
|
|
* @ORM\Id |
35
|
|
|
* @ORM\GeneratedValue(strategy="UUID") |
36
|
|
|
*/ |
37
|
|
|
private $id; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ArrayCollection|Ticket[] |
41
|
|
|
* |
42
|
|
|
* @ORM\OneToMany( |
43
|
|
|
* targetEntity="AppBundle\Entity\Ticket", |
44
|
|
|
* mappedBy="object", |
45
|
|
|
* cascade={"persist", "remove"} |
46
|
|
|
* ) |
47
|
|
|
*/ |
48
|
|
|
protected $tickets; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
* @Assert\Choice(callback="getStatuses") |
53
|
|
|
* @ORM\Column(name="status", type="string", length=15) |
54
|
|
|
* @Serializer\Type("string") |
55
|
|
|
* @Expose() |
56
|
|
|
*/ |
57
|
|
|
protected $status; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var User |
61
|
|
|
* |
62
|
|
|
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="orders") |
63
|
|
|
* @Type("AppBundle\Entity\User") |
64
|
|
|
*/ |
65
|
|
|
private $user; |
66
|
|
|
/** |
67
|
|
|
* UserOrder constructor. |
68
|
|
|
*/ |
69
|
|
|
public function __construct() |
70
|
|
|
{ |
71
|
|
|
$this->id = Uuid::uuid4(); |
72
|
|
|
$this->status = self::STATUS_OPENED; |
73
|
|
|
$this->tickets = new ArrayCollection(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Uuid |
78
|
|
|
*/ |
79
|
|
|
public function getId(): Uuid |
80
|
|
|
{ |
81
|
|
|
return $this->id; |
82
|
|
|
} |
83
|
|
|
/** |
84
|
|
|
* Get user. |
85
|
|
|
* |
86
|
|
|
* @return \AppBundle\Entity\User |
87
|
|
|
*/ |
88
|
|
|
public function getUser() |
89
|
|
|
{ |
90
|
|
|
return $this->user; |
91
|
|
|
} |
92
|
|
|
/** |
93
|
|
|
* @return Ticket[]|ArrayCollection |
94
|
|
|
*/ |
95
|
|
|
public function getTickets() |
96
|
|
|
{ |
97
|
|
|
return $this->tickets; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Ticket[]|ArrayCollection $tickets |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function setTickets($tickets) |
106
|
|
|
{ |
107
|
|
|
$this->tickets = $tickets; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getStatus(): string |
116
|
|
|
{ |
117
|
|
|
return $this->status; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param String $status |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setStatus($status) |
126
|
|
|
{ |
127
|
|
|
$this->status = $status; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add Ticket |
134
|
|
|
* |
135
|
|
|
* @param Ticket $ticket |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function addTicket(Ticket $ticket) |
140
|
|
|
{ |
141
|
|
|
if ($this->isStatusPaid()) { |
142
|
|
|
throw new \InvalidArgumentException('Order already paid. Impossible to add new ticket.'); |
143
|
|
|
} |
144
|
|
|
$this->tickets[] = $ticket; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Ticket $ticket |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function removeTicket(Ticket $ticket) |
155
|
|
|
{ |
156
|
|
|
if ($this->isStatusPaid()) { |
157
|
|
|
throw new \InvalidArgumentException('Order already pai. Impossible to remove the ticket.'); |
158
|
|
|
} |
159
|
|
|
$this->tickets->removeElement($ticket); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
private function isStatusPaid(): bool |
168
|
|
|
{ |
169
|
|
|
return $this->status === self::STATUS_PAID; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public static function getStatuses(): array |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
self::STATUS_OPENED, |
179
|
|
|
self::STATUS_CLOSED, |
180
|
|
|
self::STATUS_PAID, |
181
|
|
|
self::STATUS_PENDING, |
182
|
|
|
self::STATUS_REJECTED, |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|