Completed
Pull Request — master (#139)
by Serhii
14:07
created

CustomerOrder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 131
rs 10
wmc 12
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getId() 0 4 1
A getTickets() 0 4 1
A setTickets() 0 4 1
A getStatus() 0 4 1
A setStatus() 0 4 1
A addTicket() 0 9 2
A removeTicket() 0 7 2
A isStatusPaid() 0 4 1
A getStatuses() 0 8 1
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_PENDING  = 'pending';
24
    const STATUS_PAID     = 'paid';
25
    const STATUS_REJECTED = 'rejected';
26
27
    /**
28
     * @var Uuid
29
     *
30
     * @ORM\Column(name="id", type="uuid_binary")
31
     * @ORM\Id
32
     * @ORM\GeneratedValue(strategy="UUID")
33
     */
34
    private $id;
35
36
    /**
37
     * @var ArrayCollection|Ticket[]
38
     *
39
     * @ORM\OneToMany(
40
     *     targetEntity="AppBundle\Entity\Ticket",
41
     *     mappedBy="object",
42
     *     cascade={"persist", "remove"}
43
     * )
44
     */
45
    protected $tickets;
46
47
    /**
48
     * @var Enum
49
     * @Assert\Choice(callback="getStatuses")
50
     * @ORM\Column(name="status", type="string", columnDefinition="enum('free', 'booked', 'ordered')")
51
     * @Expose()
52
     */
53
    protected $status;
54
55
    /**
56
     * CustomerOrder constructor.
57
     */
58
    public function __construct()
59
    {
60
        $this->id = Uuid::uuid4();
61
        $this->tickets = new ArrayCollection();
62
    }
63
64
    /**
65
     * @return Uuid
66
     */
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
72
    /**
73
     * @return Ticket[]|ArrayCollection
74
     */
75
    public function getTickets()
76
    {
77
        return $this->tickets;
78
    }
79
80
    /**
81
     * @param Ticket[]|ArrayCollection $tickets
82
     */
83
    public function setTickets($tickets)
84
    {
85
        $this->tickets = $tickets;
86
    }
87
88
    /**
89
     * @return Enum
90
     */
91
    public function getStatus()
92
    {
93
        return $this->status;
94
    }
95
96
    /**
97
     * @param Enum $status
98
     */
99
    public function setStatus($status)
100
    {
101
        $this->status = $status;
102
    }
103
104
    /**
105
     * Add Ticket
106
     *
107
     * @param Ticket $ticket
108
     * @return CustomerOrder
109
     */
110
    public function addTicket(Ticket $ticket)
111
    {
112
        if ($this->isStatusPaid()) {
113
            throw new \InvalidArgumentException('Order already paid. Impossible to add new ticket.');
114
        }
115
        $this->tickets[] = $ticket;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param Ticket $ticket
122
     */
123
    public function removeTicket(Ticket $ticket)
124
    {
125
        if ($this->isStatusPaid()) {
126
            throw new \InvalidArgumentException('Order already pai. Impossible to remove the ticket.');
127
        }
128
        $this->tickets->removeElement($ticket);
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    private function isStatusPaid()
135
    {
136
        return $this->status === self::STATUS_PAID;
137
    }
138
    /**
139
     * @return array
140
     */
141
    public static function getStatuses()
142
    {
143
        return [
144
            self::STATUS_PAID,
145
            self::STATUS_PENDING,
146
            self::STATUS_REJECTED,
147
        ];
148
    }
149
}
150