Passed
Push — master ( 7d0dcf...05e539 )
by Julito
10:08
created

getResourceNodeRepository()   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 0
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\Entity\AbstractResource;
8
use Chamilo\CoreBundle\Entity\ResourceInterface;
9
use Chamilo\CoreBundle\Entity\ResourceNode;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Repository\ResourceFactory;
12
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
13
use Chamilo\CoreBundle\Repository\ResourceRepository;
14
use Doctrine\ORM\EntityNotFoundException;
15
use Symfony\Component\HttpFoundation\Request;
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 getResourceNodeRepository(): ResourceNodeRepository
29
    {
30
        return $this->container->get(ResourceNodeRepository::class);
31
    }
32
33
    public function getResourceRepositoryFactory(): ResourceFactory
34
    {
35
        return $this->container->get(ResourceFactory::class);
36
    }
37
38
    public function getRepository($tool, $type): ResourceRepository
39
    {
40
        $name = $this->getResourceRepositoryFactory()->getRepositoryService($tool, $type);
41
42
        return $this->container->get($name);
43
    }
44
45
    public function denyAccessUnlessValidResource(AbstractResource $resource = null)
46
    {
47
        if (null === $resource) {
48
            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

48
            throw new EntityNotFoundException($this->/** @scrutinizer ignore-call */ trans('Resource doesn\'t exists.'));
Loading history...
49
        }
50
51
        $resourceNode = $resource->getResourceNode();
52
53
        if (null === $resourceNode) {
54
            throw new EntityNotFoundException($this->trans('Resource doesn\'t have a node.'));
55
        }
56
    }
57
58
    public function getResourceParams(Request $request): array
59
    {
60
        $tool = $request->get('tool');
61
        $type = $request->get('type');
62
        $id = (int) $request->get('id');
63
64
        $courseId = null;
65
        $sessionId = null;
66
67
        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

67
        if ($this->/** @scrutinizer ignore-call */ hasCourse()) {
Loading history...
68
            $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

68
            $courseId = $this->/** @scrutinizer ignore-call */ getCourse()->getId();
Loading history...
69
            $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

69
            /** @scrutinizer ignore-call */ 
70
            $session = $this->getCourseSession();
Loading history...
70
            $sessionId = $session ? $session->getId() : 0;
71
        }
72
73
        return [
74
            'id' => $id,
75
            'tool' => $tool,
76
            'type' => $type,
77
            'cid' => $courseId,
78
            'sid' => $sessionId,
79
        ];
80
    }
81
82
    protected function getParentResourceNode(Request $request): ResourceNode
83
    {
84
        $parentNodeId = $request->get('id');
85
86
        $parentResourceNode = null;
87
        if (empty($parentNodeId)) {
88
            if ($this->hasCourse()) {
89
                $parentResourceNode = $this->getCourse()->getResourceNode();
90
            } else {
91
                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

91
                if ($this->/** @scrutinizer ignore-call */ isGranted('IS_AUTHENTICATED_REMEMBERED')) {
Loading history...
92
                    /** @var User $user */
93
                    $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

93
                    $parentResourceNode = $this->/** @scrutinizer ignore-call */ getUser()->getResourceNode();
Loading history...
94
                }
95
            }
96
        } else {
97
            $repo = $this->getDoctrine()->getRepository(ResourceNode::class);
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

97
            $repo = $this->/** @scrutinizer ignore-call */ getDoctrine()->getRepository(ResourceNode::class);
Loading history...
98
            $parentResourceNode = $repo->find($parentNodeId);
99
        }
100
101
        if (null === $parentResourceNode) {
102
            throw new AccessDeniedException();
103
        }
104
105
        return $parentResourceNode;
106
    }
107
108
    private function setBreadCrumb(Request $request, ResourceNode $resourceNode)
109
    {
110
        return false;
111
        $tool = $request->get('tool');
0 ignored issues
show
Unused Code introduced by
$tool = $request->get('tool') is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
112
        $resourceNodeId = $request->get('id');
113
        $routeParams = $this->getResourceParams($request);
114
        $baseNodeId = $this->getCourse()->getResourceNode()->getId();
115
116
        if (!empty($resourceNodeId)) {
117
            $breadcrumb = $this->getBreadCrumb();
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),
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 === $resourceNode) {
135
                return;
136
            }
137
            //var_dump($resourceNode->getTitle());            throw new \Exception('22');
138
            $parentList = $resourceNode->getPathForDisplayToArray($baseNodeId);
139
140
//            var_dump($originalParent->getPath(), $originalParent->getPathForDisplay());
141
142
//            $parentList = [];
143
            /*          while (null !== $parent) {
144
                          if ($type !== $parent->getResourceType()->getName()) {
145
                              break;
146
                          }
147
                          $parent = $parent->getParent();
148
                          if ($parent) {
149
                              $resource = $repo->findOneBy(['resourceNode' => $parent->getId()]);
150
                              if ($resource) {
151
                                  $parentList[] = $resource;
152
                              }
153
                          }
154
                      }
155
                      $parentList = array_reverse($parentList);
156
                      foreach ($parentList as $item) {
157
                          $params = $routeParams;
158
                          $params['id'] = $item->getResourceNode()->getId();
159
                          $breadcrumb->addChild(
160
                              $item->getResourceName(),
161
                              [
162
                                  'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
163
                              ]
164
                          );
165
                      }*/
166
167
            foreach ($parentList as $id => $title) {
168
                $params = $routeParams;
169
                $params['id'] = $id;
170
                $breadcrumb->addChild(
171
                    $title,
172
                    [
173
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
174
                    ]
175
                );
176
            }
177
178
            $params = $routeParams;
179
            $params['id'] = $resourceNode->getId();
180
            if (false === $settings->isAllowNodeCreation() || $resourceNode->hasResourceFile()) {
181
                $breadcrumb->addChild($resourceNode->getTitle());
182
            } else {
183
                $breadcrumb->addChild(
184
                    $resourceNode->getTitle(),
185
                    [
186
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
187
                    ]
188
                );
189
            }
190
        }
191
    }
192
}
193