Completed
Pull Request — master (#147)
by Ihor
13:50
created

CustomerOrder::setCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
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="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 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 Customer
60
     *
61
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Customer")
62
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", onDelete="SET NULL")
63
     */
64
    protected $customer;
65
66
    /**
67
     * CustomerOrder constructor.
68
     */
69
    public function __construct(Customer $customer)
70
    {
71
        $this->id = Uuid::uuid4();
72
        $this->status = self::STATUS_OPENED;
73
        $this->customer = $customer;
74
        $this->tickets = new ArrayCollection();
75
    }
76
77
    /**
78
     * @return Uuid
79
     */
80
    public function getId(): Uuid
81
    {
82
        return $this->id;
83
    }
84
85
    /**
86
     * @return Ticket[]|ArrayCollection
87
     */
88
    public function getTickets()
89
    {
90
        return $this->tickets;
91
    }
92
93
    /**
94
     * @param Ticket[]|ArrayCollection $tickets
95
     *
96
     * @return CustomerOrder
97
     */
98
    public function setTickets($tickets)
99
    {
100
        $this->tickets = $tickets;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getStatus(): string
109
    {
110
        return $this->status;
111
    }
112
113
    /**
114
     * @param String $status
115
     *
116
     * @return CustomerOrder
117
     */
118
    public function setStatus($status)
119
    {
120
        $this->status = $status;
121
122
        return $this;
123
    }
124
125
    /**
126
     * @return Customer
127
     */
128
    public function getCustomer()
129
    {
130
        return $this->customer;
131
    }
132
133
    /**
134
     * @param $customer
135
     *
136
     * @return CustomerOrder
137
     */
138
    public function setCustomer($customer)
139
    {
140
        $this->customer = $customer;
141
142
        return $this;
143
    }
144
145
    /**
146
     * Add Ticket
147
     *
148
     * @param Ticket $ticket
149
     *
150
     * @return CustomerOrder
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 CustomerOrder
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
     * @return array
186
     */
187
    public static function getStatuses(): array
188
    {
189
        return [
190
            self::STATUS_OPENED,
191
            self::STATUS_CLOSED,
192
            self::STATUS_PAID,
193
            self::STATUS_PENDING,
194
            self::STATUS_REJECTED,
195
        ];
196
    }
197
}
198