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

UserOrder::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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 Ramsey\Uuid\Uuid;
12
use Symfony\Component\Validator\Constraints as Assert;
13
14
/**
15
 * @ORM\Table(name="user_order")
16
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserOrderRepository")
17
 * @ExclusionPolicy("all")
18
 */
19
class UserOrder
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 string
51
     * @Assert\Choice(callback="getStatuses")
52
     * @ORM\Column(name="status", type="string", length=15)
53
     * @Serializer\Type("string")
54
     * @Expose()
55
     */
56
    protected $status;
57
58
    /**
59
     * @var User
60
     *
61
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", fetch="EAGER")
62
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="SET NULL")
63
     *
64
     * @Serializer\Type("AppBundle\Entity\User")
65
     * @Expose()
66
     */
67
    protected $user;
68
69
    /**
70
     * UserOrder constructor.
71
     */
72
    public function __construct(User $user)
73
    {
74
        $this->id = Uuid::uuid4();
75
        $this->status = self::STATUS_OPENED;
76
        $this->user = $user;
77
        $this->tickets = new ArrayCollection();
78
    }
79
80
    /**
81
     * @return Uuid
82
     */
83
    public function getId(): Uuid
84
    {
85
        return $this->id;
86
    }
87
88
    /**
89
     * @return Ticket[]|ArrayCollection
90
     */
91
    public function getTickets()
92
    {
93
        return $this->tickets;
94
    }
95
96
    /**
97
     * @param Ticket[]|ArrayCollection $tickets
98
     *
99
     * @return $this
100
     */
101
    public function setTickets($tickets)
102
    {
103
        $this->tickets = $tickets;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getStatus(): string
112
    {
113
        return $this->status;
114
    }
115
116
    /**
117
     * @param String $status
118
     *
119
     * @return $this
120
     */
121
    public function setStatus($status)
122
    {
123
        $this->status = $status;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Add Ticket
130
     *
131
     * @param Ticket $ticket
132
     *
133
     * @return $this
134
     */
135
    public function addTicket(Ticket $ticket)
136
    {
137
        if ($this->isStatusPaid()) {
138
            throw new \InvalidArgumentException('Order already paid. Impossible to add new ticket.');
139
        }
140
        $this->tickets[] = $ticket;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param Ticket $ticket
147
     *
148
     * @return $this
149
     */
150
    public function removeTicket(Ticket $ticket)
151
    {
152
        if ($this->isStatusPaid()) {
153
            throw new \InvalidArgumentException('Order already pai. Impossible to remove the ticket.');
154
        }
155
        $this->tickets->removeElement($ticket);
156
157
        return $this;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    private function isStatusPaid(): bool
164
    {
165
        return $this->status === self::STATUS_PAID;
166
    }
167
168
    /**
169
     * @return array
170
     */
171
    public static function getStatuses(): array
172
    {
173
        return [
174
            self::STATUS_OPENED,
175
            self::STATUS_CLOSED,
176
            self::STATUS_PAID,
177
            self::STATUS_PENDING,
178
            self::STATUS_REJECTED,
179
        ];
180
    }
181
182
    /**
183
     * @return User
184
     */
185
    public function getUser(): User
186
    {
187
        return $this->user;
188
    }
189
190
    /**
191
     * @param $user
192
     *
193
     * @return UserOrder
194
     */
195
    public function setUser(User $user)
196
    {
197
        $this->user = $user;
198
199
        return $this;
200
    }
201
}
202