Passed
Push — master ( f8f106...a015d1 )
by Julito
08:38
created

ResourceControllerTrait::setBreadCrumb()   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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Traits;
8
9
use Chamilo\CoreBundle\Entity\ResourceInterface;
10
use Chamilo\CoreBundle\Entity\ResourceNode;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Repository\ResourceFactory;
13
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
14
use Chamilo\CoreBundle\Repository\ResourceRepository;
15
use Doctrine\ORM\EntityNotFoundException;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
20
trait ResourceControllerTrait
21
{
22
    /**
23
     * @var ContainerInterface
24
     */
25
    protected $container;
26
27
    public function getRepositoryFromRequest(Request $request): ResourceRepository
28
    {
29
        $tool = $request->get('tool');
30
        $type = $request->get('type');
31
32
        return $this->getRepository($tool, $type);
33
    }
34
35
    public function getResourceNodeRepository(): ResourceNodeRepository
36
    {
37
        return $this->container->get(ResourceNodeRepository::class);
38
    }
39
40
    public function getResourceRepositoryFactory(): ResourceFactory
41
    {
42
        return $this->container->get(ResourceFactory::class);
43
    }
44
45
    public function getRepository(string $tool, string $type): ResourceRepository
46
    {
47
        return $this->getResourceRepositoryFactory()->getRepositoryService($tool, $type);
48
    }
49
50
    public function denyAccessUnlessValidResource(ResourceInterface $resource = null): void
51
    {
52
        if (null === $resource) {
53
            throw new EntityNotFoundException($this->trans("Resource doesn't exists."));
0 ignored issues
show
Bug introduced by
It seems like trans() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            throw new EntityNotFoundException($this->/** @scrutinizer ignore-call */ trans("Resource doesn't exists."));
Loading history...
54
        }
55
56
        $resourceNode = $resource->getResourceNode();
57
58
        if (null === $resourceNode) {
59
            throw new EntityNotFoundException($this->trans("Resource doesn't have a node."));
60
        }
61
    }
62
63
    public function getResourceParams(Request $request): array
64
    {
65
        $tool = $request->get('tool');
66
        $type = $request->get('type');
67
        $id = (int) $request->get('id');
68
69
        $courseId = null;
70
        $sessionId = null;
71
72
        if ($this->hasCourse()) {
0 ignored issues
show
Bug introduced by
It seems like hasCourse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        if ($this->/** @scrutinizer ignore-call */ hasCourse()) {
Loading history...
73
            $courseId = $this->getCourse()->getId();
0 ignored issues
show
Bug introduced by
It seems like getCourse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $courseId = $this->/** @scrutinizer ignore-call */ getCourse()->getId();
Loading history...
74
            $session = $this->getCourseSession();
0 ignored issues
show
Bug introduced by
It seems like getCourseSession() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            /** @scrutinizer ignore-call */ 
75
            $session = $this->getCourseSession();
Loading history...
75
            $sessionId = $session ? $session->getId() : 0;
76
        }
77
78
        return [
79
            'id' => $id,
80
            'tool' => $tool,
81
            'type' => $type,
82
            'cid' => $courseId,
83
            'sid' => $sessionId,
84
        ];
85
    }
86
87
    protected function getParentResourceNode(Request $request): ResourceNode
88
    {
89
        $parentNodeId = $request->get('id');
90
91
        $parentResourceNode = null;
92
        if (empty($parentNodeId)) {
93
            if ($this->hasCourse()) {
94
                $parentResourceNode = $this->getCourse()->getResourceNode();
95
            } elseif ($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
96
                /** @var User $user */
97
                $user = $this->getUser();
98
                if ($user) {
0 ignored issues
show
introduced by
$user is of type Chamilo\CoreBundle\Entity\User, thus it always evaluated to true.
Loading history...
99
                    $parentResourceNode = $user->getResourceNode();
100
                }
101
            }
102
        } else {
103
            $repo = $this->container->get('doctrine')->getRepository(ResourceNode::class);
104
            $parentResourceNode = $repo->find($parentNodeId);
105
        }
106
107
        if (null === $parentResourceNode) {
108
            throw new AccessDeniedException();
109
        }
110
111
        return $parentResourceNode;
112
    }
113
114
    /**
115
     * @return User|null
116
     */
117
    protected function getUser()
118
    {
119
        /*if (!$this->container->has('security.token_storage')) {
120
            throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
121
        }*/
122
123
        if (null === $token = $this->container->get('security.token_storage')->getToken()) {
124
            return null;
125
        }
126
127
        /** @var User $user */
128
        $user = $token->getUser();
129
130
        if (!\is_object($user)) {
131
            // e.g. anonymous authentication
132
            return null;
133
        }
134
135
        return $user;
136
    }
137
}
138