Completed
Push — master ( 2f124c...66b0c2 )
by Mohamed
11:36 queued 09:28
created

Category::setCreated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 2.1481
1
<?php
2
3
/*
4
 * This file is part of the Moo\FlashCardBundle package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moo\FlashCardBundle\Entity;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Symfony\Component\Validator\Constraints as Assert;
17
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
18
19
/**
20
 * Category is the entity class that represents a record from the database.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 * @ORM\Table(name="card_category")
24
 * @ORM\Entity(repositoryClass="Moo\FlashCardBundle\Repository\Category")
25
 * @ORM\HasLifecycleCallbacks()
26
 * @UniqueEntity(
27
 *     fields={"title"},
28
 *     message="There is an existing category with the same title."
29
 * )
30
 */
31
class Category
32
{
33
    /**
34
     * @var int
35
     *
36
     * @ORM\Column(name="id", type="integer", nullable=false)
37
     * @ORM\Id
38
     * @ORM\GeneratedValue(strategy="IDENTITY")
39
     */
40
    private $id;
41
42
    /**
43
     * @var string
44
     *
45
     * @ORM\Column(name="title", type="string", length=255, nullable=false,unique=true)
46
     * @Assert\NotBlank()
47
     * @Assert\Length(
48
     *      min = "5",
49
     *      max = "255",
50
     *      minMessage = "Category title must be at least {{ limit }} characters length",
51
     *      maxMessage = "Category title cannot be longer than {{ limit }} characters length"
52
     * )
53
     */
54
    private $title;
55
56
    /**
57
     * @var string
58
     *
59
     * @ORM\Column(name="description", type="text", nullable=true)
60
     */
61
    private $description;
62
63
    /**
64
     * @var \DateTime
65
     *
66
     * @ORM\Column(name="created", type="datetime", nullable=false)
67
     */
68
    private $created;
69
70
    /**
71
     * @var \DateTime
72
     *
73
     * @ORM\Column(name="updated", type="datetime", nullable=false)
74
     */
75
    private $updated;
76
77
    /**
78
     * @var int
79
     *
80
     * @ORM\Column(name="active", type="boolean", nullable=true)
81
     */
82
    private $active = false;
83
84
    /**
85
     * @var Category
86
     *
87
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children", fetch="EAGER")
88
     * @ORM\JoinColumns({
89
     *   @ORM\JoinColumn(name="parent", referencedColumnName="id")
90
     * })
91
     */
92
    private $parent;
93
94
    /**
95
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent", fetch="EAGER")
96
     */
97
    private $children;
98
99
    /**
100
     * @ORM\OneToMany(targetEntity="Card", mappedBy="category", fetch="EAGER")
101
     */
102
    private $cards;
103
104 1
    public function __construct()
105
    {
106 1
        $this->cards = new ArrayCollection();
107 1
        $this->children = new ArrayCollection();
108 1
    }
109
110
    /**
111
     * Get id
112
     *
113
     * @return int
114
     */
115
    public function getId()
116
    {
117
        return $this->id;
118
    }
119
120
    /**
121
     * Set title
122
     *
123
     * @param string $title
124
     *
125
     * @return Category
126
     */
127 1
    public function setTitle($title)
128
    {
129 1
        $this->title = $title;
130
131 1
        return $this;
132
    }
133
134
    /**
135
     * Get title
136
     *
137
     * @return string
138
     */
139 1
    public function getTitle()
140
    {
141 1
        return $this->title;
142
    }
143
144
    /**
145
     * Set description
146
     *
147
     * @param string $description
148
     *
149
     * @return Category
150
     */
151 1
    public function setDescription($description)
152
    {
153 1
        $this->description = $description;
154
155 1
        return $this;
156
    }
157
158
    /**
159
     * Get description
160
     *
161
     * @return string
162
     */
163 1
    public function getDescription()
164
    {
165 1
        return $this->description;
166
    }
167
168
    /**
169
     * Set created
170
     *
171
     * @param \DateTime $created
172
     *
173
     * @return Category
174
     */
175 1 View Code Duplication
    public function setCreated($created = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
    {
177 1
        if ($created === null) {
178
            $created = new \DateTime();
179
            $this->setUpdated($created);
180
        }
181 1
        $this->created = $created;
182
183 1
        return $this;
184
    }
185
186
    /**
187
     * Get created
188
     *
189
     * @return \DateTime
190
     */
191 1
    public function getCreated()
192
    {
193 1
        return $this->created;
194
    }
195
196
    /**
197
     * Set updated
198
     *
199
     * @param \DateTime $updated
200
     *
201
     * @return Category
202
     */
203 View Code Duplication
    public function setUpdated($updated = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        if ($updated === null) {
206
            $updated = new \DateTime();
207
        }
208
        $this->updated = $updated;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Get updated
215
     *
216
     * @return \DateTime
217
     */
218
    public function getUpdated()
219
    {
220
        return $this->updated;
221
    }
222
223
    /**
224
     * Set isActive
225
     *
226
     * @param int $isActive
227
     *
228
     * @return Category
229
     */
230 1
    public function setActive($isActive)
231
    {
232 1
        $this->active = $isActive;
233
234 1
        return $this;
235
    }
236
237
    /**
238
     * Get isActive
239
     *
240
     * @return int
241
     */
242
    public function isActive()
243
    {
244
        return $this->active;
245
    }
246
247
    /**
248
     * Set parent
249
     *
250
     * @param Category $parent
251
     *
252
     * @return Category
253
     */
254 1
    public function setParent(Category $parent = null)
255
    {
256 1
        $this->parent = $parent;
257
258 1
        return $this;
259
    }
260
261
    /**
262
     * Get parent
263
     *
264
     * @return Category
265
     */
266 1
    public function getParent()
267
    {
268 1
        return $this->parent;
269
    }
270
271
    /**
272
     * @return ArrayCollection
273
     */
274
    public function getCards()
275
    {
276
        return $this->cards;
277
    }
278
279
    /**
280
     * @ORM\PrePersist()
281
     */
282
    public function prePersist()
283
    {
284
        $this->setCreated();
285
    }
286
287
    /**
288
     * @ORM\PreUpdate()
289
     */
290
    public function preUpdate()
291
    {
292
        $this->setUpdated();
293
    }
294
295
    public function __toString()
296
    {
297
        return $this->getTitle() ?: 'New Category';
298
    }
299
}
300