1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Entity; |
4
|
|
|
|
5
|
|
|
use AppBundle\Traits\TimestampableTrait; |
6
|
|
|
use Doctrine\Common\Annotations\Annotation\Enum; |
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
9
|
|
|
use JMS\Serializer\Annotation\ExclusionPolicy; |
10
|
|
|
use JMS\Serializer\Annotation\Expose; |
11
|
|
|
use Ramsey\Uuid\Uuid; |
12
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @ORM\Table(name="customer_order") |
16
|
|
|
* @ORM\Entity(repositoryClass="AppBundle\Repository\CustomerOrderRepository") |
17
|
|
|
* @ExclusionPolicy("all") |
18
|
|
|
*/ |
19
|
|
|
class CustomerOrder |
20
|
|
|
{ |
21
|
|
|
use TimestampableTrait; |
22
|
|
|
|
23
|
|
|
const STATUS_OPENED = 'opened'; |
24
|
|
|
const STATUS_CLOSED = 'closed'; |
25
|
|
|
const STATUS_PENDING = 'pending'; |
26
|
|
|
const STATUS_PAID = 'paid'; |
27
|
|
|
const STATUS_REJECTED = 'rejected'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Uuid |
31
|
|
|
* |
32
|
|
|
* @ORM\Column(name="id", type="uuid_binary") |
33
|
|
|
* @ORM\Id |
34
|
|
|
* @ORM\GeneratedValue(strategy="UUID") |
35
|
|
|
*/ |
36
|
|
|
private $id; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ArrayCollection|Ticket[] |
40
|
|
|
* |
41
|
|
|
* @ORM\OneToMany( |
42
|
|
|
* targetEntity="AppBundle\Entity\Ticket", |
43
|
|
|
* mappedBy="object", |
44
|
|
|
* cascade={"persist", "remove"} |
45
|
|
|
* ) |
46
|
|
|
*/ |
47
|
|
|
protected $tickets; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var Enum |
51
|
|
|
* @Assert\Choice(callback="getStatuses") |
52
|
|
|
* @ORM\Column( |
53
|
|
|
* name="status", |
54
|
|
|
* type="string", |
55
|
|
|
* columnDefinition="enum('free', 'booked', 'ordered', 'opened', 'closed')" |
56
|
|
|
* ) |
57
|
|
|
* @Expose() |
58
|
|
|
*/ |
59
|
|
|
protected $status; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* CustomerOrder constructor. |
63
|
|
|
*/ |
64
|
|
|
public function __construct() |
65
|
|
|
{ |
66
|
|
|
$this->id = Uuid::uuid4(); |
67
|
|
|
$this->status = self::STATUS_OPENED; |
|
|
|
|
68
|
|
|
$this->tickets = new ArrayCollection(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Uuid |
73
|
|
|
*/ |
74
|
|
|
public function getId() |
75
|
|
|
{ |
76
|
|
|
return $this->id; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return Ticket[]|ArrayCollection |
81
|
|
|
*/ |
82
|
|
|
public function getTickets() |
83
|
|
|
{ |
84
|
|
|
return $this->tickets; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Ticket[]|ArrayCollection $tickets |
89
|
|
|
*/ |
90
|
|
|
public function setTickets($tickets) |
91
|
|
|
{ |
92
|
|
|
$this->tickets = $tickets; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Enum |
97
|
|
|
*/ |
98
|
|
|
public function getStatus() |
99
|
|
|
{ |
100
|
|
|
return $this->status; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Enum $status |
105
|
|
|
*/ |
106
|
|
|
public function setStatus($status) |
107
|
|
|
{ |
108
|
|
|
$this->status = $status; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add Ticket |
113
|
|
|
* |
114
|
|
|
* @param Ticket $ticket |
115
|
|
|
* @return CustomerOrder |
116
|
|
|
*/ |
117
|
|
|
public function addTicket(Ticket $ticket) |
118
|
|
|
{ |
119
|
|
|
if ($this->isStatusPaid()) { |
120
|
|
|
throw new \InvalidArgumentException('Order already paid. Impossible to add new ticket.'); |
121
|
|
|
} |
122
|
|
|
$this->tickets[] = $ticket; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Ticket $ticket |
129
|
|
|
*/ |
130
|
|
|
public function removeTicket(Ticket $ticket) |
131
|
|
|
{ |
132
|
|
|
if ($this->isStatusPaid()) { |
133
|
|
|
throw new \InvalidArgumentException('Order already pai. Impossible to remove the ticket.'); |
134
|
|
|
} |
135
|
|
|
$this->tickets->removeElement($ticket); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
private function isStatusPaid() |
142
|
|
|
{ |
143
|
|
|
return $this->status === self::STATUS_PAID; |
144
|
|
|
} |
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public static function getStatuses() |
149
|
|
|
{ |
150
|
|
|
return [ |
151
|
|
|
self::STATUS_OPENED, |
152
|
|
|
self::STATUS_CLOSED, |
153
|
|
|
self::STATUS_PAID, |
154
|
|
|
self::STATUS_PENDING, |
155
|
|
|
self::STATUS_REJECTED, |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..