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