Ticket   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Test Coverage

Coverage 96.23%

Importance

Changes 0
Metric Value
wmc 21
dl 0
loc 227
ccs 51
cts 53
cp 0.9623
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getReference() 0 3 1
A isClosed() 0 3 1
A __construct() 0 11 2
A getCategory() 0 3 1
A changeCategory() 0 7 2
A isReopened() 0 3 1
A getFirstMessage() 0 3 1
A getLastMessage() 0 3 1
A getMessages() 0 3 1
A reopen() 0 13 3
A isAnswered() 0 3 1
A getStatus() 0 3 1
A close() 0 3 1
A answer() 0 13 3
A isNew() 0 3 1
1
<?php
2
3
namespace Matks\Bundle\CustomerSupportBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
7
use Symfony\Component\Validator\Constraints as Assert;
8
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9
10
use Matks\Bundle\CustomerSupportBundle\Model\TicketInterface;
11
use Matks\Bundle\CustomerSupportBundle\Model\MessageInterface;
12
use Matks\Bundle\CustomerSupportBundle\Model\CategoryInterface;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use LogicException;
16
17
/**
18
 * Ticket entity
19
 *
20
 * @ORM\Entity(repositoryClass="Matks\Bundle\CustomerSupportBundle\Repository\TicketRepository")
21
 * @ORM\Table(name="tickets")
22
 * @UniqueEntity(fields="reference", message="this reference already exists")
23
 *
24
 * @author Mathieu Ferment <[email protected]>
25
 */
26
class Ticket implements TicketInterface
27
{
28
    use ORMBehaviors\Timestampable\Timestampable;
29
30
    /**
31
     * @var int
32
     *
33
     * @ORM\Id
34
     * @ORM\Column(type="integer")
35
     * @ORM\GeneratedValue(strategy="AUTO")
36
     */
37
    private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
38
39
    /**
40
     * @var CategoryInterface
41
     *
42
     * @ORM\ManyToOne(
43
     *      targetEntity="Matks\Bundle\CustomerSupportBundle\Model\CategoryInterface",
44
     *      inversedBy="tickets"
45
     * )
46
     * @Assert\Valid()
47
     */
48
    private $category;
49
50
    /**
51
     * @var string
52
     *
53
     * @ORM\Column(type="string", length=255)
54
     * @Assert\NotBlank()
55
     */
56
    private $status;
57
58
    /**
59
     * Ticket unique reference
60
     *
61
     * @var string
62
     *
63
     * @ORM\Column(type="string", length=255, unique=true)
64
     * @Assert\NotBlank()
65
     */
66
    private $reference;
67
68
    /**
69
     * @var MessageInterface[]
70
     *
71
     * @ORM\OneToMany(
72
     *     targetEntity="Matks\Bundle\CustomerSupportBundle\Model\MessageInterface",
73
     *     mappedBy="messages",
74
     *     cascade={"persist", "remove", "merge"}
75
     * )
76
     * @Assert\Valid()
77
     */
78
    private $messages;
79
80
81
    /**
82
     * Constructor
83
     *
84
     * @param string            $reference
85
     * @param CategoryInterface $category
86
     * @param MessageInterface  $message
87
     */
88 12
    public function __construct($reference, CategoryInterface $category, MessageInterface $message)
89
    {
90 12
        if (!$message->getAuthor()->isACustomer()) {
91 1
            throw new LogicException("Only customers can open a ticket");
92
        }
93
94 11
        $this->reference = $reference;
95 11
        $this->status    = TicketInterface::STATUS_NEW;
96 11
        $this->category  = $category;
97 11
        $this->messages  = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type Matks\Bundle\CustomerSup...odel\MessageInterface[] of property $messages.

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..

Loading history...
98 11
        $this->messages->add($message);
99 11
    }
100
101
    /**
102
     * Answer to customer ticket
103
     *
104
     * @param MessageInterface
105
     */
106 5
    public function answer(MessageInterface $message)
107
    {
108 5
        if ($this->isClosed()) {
109 1
            throw new LogicException("Cannot answer a closed ticket");
110
        }
111
112 4
        if ($message->getAuthor()->isACustomer()) {
113 1
            throw new LogicException("Only company users can answer a ticket");
114
        }
115
116 3
        $this->messages->add($message);
117
118 3
        $this->status = TicketInterface::STATUS_ANSWERED;
119 3
    }
120
121
    /**
122
     * Add new customer message to a ticket
123
     *
124
     * @param MessageInterface
125
     */
126 3
    public function reopen(MessageInterface $message)
127
    {
128 3
        if ($this->isClosed()) {
129 1
            throw new LogicException("Cannot reopen a closed ticket");
130
        }
131
132 2
        if (!$message->getAuthor()->isACustomer()) {
133 1
            throw new LogicException("Only customers can reopen a ticket");
134
        }
135
136 1
        $this->messages->add($message);
137
138 1
        $this->status = TicketInterface::STATUS_REOPENED;
139 1
    }
140
141
    /**
142
     * Close a ticket in order to prevent future answers
143
     */
144 3
    public function close()
145
    {
146 3
        $this->status = TicketInterface::STATUS_CLOSED;
147 3
    }
148
149
    /**
150
     * Change category
151
     *
152
     * @param CategoryInterface $category
153
     */
154 2
    public function changeCategory(CategoryInterface $category)
155
    {
156 2
        if (!$category->isActive()) {
157 1
            throw new LogicException("Cannot set new ticket category for an inactive category");
158
        }
159
160 1
        $this->category = $category;
161 1
    }
162
163
    /**
164
     * Get unique reference
165
     *
166
     * @return string
167
     */
168 1
    public function getReference()
169
    {
170 1
        return $this->reference;
171
    }
172
173
    /**
174
     * Get related category
175
     *
176
     * @return CategoryInterface
177
     */
178 1
    public function getCategory()
179
    {
180 1
        return $this->category;
181
    }
182
183
    /**
184
     * Get messages
185
     *
186
     * @return MessageInterface[]
187
     */
188
    public function getMessages()
189
    {
190
        return $this->messages;
191
    }
192
193
    /**
194
     * Get first message
195
     *
196
     * @return MessageInterface
197
     */
198 3
    public function getFirstMessage()
199
    {
200 3
        return $this->messages->first();
201
    }
202
203
    /**
204
     * Get last message
205
     *
206
     * @return MessageInterface
207
     */
208 3
    public function getLastMessage()
209
    {
210 3
        return $this->messages->last();
211
    }
212
213
    /**
214
     * Get status
215
     *
216
     * @return string
217
     */
218 4
    public function getStatus()
219
    {
220 4
        return $this->status;
221
    }
222
223
    /**
224
     * @return bool
225
     */
226 1
    public function isNew()
227
    {
228 1
        return TicketInterface::STATUS_NEW === $this->status;
229
    }
230
231
    /**
232
     * @return bool
233
     */
234 1
    public function isAnswered()
235
    {
236 1
        return TicketInterface::STATUS_ANSWERED === $this->status;
237
    }
238
239
    /**
240
     * @return bool
241
     */
242 1
    public function isReopened()
243
    {
244 1
        return TicketInterface::STATUS_REOPENED === $this->status;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250 7
    public function isClosed()
251
    {
252 7
        return TicketInterface::STATUS_CLOSED === $this->status;
253
    }
254
}
255