Completed
Push — master ( a34f18...c6c5e0 )
by Julito
15:01
created

ResourceNode   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 471
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 88
dl 0
loc 471
rs 8.8
c 1
b 0
f 0
wmc 45

33 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreator() 0 3 1
A hasResourceFile() 0 3 1
A setResourceType() 0 5 1
A getResourceLinks() 0 3 1
A isCourseNode() 0 3 1
A isIllustrationNode() 0 3 1
A getResourceFile() 0 3 1
A getCourse() 0 3 1
A isResourceFileAVideo() 0 10 3
A getResourceType() 0 3 1
A getPathForDisplay() 0 3 1
A getChildren() 0 3 1
A getParent() 0 3 1
A convertPathForDisplay() 0 22 3
A isResourceFileAnImage() 0 10 3
A getPathForCreationLog() 0 3 1
A setCreator() 0 5 1
A getId() 0 3 1
A hasEditableContent() 0 10 3
A getPath() 0 3 1
A setParent() 0 5 1
A hasSession() 0 12 1
A setSlug() 0 9 2
A __construct() 0 3 1
A setResourceLinks() 0 5 1
A getLevel() 0 3 1
A getSlug() 0 3 1
A setId() 0 5 1
A setPathForCreationLog() 0 3 1
A getPathForDisplayRemoveBase() 0 5 1
A getIcon() 0 15 4
A __toString() 0 3 1
A setResourceFile() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like ResourceNode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ResourceNode, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Entity\Resource;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Session;
8
use Chamilo\UserBundle\Entity\User;
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\Common\Collections\Criteria;
11
use Doctrine\ORM\Mapping as ORM;
12
use Gedmo\Mapping\Annotation as Gedmo;
13
use Gedmo\Timestampable\Traits\TimestampableEntity;
14
use Symfony\Component\Validator\Constraints as Assert;
15
16
/**
17
 * Base entity for all resources.
18
 *
19
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\ResourceNodeRepository")
20
 *
21
 * @ORM\Table(name="resource_node")
22
 *
23
 * @Gedmo\Tree(type="materializedPath")
24
 */
