Passed
Push — master ( eeb046...442ea1 )
by Julito
09:25 queued 26s
created

ResourceControllerTrait::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Traits;
6
7
use Chamilo\CoreBundle\Component\Utils\Glide;
8
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
9
use Chamilo\CoreBundle\Entity\Resource\ResourceInterface;
10
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
11
use Chamilo\CoreBundle\Repository\ResourceFactory;
12
use Chamilo\CoreBundle\Repository\ResourceRepository;
13
use Chamilo\UserBundle\Entity\User;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
18
trait ResourceControllerTrait
19
{
20
    public function getRepositoryFromRequest(Request $request): ResourceRepository
21
    {
22
        $tool = $request->get('tool');
23
        $type = $request->get('type');
24
25
        return $this->getRepository($tool, $type);
26
    }
27
28
    public function getResourceRepositoryFactory(): ResourceFactory
29
    {
30
        return $this->container->get('resource_factory');
31
    }
32
33
    /**
34
     * @return Glide
35
     */
36
    public function getGlide()
37
    {
38
        return $this->container->get('glide');
39
    }
40
41
    public function getRepository($tool, $type): ResourceRepository
42
    {
43
        return $this->getResourceRepositoryFactory()->createRepository($tool, $type);
44
    }
45
46
    public function denyAccessUnlessValidResource(AbstractResource $resource)
47
    {
48
        if (null === $resource) {
49
            throw new NotFoundHttpException($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

49
            throw new NotFoundHttpException($this->/** @scrutinizer ignore-call */ trans('Resource doesn\'t exists.'));
Loading history...
50
        }
51
52
        $resourceNode = $resource->getResourceNode();
53
54
        if (null === $resourceNode) {
55
            throw new NotFoundHttpException($this->trans('Resource doesn\'t have a node.'));
56
        }
57
    }
58
59
    protected function getParentResourceNode(Request $request): ResourceNode
60
    {
61
        $parentNodeId = $request->get('id');
62
63
        $parentResourceNode = null;
64
        if (empty($parentNodeId)) {
65
            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

65
            if ($this->/** @scrutinizer ignore-call */ hasCourse()) {
Loading history...
66
                $parentResourceNode = $this->getCourse()->getResourceNode();
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

66
                $parentResourceNode = $this->/** @scrutinizer ignore-call */ getCourse()->getResourceNode();
Loading history...
67
            } else {
68
                if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
0 ignored issues
show
Bug introduced by
It seems like isGranted() 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

68
                if ($this->/** @scrutinizer ignore-call */ isGranted('IS_AUTHENTICATED_REMEMBERED')) {
Loading history...
69
                    /** @var User $user */
70
                    $parentResourceNode = $this->getUser()->getResourceNode();
0 ignored issues
show
Bug introduced by
It seems like getUser() 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

70
                    $parentResourceNode = $this->/** @scrutinizer ignore-call */ getUser()->getResourceNode();
Loading history...
71
                }
72
            }
73
        } else {
74
            $repo = $this->getDoctrine()->getRepository('ChamiloCoreBundle:Resource\ResourceNode');
0 ignored issues
show
Bug introduced by
It seems like getDoctrine() 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
            $repo = $this->/** @scrutinizer ignore-call */ getDoctrine()->getRepository('ChamiloCoreBundle:Resource\ResourceNode');
Loading history...
75
            $parentResourceNode = $repo->find($parentNodeId);
76
        }
77
78
        if (null === $parentResourceNode) {
79
            throw new AccessDeniedException();
80
        }
81
82
        return $parentResourceNode;
83
    }
84
85
    public function getResourceParams(Request $request): array
86
    {
87
        $tool = $request->get('tool');
88
        $type = $request->get('type');
89
        $id = (int) $request->get('id');
90
91
        $courseId = null;
92
        $sessionId = null;
93
94
        if ($this->hasCourse()) {
95
            $courseId = $this->getCourse()->getId();
96
            $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

96
            /** @scrutinizer ignore-call */ 
97
            $session = $this->getCourseSession();
Loading history...
97
            $sessionId = $session ? $session->getId() : 0;
98
        }
99
100
        return [
101
            'id' => $id,
102
            'tool' => $tool,
103
            'type' => $type,
104
            'cid' => $courseId,
105
            'sid' => $sessionId,
106
        ];
107
    }
108
109
    private function setBreadCrumb(Request $request)
110
    {
111
        $tool = $request->get('tool');
112
        $type = $request->get('type');
113
        $resourceNodeId = $request->get('id');
114
        $routeParams = $this->getResourceParams($request);
115
116
        if (!empty($resourceNodeId)) {
117
            $breadcrumb = $this->getBreadCrumb();
0 ignored issues
show
Bug introduced by
It seems like getBreadCrumb() 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

117
            /** @scrutinizer ignore-call */ 
118
            $breadcrumb = $this->getBreadCrumb();
Loading history...
118
            $toolParams = $routeParams;
119
            $toolParams['id'] = null;
120
121
            // Root tool link
122
            $breadcrumb->addChild(
123
                $this->trans($tool),
124
                [
125
                    'uri' => $this->generateUrl('chamilo_core_resource_index', $toolParams),
0 ignored issues
show
Bug introduced by
It seems like generateUrl() 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

125
                    'uri' => $this->/** @scrutinizer ignore-call */ generateUrl('chamilo_core_resource_index', $toolParams),
Loading history...
126
                ]
127
            );
128
129
            $repo = $this->getRepositoryFromRequest($request);
130
            $settings = $repo->getResourceSettings();
131
132
            /** @var ResourceInterface $originalResource */
133
            $originalResource = $repo->findOneBy(['resourceNode' => $resourceNodeId]);
134
            if (null === $originalResource) {
135
                return;
136
            }
137
            $parent = $originalParent = $originalResource->getResourceNode();
138
139
            $parentList = [];
140
            while (null !== $parent) {
141
                if ($type !== $parent->getResourceType()->getName()) {
142
                    break;
143
                }
144
                $parent = $parent->getParent();
145
                if ($parent) {
146
                    $resource = $repo->findOneBy(['resourceNode' => $parent->getId()]);
147
                    if ($resource) {
148
                        $parentList[] = $resource;
149
                    }
150
                }
151
            }
152
153
            $parentList = array_reverse($parentList);
154
            /** @var ResourceInterface $item */
155
            foreach ($parentList as $item) {
156
                $params = $routeParams;
157
                $params['id'] = $item->getResourceNode()->getId();
158
                $breadcrumb->addChild(
159
                    $item->getResourceName(),
160
                    [
161
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
162
                    ]
163
                );
164
            }
165
166
            $params = $routeParams;
167
            $params['id'] = $originalParent->getId();
168
            if (false === $settings->isAllowNodeCreation() || $originalResource->getResourceNode()->hasResourceFile()) {
169
                $breadcrumb->addChild($originalResource->getResourceName());
170
            } else {
171
                $breadcrumb->addChild(
172
                    $originalResource->getResourceName(),
173
                    [
174
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
175
                    ]
176
                );
177
            }
178
        }
179
    }
180
}
181