Completed
Push — master ( 9f183a...8cd85a )
by Julito
13:16
created

AbstractResource::getResourceNode()   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 Doctrine\Common\Collections\Criteria;
7
use Doctrine\ORM\Event\LifecycleEventArgs;
8
use Doctrine\ORM\Mapping as ORM;
9
use Sylius\Component\Resource\Model\ResourceInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Chamilo\CoreBundle\Entit...ource\ResourceInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
11
/**
12
 * @ORM\MappedSuperclass
13
 * @ORM\HasLifecycleCallbacks
14
 */
15
abstract class AbstractResource implements ResourceInterface
16
{
17
    /**
18
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", cascade={"remove"}, orphanRemoval=true)
19
     * @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
20
     */
21
    public $resourceNode;
22
23
    /**
24
     * @return string
25
     */
26
    abstract public function getResourceName(): string;
27
28
    /**
29
     * @ORM\PostUpdate()
30
     *
31
     * @param LifecycleEventArgs $args
32
     */
33
    public function postUpdate(LifecycleEventArgs $args)
34
    {
35
        $em = $args->getEntityManager();
36
        // Updates resource node name with the resource name.
37
        $node = $this->getResourceNode();
38
        $name = $this->getResourceName();
39
        $node->setName($name);
40
41
        if ($node->hasResourceFile()) {
42
            // Update file name if exists too.
43
            $node->getResourceFile()->setOriginalName($name);
44
        }
45
46
        $em->persist($node);
47
        $em->flush();
48
    }
49
50
    /**
51
     * @param ResourceNode $resourceNode
52
     *
53
     * @return $this
54
     */
55
    public function setResourceNode(ResourceNode $resourceNode): self
56
    {
57
        $this->resourceNode = $resourceNode;
58
59
        return $this;
60
    }
61
62
    /**
63
     * @return ResourceNode
64
     */
65
    public function getResourceNode(): ResourceNode
66
    {
67
        return $this->resourceNode;
68
    }
69
70
    /**
71
     * @return ResourceNode
72
     */
73
    public function getResourceNodeIllustration()
74
    {
75
        $node = $this->getResourceNode();
76
        // @todo also filter by the resource type = Illustration
77
        $criteria = Criteria::create()->where(
78
            Criteria::expr()->eq('name', 'illustration')
79
        );
80
81
        $illustration = $node->getChildren()->matching($criteria)->first();
82
83
        return $illustration;
84
    }
85
}
86