Completed
Push — master ( 9fe4f1...0c649e )
by Julito
10:07
created

ResourceNode::setSlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
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\Session;
7
use Chamilo\UserBundle\Entity\User;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Criteria;
10
use Doctrine\ORM\Mapping as ORM;
11
use Gedmo\Mapping\Annotation as Gedmo;
12
use Gedmo\Timestampable\Traits\TimestampableEntity;
13
use Symfony\Component\Validator\Constraints as Assert;
14
15
/**
16
 * Base entity for all resources.
17
 *
18
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\ResourceNodeRepository")
19
 *
20
 * @ORM\Table(name="resource_node")
21
 *
22
 * @Gedmo\Tree(type="materializedPath")
23
 */
24
class ResourceNode
25
{
26
    use TimestampableEntity;
27
28
    public const PATH_SEPARATOR = '`';
29
30
    /**
31
     * @ORM\Id
32
     * @ORM\Column(type="integer")
33
     * @ORM\GeneratedValue(strategy="AUTO")
34
     */
35
    protected $id;
36
37
    /**
38
     * @Assert\NotBlank()
39
     *
40
     * @Gedmo\TreePathSource
41
     * @ORM\Column(name="slug", type="string", length=255, nullable=true)
42
     */
43
    protected $slug;
44
45
    /**
46
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceType", inversedBy="resourceNodes")
47
     * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id", nullable=false)
48
     */
49
    protected $resourceType;
50
51
    /**
52
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode",
53
     *                                                                                cascade={"remove"})
54
     */
55
    protected $resourceLinks;
56
57
    /**
58
     * @var ResourceFile
59
     *
60
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", orphanRemoval=true)
61
     * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id", onDelete="CASCADE")
62
     */
63
    protected $resourceFile;
64
65
    /**
66
     * @ORM\ManyToOne(
67
     *     targetEntity="Chamilo\UserBundle\Entity\User",
68
     *     inversedBy="resourceNodes",
69
     *     cascade={"persist"}
70
     * )
71
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
72
     */
73
    protected $creator;
74
75
    /**
76
     * @Gedmo\TreeParent
77
     *
78
     * @ORM\ManyToOne(
79
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
80
     *     inversedBy="children"
81
     * )
82
     * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")})
83
     */
84
    protected $parent;
85
86
    /**
87
     * @Gedmo\TreeLevel
88
     *
89
     * @ORM\Column(name="level", type="integer", nullable=true)
90
     */
91
    protected $level;
92
93
    /**
94
     * @var ResourceNode[]
95
     *
96
     * @ORM\OneToMany(
97
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
98
     *     mappedBy="parent"
99
     * )
100
     * @ORM\OrderBy({"id" = "ASC"})
101
     */
102
    protected $children;
103
104
    /**
105
     * @Gedmo\TreePath(separator="`")
106
     *
107
     * @ORM\Column(name="path", type="string", length=3000, nullable=true)
108
     */
109
    protected $path;
110
111
    //protected $pathForCreationLog = '';
112
113
    /**
114
     * Constructor.
115
     */
116
    public function __construct()
117
    {
118
        $this->children = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Doctrine\Common\Collections\ArrayCollection() of type Doctrine\Common\Collections\ArrayCollection is incompatible with the declared type Chamilo\CoreBundle\Entity\Resource\ResourceNode[] of property $children.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function __toString()
125
    {
126
        return (string) $this->getPathForDisplay();
127
    }
128
129
    /**
130
     * Returns the resource id.
131
     *
132
     * @return int
133
     */
134
    public function getId()
135
    {
136
        return $this->id;
137
    }
138
139
    /**
140
     * @param int $id
141
     *
142
     * @return $this
143
     */
144
    public function setId($id)
145
    {
146
        $this->id = $id;
147
148
        return $this;
149
    }
150
151
    /**
152
     * Returns the resource creator.
153
     *
154
     * @return User
155
     */
156
    public function getCreator()
157
    {
158
        return $this->creator;
159
    }
160
161
    /**
162
     * Sets the resource creator.
163
     *
164
     * @return $this
165
     */
166
    public function setCreator(User $creator)
167
    {
168
        $this->creator = $creator;
169
170
        return $this;
171
    }
172
173
    /**
174
     * Returns the children resource instances.
175
     *
176
     * @return ArrayCollection
177
     */
178
    public function getChildren()
179
    {
180
        return $this->children;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->children returns the type Chamilo\CoreBundle\Entity\Resource\ResourceNode[] which is incompatible with the documented return type Doctrine\Common\Collections\ArrayCollection.
Loading history...
181
    }
182
183
    /**
184
     * Sets the parent resource.
185
     *
186
     * @param ResourceNode $parent
187
     *
188
     * @return $this
189
     */
190
    public function setParent(ResourceNode $parent = null)
191
    {
192
        $this->parent = $parent;
193
194
        return $this;
195
    }
196
197
    /**
198
     * Returns the parent resource.
199
     *
200
     * @return ResourceNode
201
     */
202
    public function getParent()
203
    {
204
        return $this->parent;
205
    }
206
207
    /**
208
     * Return the lvl value of the resource in the tree.
209
     *
210
     * @return int
211
     */
212
    public function getLevel()
213
    {
214
        return $this->level;
215
    }
216
217
    /**
218
     * Returns the "raw" path of the resource
219
     * (the path merge names and ids of all items).
220
     * Eg.: "Root-1/subdir-2/file.txt-3/".
221
     *
222
     * @return string
223
     */
224
    public function getPath()
225
    {
226
        return $this->path;
227
    }
228
229
    /**
230
     * Returns the path cleaned from its ids.
231
     * Eg.: "Root/subdir/file.txt".
232
     *
233
     * @return string
234
     */
235
    public function getPathForDisplay()
236
    {
237
        return self::convertPathForDisplay($this->path);
238
    }
239
240
    /**
241
     * @return mixed
242
     */
243
    public function getSlug()
244
    {
245
        return $this->slug;
246
    }
247
248
    /**
249
     * @param mixed $slug
250
     *
251
     * @return ResourceNode
252
     */
253
    public function setSlug($slug)
254
    {
255
        if (strpos(self::PATH_SEPARATOR, $slug) !== false) {
256
            throw new \InvalidArgumentException('Invalid character "'.self::PATH_SEPARATOR.'" in resource name.');
257
        }
258
259
        $this->slug = $slug;
260
261
        return $this;
262
    }
263
264
    /**
265
     * Convert a path for display: remove ids.
266
     *
267
     * @param string $path
268
     *
269
     * @return string
270
     */
271
    public static function convertPathForDisplay($path)
272
    {
273
        /*$pathForDisplay = preg_replace(
274
            '/-\d+'.self::PATH_SEPARATOR.'/',
275
            ' / ',
276
            $path
277
        );
278
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
279
            $pathForDisplay = substr_replace($pathForDisplay, '', -3);
280
        }
281
        */
282
        $pathForDisplay = preg_replace(
283
            '/-\d+'.self::PATH_SEPARATOR.'/',
284
            '/',
285
            $path
286
        );
287
288
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
289
            $pathForDisplay = substr_replace($pathForDisplay, '', -1);
290
        }
291
292
        return $pathForDisplay;
293
    }
294
295
    /**
296
     * This is required for logging the resource path at the creation.
297
     * Do not use this function otherwise.
298
     */
299
    public function setPathForCreationLog($path)
300
    {
301
        $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...
302
    }
303
304
    /**
305
     * This is required for logging the resource path at the creation.
306
     * Do not use this function otherwise.
307
     *
308
     * @return type
309
     */
310
    public function getPathForCreationLog()
311
    {
312
        return $this->pathForCreationLog;
313
    }
314
315
    /**
316
     * @return ResourceType
317
     */
318
    public function getResourceType()
319
    {
320
        return $this->resourceType;
321
    }
322
323
    /**
324
     * @param ResourceType $resourceType
325
     *
326
     * @return ResourceNode
327
     */
328
    public function setResourceType($resourceType)
329
    {
330
        $this->resourceType = $resourceType;
331
332
        return $this;
333
    }
334
335
    /**
336
     * @return ArrayCollection|ResourceLink[]
337
     */
338
    public function getResourceLinks()
339
    {
340
        return $this->resourceLinks;
341
    }
342
343
    /**
344
     * @param mixed $resourceLinks
345
     *
346
     * @return ResourceNode
347
     */
348
    public function setResourceLinks($resourceLinks)
349
    {
350
        $this->resourceLinks = $resourceLinks;
351
352
        return $this;
353
    }
354
355
    /**
356
     * @param Session $session
357
     *
358
     * @return ArrayCollection
359
     */
360
    public function hasSession(Session $session = null)
361
    {
362
        $links = $this->getResourceLinks();
363
        $criteria = Criteria::create();
364
365
        $criteria->andWhere(
366
            Criteria::expr()->eq('session', $session)
367
        );
368
369
        $result = $links->matching($criteria);
370
371
        return $result;
372
    }
373
374
    /**
375
     * @return bool
376
     */
377
    public function hasResourceFile()
378
    {
379
        return $this->resourceFile !== null;
380
    }
381
382
    /**
383
     * @return ResourceFile
384
     */
385
    public function getResourceFile(): ?ResourceFile
386
    {
387
        return $this->resourceFile;
388
    }
389
390
    /**
391
     * @return bool
392
     */
393
    public function isEditable()
394
    {
395
        if ($this->hasResourceFile()) {
396
            $mimeType = $this->getResourceFile()->getMimeType();
397
            if (strpos($mimeType, 'text') !== false) {
398
                return true;
399
            }
400
        }
401
402
        return false;
403
    }
404
405
    /**
406
     * @return bool
407
     */
408
    public function isResourceFileAnImage()
409
    {
410
        if ($this->hasResourceFile()) {
411
412
            $mimeType = $this->getResourceFile()->getMimeType();
413
            if (strpos($mimeType, 'image') !== false) {
414
                return true;
415
            }
416
        }
417
418
        return false;
419
    }
420
421
    /**
422
     * @param ResourceFile $resourceFile
423
     *
424
     * @return ResourceNode
425
     */
426
    public function setResourceFile(ResourceFile $resourceFile): ResourceNode
427
    {
428
        $this->resourceFile = $resourceFile;
429
430
        return $this;
431
    }
432
}
433