Completed
Push — master ( 028ddc...8aadd3 )
by Alex
13:57
created

Category::setCreatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
* This file is part of the OrbitaleCmsBundle package.
5
*
6
* (c) Alexandre Rock Ancelet <[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 Orbitale\Bundle\CmsBundle\Entity;
13
14
use Behat\Transliterator\Transliterator;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\ORM\Mapping as ORM;
17
use Doctrine\ORM\Event\LifecycleEventArgs;
18
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19
20
/**
21
 * @UniqueEntity("slug")
22
 * @ORM\HasLifecycleCallbacks()
23
 * @ORM\MappedSuperclass(repositoryClass="Orbitale\Bundle\CmsBundle\Repository\CategoryRepository")
24
 */
25
abstract class Category
26
{
27
    /**
28
     * @return int
29
     */
30
    abstract public function getId();
31
32
    /**
33
     * @var string
34
     * @ORM\Column(name="name", type="string", length=255)
35
     */
36
    protected $name;
37
38
    /**
39
     * @var string
40
     * @ORM\Column(name="slug", type="string", length=255, unique=true)
41
     */
42
    protected $slug;
43
44
    /**
45
     * @var string
46
     * @ORM\Column(name="description", type="text", nullable=true)
47
     */
48
    protected $description;
49
50
    /**
51
     * @var bool
52
     * @ORM\Column(name="enabled", type="boolean")
53
     */
54
    protected $enabled = false;
55
56
    /**
57
     * @var \DateTime
58
     *
59
     * @ORM\Column(name="created_at", type="datetime")
60
     */
61
    protected $createdAt;
62
63
    /**
64
     * @var Category
65
     */
66
    protected $parent;
67
68
    /**
69
     * @var Category[]|ArrayCollection
70
     */
71
    protected $children;
72
73
    public function __toString()
74
    {
75
        return $this->name;
76
    }
77
78
    public function __construct()
79
    {
80
        $this->createdAt = new \DateTime();
81
        $this->children = new ArrayCollection();
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getName()
88
    {
89
        return $this->name;
90
    }
91
92
    /**
93
     * @param string $name
94
     *
95
     * @return Category
96
     */
97
    public function setName($name)
98
    {
99
        $this->name = $name;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getDescription()
108
    {
109
        return $this->description;
110
    }
111
112
    /**
113
     * @param string $description
114
     *
115
     * @return Category
116
     */
117
    public function setDescription($description)
118
    {
119
        $this->description = $description;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return mixed
126
     */
127
    public function getSlug()
128
    {
129
        return $this->slug;
130
    }
131
132
    /**
133
     * @param mixed $slug
134
     *
135
     * @return Category
136
     */
137
    public function setSlug($slug)
138
    {
139
        $this->slug = $slug;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public function isEnabled()
148
    {
149
        return $this->enabled;
150
    }
151
152
    /**
153
     * @param bool $enabled
154
     *
155
     * @return Category
156
     */
157
    public function setEnabled($enabled)
158
    {
159
        $this->enabled = $enabled;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return Category
166
     */
167
    public function getParent()
168
    {
169
        return $this->parent;
170
    }
171
172
    /**
173
     * @param mixed $parent
174
     *
175
     * @return Category
176
     */
177
    public function setParent(Category $parent = null)
178
    {
179
        if ($parent === $this) {
180
            // Refuse the category to have itself as parent
181
            $this->parent = null;
182
183
            return $this;
184
        }
185
        $this->parent = $parent;
186
187
        return $this;
188
    }
189
190
    /**
191
     * @param  \DateTime $createdAt
192
     * @return $this
193
     */
194
    public function setCreatedAt(\DateTime $createdAt)
195
    {
196
        $this->createdAt = $createdAt;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return \DateTime
203
     */
204
    public function getCreatedAt()
205
    {
206
        return $this->createdAt;
207
    }
208
209
    /**
210
     * @return Category[]|ArrayCollection
211
     */
212
    public function getChildren()
213
    {
214
        return $this->children;
215
    }
216
217
    /**
218
     * @param Category $child
219
     *
220
     * @return Category[]
221
     */
222
    public function addChild(Category $child)
223
    {
224
        $this->children->add($child);
225
226
        return $this;
227
    }
228
229
    /**
230
     * @param Category $child
231
     *
232
     * @return Category[]
233
     */
234
    public function removeChild(Category $child)
235
    {
236
        $this->children->removeElement($child);
237
238
        return $this;
239
    }
240
241
    /**
242
     * @param string $separator
243
     *
244
     * @return string
245
     */
246 View Code Duplication
    public function getTree($separator = '/')
247
    {
248
        $tree = '';
249
250
        $current = $this;
251
        do {
252
            $tree = $current->getSlug().$separator.$tree;
253
            $current = $current->getParent();
254
        } while ($current);
255
256
        return trim($tree, $separator);
257
    }
258
259
    /**
260
     * @ORM\PrePersist()
261
     * @ORM\PreUpdate()
262
     */
263
    public function updateSlug()
264
    {
265
        if (!$this->slug) {
266
            $this->slug = Transliterator::transliterate($this->title);
0 ignored issues
show
Bug introduced by
The property title does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
267
        }
268
    }
269
270
    /**
271
     * @ORM\PreRemove()
272
     *
273
     * @param LifecycleEventArgs $event
274
     */
275 View Code Duplication
    public function onRemove(LifecycleEventArgs $event)
276
    {
277
        $em = $event->getEntityManager();
278
        if ($this->children) {
279
            foreach ($this->children as $child) {
280
                $child->setParent(null);
281
                $em->persist($child);
282
            }
283
        }
284
        $this->enabled = false;
285
        $this->parent = null;
286
        $this->name .= '-'.$this->getId().'-deleted';
287
        $this->slug .= '-'.$this->getId().'-deleted';
288
    }
289
}
290