Completed
Pull Request — master (#141)
by
unknown
34:11 queued 22:37
created

CustomerOrder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 182
ccs 0
cts 35
cp 0
rs 10
c 2
b 1
f 0
wmc 14
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getId() 0 4 1
A getTickets() 0 4 1
A setTickets() 0 6 1
A getStatus() 0 4 1
A setStatus() 0 6 1
A addTicket() 0 9 2
A removeTicket() 0 9 2
A isStatusPaid() 0 4 1
A getStatuses() 0 10 1
A setCustomer() 0 6 1
A getCustomer() 0 4 1
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="customerOrder",
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
     */
63
    protected $customer;
64
65
    /**
66
     * CustomerOrder constructor.
67
     */
68
    public function __construct(Customer $customer)
69
    {
70
        $this->status = self::STATUS_OPENED;
71
        $this->tickets = new ArrayCollection();
72
        $this->customer = $customer;
73
    }
74
75
    /**
76
     * @return Uuid
77
     */
78
    public function getId(): Uuid
79
    {
80
        return $this->id;
81
    }
82
83
    /**
84
     * @return Ticket[]|ArrayCollection
85
     */
86
    public function getTickets()
87
    {
88
        return $this->tickets;
89
    }
90
91
    /**
92
     * @param Ticket[]|ArrayCollection $tickets
93
     *
94
     * @return CustomerOrder
95
     */
96
    public function setTickets($tickets)
97
    {
98
        $this->tickets = $tickets;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getStatus(): string
107
    {
108
        return $this->status;
109
    }
110
111
    /**
112
     * @param String $status
113
     *
114
     * @return CustomerOrder
115
     */
116
    public function setStatus($status)
117
    {
118
        $this->status = $status;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Add Ticket
125
     *
126
     * @param Ticket $ticket
127
     *
128
     * @return CustomerOrder
129
     */
130
    public function addTicket(Ticket $ticket)
131
    {
132
        if ($this->isStatusPaid()) {
133
            throw new \InvalidArgumentException('Order already paid. Impossible to add new ticket.');
134
        }
135
        $this->tickets[] = $ticket;
136
137
        return $this;
138
    }
139
140
    /**
141
     * @param Ticket $ticket
142
     *
143
     * @return CustomerOrder
144
     */
145
    public function removeTicket(Ticket $ticket)
146
    {
147
        if ($this->isStatusPaid()) {
148
            throw new \InvalidArgumentException('Order already pai. Impossible to remove the ticket.');
149
        }
150
        $this->tickets->removeElement($ticket);
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    private function isStatusPaid(): bool
159
    {
160
        return $this->status === self::STATUS_PAID;
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    public static function getStatuses(): array
167
    {
168
        return [
169
            self::STATUS_OPENED,
170
            self::STATUS_CLOSED,
171
            self::STATUS_PAID,
172
            self::STATUS_PENDING,
173
            self::STATUS_REJECTED,
174
        ];
175
    }
176
177
    /**
178
     * Set customer
179
     *
180
     * @param Customer $customer
181
     *
182
     * @return CustomerOrder
183
     */
184
    public function setCustomer(Customer $customer = null)
185
    {
186
        $this->customer = $customer;
187
188
        return $this;
189
    }
190
191
    /**
192
     * Get customer
193
     *
194
     * @return \AppBundle\Entity\Customer
195
     */
196
    public function getCustomer()
197
    {
198
        return $this->customer;
199
    }
200
}
201