Completed
Push — master ( b75c6e...ecc25c )
by Julito
07:54
created

ResourceNode::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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