Passed
Push — master ( 83c82e...d076ea )
by Julito
09:11
created

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