25
class ResourceNode
26
{
27
    use TimestampableEntity;
28
29
    public const PATH_SEPARATOR = '`';
30
31
    /**
32
     * @ORM\Id
33
     * @ORM\Column(type="integer")
34
     * @ORM\GeneratedValue(strategy="AUTO")
35
     */
36
    protected $id;
37
38
    /**
39
     * @Assert\NotBlank()
40
     *
41
     * @Gedmo\TreePathSource
42
     * @ORM\Column(name="slug", type="string", length=255, nullable=true)
43
     */
44
    protected $slug;
45
46
    /**
47
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceType", inversedBy="resourceNodes")
48
     * @ORM\JoinColumn(name="resource_type_id", referencedColumnName="id", nullable=false)
49
     */
50
    protected $resourceType;
51
52
    /**
53
     * @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceLink", mappedBy="resourceNode",
54
     *                                                                                cascade={"remove"})
55
     */
56
    protected $resourceLinks;
57
58
    /**
59
     * @var ResourceFile
60
     *
61
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceFile", inversedBy="resourceNode", orphanRemoval=true)
62
     * @ORM\JoinColumn(name="resource_file_id", referencedColumnName="id", onDelete="CASCADE")
63
     */
64
    protected $resourceFile;
65
66
    /**
67
     * @ORM\ManyToOne(
68
     *     targetEntity="Chamilo\UserBundle\Entity\User", inversedBy="resourceNodes"
69
     * )
70
     * @ORM\JoinColumn(name="creator_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
71
     */
72
    protected $creator;
73
74
    /**
75
     * @Gedmo\TreeParent
76
     *
77
     * @ORM\ManyToOne(
78
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
79
     *     inversedBy="children"
80
     * )
81
     * @ORM\JoinColumns({@ORM\JoinColumn(onDelete="CASCADE")})
82
     */
83
    protected $parent;
84
85
    /**
86
     * @Gedmo\TreeLevel
87
     *
88
     * @ORM\Column(name="level", type="integer", nullable=true)
89
     */
90
    protected $level;
91
92
    /**
93
     * @var ResourceNode[]
94
     *
95
     * @ORM\OneToMany(
96
     *     targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode",
97
     *     mappedBy="parent"
98
     * )
99
     * @ORM\OrderBy({"id" = "ASC"})
100
     */
101
    protected $children;
102
103
    /**
104
     * @Gedmo\TreePath(separator="`")
105
     *
106
     * @ORM\Column(name="path", type="string", length=3000, nullable=true)
107
     */
108
    protected $path;
109
110
    /**
111
     * Shortcut to access Course resource from ResourceNode.
112
     *
113
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", mappedBy="resourceNode")
114
     */
115
    protected $course;
116
117
    /**
118
     * Shortcut to access Course resource from ResourceNode.
119
     *
120
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Illustration", mappedBy="resourceNode")
121
     */
122
    protected $illustration;
123
124
    //protected $pathForCreationLog = '';
125
126
    /**
127
     * Constructor.
128
     */
129
    public function __construct()
130
    {
131
        $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...
132
    }
133
134
    /**
135
     * @return Course
136
     */
137
    public function getCourse(): ?Course
138
    {
139
        return $this->course;
140
    }
141
142
    public function isCourseNode(): bool
143
    {
144
        return null !== $this->course;
145
    }
146
147
    public function isIllustrationNode(): bool
148
    {
149
        return null !== $this->illustration;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function __toString()
156
    {
157
        return (string) $this->getPathForDisplay();
158
    }
159
160
    /**
161
     * Returns the resource id.
162
     *
163
     * @return int
164
     */
165
    public function getId()
166
    {
167
        return $this->id;
168
    }
169
170
    /**
171
     * @param int $id
172
     *
173
     * @return $this
174
     */
175
    public function setId($id)
176
    {
177
        $this->id = $id;
178
179
        return $this;
180
    }
181
182
    /**
183
     * Returns the resource creator.
184
     *
185
     * @return User
186
     */
187
    public function getCreator(): ?User
188
    {
189
        return $this->creator;
190
    }
191
192
    public function setCreator(User $creator = null)
193
    {
194
        $this->creator = $creator;
195
196
        return $this;
197
    }
198
199
    /**
200
     * Returns the children resource instances.
201
     *
202
     * @return ArrayCollection
203
     */
204
    public function getChildren()
205
    {
206
        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...
207
    }
208
209
    /**
210
     * Sets the parent resource.
211
     *
212
     * @param ResourceNode $parent
213
     *
214
     * @return $this
215
     */
216
    public function setParent(ResourceNode $parent = null)
217
    {
218
        $this->parent = $parent;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Returns the parent resource.
225
     *
226
     * @return ResourceNode
227
     */
228
    public function getParent()
229
    {
230
        return $this->parent;
231
    }
232
233
    /**
234
     * Return the lvl value of the resource in the tree.
235
     *
236
     * @return int
237
     */
238
    public function getLevel()
239
    {
240
        return $this->level;
241
    }
242
243
    /**
244
     * Returns the "raw" path of the resource
245
     * (the path merge names and ids of all items).
246
     * Eg.: "Root-1/subdir-2/file.txt-3/".
247
     *
248
     * @return string
249
     */
250
    public function getPath()
251
    {
252
        return $this->path;
253
    }
254
255
    /**
256
     * Returns the path cleaned from its ids.
257
     * Eg.: "Root/subdir/file.txt".
258
     *
259
     * @return string
260
     */
261
    public function getPathForDisplay()
262
    {
263
        return self::convertPathForDisplay($this->path);
264
    }
265
266
    /**
267
     * @return string
268
     */
269
    public function getPathForDisplayRemoveBase(string $base)
270
    {
271
        $path = str_replace($base, '', $this->path);
272
273
        return self::convertPathForDisplay($path);
274
    }
275
276
    /**
277
     * @return mixed
278
     */
279
    public function getSlug()
280
    {
281
        return $this->slug;
282
    }
283
284
    /**
285
     * @param mixed $slug
286
     *
287
     * @return ResourceNode
288
     */
289
    public function setSlug($slug)
290
    {
291
        if (strpos(self::PATH_SEPARATOR, $slug) !== false) {
292
            throw new \InvalidArgumentException('Invalid character "'.self::PATH_SEPARATOR.'" in resource name.');
293
        }
294
295
        $this->slug = $slug;
296
297
        return $this;
298
    }
299
300
    /**
301
     * Convert a path for display: remove ids.
302
     *
303
     * @param string $path
304
     *
305
     * @return string
306
     */
307
    public static function convertPathForDisplay($path)
308
    {
309
        /*$pathForDisplay = preg_replace(
310
            '/-\d+'.self::PATH_SEPARATOR.'/',
311
            ' / ',
312
            $path
313
        );
314
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
315
            $pathForDisplay = substr_replace($pathForDisplay, '', -3);
316
        }
317
        */
318
        $pathForDisplay = preg_replace(
319
            '/-\d+'.self::PATH_SEPARATOR.'/',
320
            '/',
321
            $path
322
        );
323
324
        if ($pathForDisplay !== null && strlen($pathForDisplay) > 0) {
325
            $pathForDisplay = substr_replace($pathForDisplay, '', -1);
326
        }
327
328
        return $pathForDisplay;
329
    }
330
331
    /**
332
     * This is required for logging the resource path at the creation.
333
     * Do not use this function otherwise.
334
     */
335
    public function setPathForCreationLog($path)
336
    {
337
        $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...
338
    }
339
340
    /**
341
     * This is required for logging the resource path at the creation.
342
     * Do not use this function otherwise.
343
     *
344
     * @return type
345
     */
346
    public function getPathForCreationLog()
347
    {
348
        return $this->pathForCreationLog;
349
    }
350
351
    /**
352
     * @return ResourceType
353
     */
354
    public function getResourceType()
355
    {
356
        return $this->resourceType;
357
    }
358
359
    /**
360
     * @param ResourceType $resourceType
361
     *
362
     * @return ResourceNode
363
     */
364
    public function setResourceType($resourceType)
365
    {
366
        $this->resourceType = $resourceType;
367
368
        return $this;
369
    }
370
371
    /**
372
     * @return ArrayCollection|ResourceLink[]
373
     */
374
    public function getResourceLinks()
375
    {
376
        return $this->resourceLinks;
377
    }
378
379
    /**
380
     * @param mixed $resourceLinks
381
     *
382
     * @return ResourceNode
383
     */
384
    public function setResourceLinks($resourceLinks)
385
    {
386
        $this->resourceLinks = $resourceLinks;
387
388
        return $this;
389
    }
390
391
    /**
392
     * @param Session $session
393
     *
394
     * @return ArrayCollection
395
     */
396
    public function hasSession(Session $session = null)
397
    {
398
        $links = $this->getResourceLinks();
399
        $criteria = Criteria::create();
400
401
        $criteria->andWhere(
402
            Criteria::expr()->eq('session', $session)
403
        );
404
405
        $result = $links->matching($criteria);
406
407
        return $result;
408
    }
409
410
    /**
411
     * @return bool
412
     */
413
    public function hasResourceFile()
414
    {
415
        return $this->resourceFile !== null;
416
    }
417
418
    /**
419
     * @return ResourceFile
420
     */
421
    public function getResourceFile(): ?ResourceFile
422
    {
423
        return $this->resourceFile;
424
    }
425
426
    /**
427
     * @return bool
428
     */
429
    public function hasEditableContent()
430
    {
431
        if ($this->hasResourceFile()) {
432
            $mimeType = $this->getResourceFile()->getMimeType();
433
            if (strpos($mimeType, 'text') !== false) {
434
                return true;
435
            }
436
        }
437
438
        return false;
439
    }
440
441
    /**
442
     * @return bool
443
     */
444
    public function isResourceFileAnImage()
445
    {
446
        if ($this->hasResourceFile()) {
447
            $mimeType = $this->getResourceFile()->getMimeType();
448
            if (strpos($mimeType, 'image') !== false) {
449
                return true;
450
            }
451
        }
452
453
        return false;
454
    }
455
456
    /**
457
     * @return bool
458
     */
459
    public function isResourceFileAVideo()
460
    {
461
        if ($this->hasResourceFile()) {
462
            $mimeType = $this->getResourceFile()->getMimeType();
463
            if (strpos($mimeType, 'video') !== false) {
464
                return true;
465
            }
466
        }
467
468
        return false;
469
    }
470
471
    public function setResourceFile(ResourceFile $resourceFile): ResourceNode
472
    {
473
        $this->resourceFile = $resourceFile;
474
475
        return $this;
476
    }
477
478
    /**
479
     * @return string
480
     */
481
    public function getIcon()
482
    {
483
        $class = 'fa fa-folder';
484
        if ($this->hasResourceFile()) {
485
            $class = 'far fa-file';
486
487
            if ($this->isResourceFileAnImage()) {
488
                $class = 'far fa-file-image';
489
            }
490
            if ($this->isResourceFileAVideo()) {
491
                $class = 'far fa-file-video';
492
            }
493
        }
494
495
        return '<i class="'.$class.'"></i>';
496
    }
497
}
498