Completed
Push — master ( d28136...3a5b5f )
by Julito
16:58
created

AbstractResource::getFieldName()   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 APY\DataGridBundle\Grid\Mapping as GRID;
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\Session;
9
use Doctrine\Common\Collections\Criteria;
10
use Doctrine\ORM\Mapping as ORM;
11
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...
12
13
/**
14
 * @ORM\MappedSuperclass
15
 * @ORM\HasLifecycleCallbacks
16
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\ResourceListener"})
17
 */
18
abstract class AbstractResource implements ResourceInterface
19
{
20
    /**
21
     * @GRID\Column(field="resourceNode.createdAt", title="Date added", type="datetime")
22
     *
23
     * @ORM\OneToOne(targetEntity="Chamilo\CoreBundle\Entity\Resource\ResourceNode", cascade={"remove"}, orphanRemoval=true)
24
     * @ORM\JoinColumn(name="resource_node_id", referencedColumnName="id", onDelete="CASCADE")
25
     */
26
    public $resourceNode;
27
28
    //abstract public function getResourceName(): string;
29
30
    /**
31
     * @return $this
32
     */
33
    public function setResourceNode(ResourceNode $resourceNode): self
34
    {
35
        $this->resourceNode = $resourceNode;
36
37
        return $this;
38
    }
39
40
    public function getResourceNode(): ResourceNode
41
    {
42
        return $this->resourceNode;
43
    }
44
45
    /**
46
     * @return ResourceLink
47
     */
48
    public function getCourseSessionResourceLink(Course $course, Session $session = null)
49
    {
50
        return $this->getFirstResourceLinkFromCourseSession($course, $session);
51
    }
52
53
    /**
54
     * See ResourceLink to see the visibility constants. Example: ResourceLink::VISIBILITY_DELETED.
55
     *
56
     * @return int
57
     */
58
    public function getLinkVisibility(Course $course, Session $session = null)
59
    {
60
        return $this->getCourseSessionResourceLink($course, $session)->getVisibility();
61
    }
62
63
    public function isVisible(Course $course, Session $session = null): bool
64
    {
65
        return $this->getCourseSessionResourceLink($course, $session) === ResourceLink::VISIBILITY_PUBLISHED;
66
    }
67
68
    public function getFirstResourceLinkFromCourseSession(Course $course, Session $session = null): ?ResourceLink
69
    {
70
        $criteria = Criteria::create();
71
        $criteria
72
            ->where(Criteria::expr()->eq('course', $course))
73
            ->andWhere(
74
                Criteria::expr()->eq('session', $session)
75
            );
76
        $resourceNode = $this->getResourceNode();
77
78
        $result = null;
79
        if ($resourceNode && $resourceNode->getResourceLinks()) {
80
            $result = $resourceNode->getResourceLinks()->matching($criteria)->first();
81
            if ($result) {
82
                return $result;
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    public static function getFieldName()
90
    {
91
        return 'name';
92
    }
93
94
}
95