Passed
Push — master ( 16202d...83c82e )
by Julito
08:35
created

ResourceNode::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Entity\Resource;
5
6
use Chamilo\CoreBundle\Entity\Tool;
7
use Chamilo\UserBundle\Entity\User;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Gedmo\Mapping\Annotation as Gedmo;
11
use Symfony\Component\Validator\Constraints as Assert;
12
13
/**
14
 * Base entity for all resources.
15
 *
16
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\MaterializedPathRepository")
17
 * @ORM\Table(name="resource_node")
18
 * @Gedmo\Tree(type="materializedPath")
19
 */
20
class ResourceNode
21
{
22
    public const PATH_SEPARATOR = '`';
23
24
    /**
25
     * @ORM\Id
26
     * @ORM\Column(type="integer")
27
     * @ORM\GeneratedValue(strategy="AUTO")
28
     */
29
    protected $id;
30
31
    /**
32
     * @Gedmo\TreePathSource
33
     * @ORM\Column()
34
     * @Assert\NotBlank()
35
     */
36
    protected $name;
37
38
    /**
39
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceType", inversedBy="resourceNodes")
40
     * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id")
41
     */
42
    protected $resourceType;
43
44
    /**
45
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode", cascade={"remove"})
46
     */
47
    protected $resourceLinks;
48
49
    /**
50
     * @ORM\ManyToOne(
51
     *     targetEntity="Chamilo\UserBundle\Entity\User",
52
     *     inversedBy="resourceNodes",
53
     *     cascade={"persist"}
54
     * )
55
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
56
     */
57
    protected $creator;
58
59
    /**
60
     * @Gedmo\TreeParent
61
     * @ORM\ManyToOne(
62
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
63
     *     inversedBy="children"
64
     * )
65
     * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")})
66
     */
67
    protected $parent;
68
69
    /**
70
     * @Gedmo\TreeLevel
71
     * @ORM\Column(name="level", type="integer", nullable=true)
72
     */
73
    protected $level;
74
75
    /**
76
     * @ORM\OneToMany(
77
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
78
     *     mappedBy="parent"
79
     * )
80
     * @ORM\OrderBy({"id" = "ASC"})
81
     */
82
    protected $children;
83
84
    /**
85
     * @Gedmo\TreePath(separator="`")
86
     * @ORM\Column(name="path", type="string", length=3000, nullable=true)
87
     */
88
    protected $path;
89
90
    /**
91
     * @var ResourceFile
92
     *
93
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode")
94
     * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id")
95
     */
96
    protected $resourceFile;
97
98
    /**
99
     * @ORM\Column(name="created_at", type="datetime")
100
     * @Gedmo\Timestampable(on="create")
101
     */
102
    protected $createdAt;
103
104
    /**
105
     * @ORM\Column(name="updated_at", type="datetime")
106
     * @Gedmo\Timestampable(on="update")
107
     */
108
    protected $updatedAt;
109
110
    //protected $pathForCreationLog = '';
111
112
    /**
113
     * Constructor.
114
     */
115
    public function __construct()
116
    {
117
        $this->children = new ArrayCollection();
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function __toString()
124
    {
125
        return (string) $this->getPath();
126
    }
127
128
    /**
129
     * Returns the resource id.
130
     *
131
     * @return int
132
     */
133
    public function getId()
134
    {
135
        return $this->id;
136
    }
137
138
    /**
139
     * @param int $id
140
     *
141
     * @return $this
142
     */
143
    public function setId($id)
144
    {
145
        $this->id = $id;
146
147
        return $this;
148
    }
149
150
    /**
151
     * @param \DateTime|null $updatedAt
152
     */
153
    public function setUpdatedAt(\DateTime $updatedAt = null)
154
    {
155
        $this->updatedAt = $updatedAt;
156
    }
157
158
    /**
159
     * @return mixed
160
     */
161
    public function getUpdatedAt()
162
    {
163
        return $this->updatedAt;
164
    }
165
166
    /**
167
     * @param \DateTime|null $createdAt
168
     */
169
    public function setCreatedAt(\DateTime $createdAt = null)
170
    {
171
        $this->createdAt = $createdAt;
172
    }
173
174
    /**
175
     * @return mixed
176
     */
177
    public function getCreatedAt()
178
    {
179
        return $this->createdAt;
180
    }
181
182
    /**
183
     * Returns the resource creator.
184
     *
185
     * @return User
186
     */
187
    public function getCreator()
188
    {
189
        return $this->creator;
190
    }
191
192
    /**
193
     * Sets the resource creator.
194
     *
195
     * @param User $creator
196
     *
197
     * @return $this
198
     */
199
    public function setCreator(User $creator)
200
    {
201
        $this->creator = $creator;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Returns the children resource instances.
208
     *
209
     * @return ArrayCollection
210
     */
211
    public function getChildren()
212
    {
213
        return $this->children;
214
    }
215
216
    /**
217
     * Sets the parent resource.
218
     *
219
     * @param ResourceNode $parent
220
     *
221
     * @return $this
222
     */
223
    public function setParent(ResourceNode $parent = null)
224
    {
225
        $this->parent = $parent;
226
227
        return $this;
228
    }
229
230
    /**
231
     * Returns the parent resource.
232
     *
233
     * @return AbstractResource
234
     */
235
    public function getParent()
236
    {
237
        return $this->parent;
238
    }
239
240
    /**
241
     * Return the lvl value of the resource in the tree.
242
     *
243
     * @return int
244
     */
245
    public function getLevel()
246
    {
247
        return $this->level;
248
    }
249
250
    /**
251
     * Returns the "raw" path of the resource
252
     * (the path merge names and ids of all items).
253
     * Eg.: "Root-1/subdir-2/file.txt-3/".
254
     *
255
     * @return string
256
     */
257
    public function getPath()
258
    {
259
        return $this->path;
260
    }
261
262
    /**
263
     * Returns the path cleaned from its ids.
264
     * Eg.: "Root/subdir/file.txt".
265
     *
266
     * @return
267
     */
268
    public function getPathForDisplay()
269
    {
270
        return self::convertPathForDisplay($this->path);
271
    }
272
273
    /**
274
     * Sets the resource name.
275
     *
276
     * @param string $name
277
     *
278
     * @throws an exception if the name contains the path separator ('/')
279
     *
280
     * @return $this
281
     */
282
    public function setName($name)
283
    {
284
        if (strpos(self::PATH_SEPARATOR, $name) !== false) {
285
            throw new \InvalidArgumentException(
286
                'Invalid character "'.self::PATH_SEPARATOR.'" in resource name.'
287
            );
288
        }
289
290
        $this->name = $name;
291
292
        return $this;
293
    }
294
295
    /**
296
     * Returns the resource name.
297
     *
298
     * @return string
299
     */
300
    public function getName()
301
    {
302
        return $this->name;
303
    }
304
305
    /**
306
     * Convert a path for display: remove ids.
307
     *
308
     * @param string $path
309
     *
310
     * @return string
311
     */
312
    public static function convertPathForDisplay($path)
313
    {
314
        $pathForDisplay = preg_replace(
315
            '/-\d+'.self::PATH_SEPARATOR.'/',
316
            ' / ',
317
            $path
318
        );
319
320
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
321
            $pathForDisplay = substr_replace($pathForDisplay, "", -3);
322
        }
323
324
        return $pathForDisplay;
325
    }
326
327
    /**
328
     * This is required for logging the resource path at the creation.
329
     * Do not use this function otherwise.
330
     *
331
     * @return type
332
     */
333
    public function setPathForCreationLog($path)
334
    {
335
        $this->pathForCreationLog = $path;
0 ignored issues
show
Bug Best Practice introduced by
The property pathForCreationLog does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
336
    }
337
338
    /**
339
     * This is required for logging the resource path at the creation.
340
     * Do not use this function otherwise.
341
     *
342
     * @return type
343
     */
344
    public function getPathForCreationLog()
345
    {
346
        return $this->pathForCreationLog;
347
    }
348
349
    /**
350
     * @return ResourceType
351
     */
352
    public function getResourceType()
353
    {
354
        return $this->resourceType;
355
    }
356
357
    /**
358
     * @param ResourceType $resourceType
359
     *
360
     * @return ResourceNode
361
     */
362
    public function setResourceType($resourceType)
363
    {
364
        $this->resourceType = $resourceType;
365
366
        return $this;
367
    }
368
369
    /**
370
     * @return mixed
371
     */
372
    public function getResourceLinks()
373
    {
374
        return $this->resourceLinks;
375
    }
376
377
    /**
378
     * @param mixed $resourceLinks
379
     *
380
     * @return ResourceNode
381
     */
382
    public function setResourceLinks($resourceLinks)
383
    {
384
        $this->resourceLinks = $resourceLinks;
385
386
        return $this;
387
    }
388
389
    /**
390
     * @return ResourceFile
391
     */
392
    public function getResourceFile(): ResourceFile
393
    {
394
        return $this->resourceFile;
395
    }
396
397
    /**
398
     * @param ResourceFile $resourceFile
399
     *
400
     * @return ResourceNode
401
     */
402
    public function setResourceFile(ResourceFile $resourceFile): ResourceNode
403
    {
404
        $this->resourceFile = $resourceFile;
405
406
        return $this;
407
    }
408
}
409