Completed
Push — master ( e7429e...db9c88 )
by Julito
11:10
created

ResourceControllerTrait::getResourceParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 21
rs 9.7666
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\ResourceRepository;
13
use Doctrine\ORM\EntityNotFoundException;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
16
17
trait ResourceControllerTrait
18
{
19
    public function getRepositoryFromRequest(Request $request): ResourceRepository
20
    {
21
        $tool = $request->get('tool');
22
        $type = $request->get('type');
23
24
        return $this->getRepository($tool, $type);
25
    }
26
27
    public function getResourceRepositoryFactory(): ResourceFactory
28
    {
29
        return $this->container->get('resource_factory');
30
    }
31
32
    public function getRepository($tool, $type): ResourceRepository
33
    {
34
        $name = $this->getResourceRepositoryFactory()->getRepository($tool, $type);
35
36
        return $this->container->get($name);
37
    }
38
39
    public function denyAccessUnlessValidResource(AbstractResource $resource)
40
    {
41
        if (null === $resource) {
42
            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

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

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

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

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

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

87
                    $parentResourceNode = $this->/** @scrutinizer ignore-call */ getUser()->getResourceNode();
Loading history...
88
                }
89
            }
90
        } else {
91
            $repo = $this->getDoctrine()->getRepository('ChamiloCoreBundle: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

91
            $repo = $this->/** @scrutinizer ignore-call */ getDoctrine()->getRepository('ChamiloCoreBundle:ResourceNode');
Loading history...
92
            $parentResourceNode = $repo->find($parentNodeId);
93
        }
94
95
        if (null === $parentResourceNode) {
96
            throw new AccessDeniedException();
97
        }
98
99
        return $parentResourceNode;
100
    }
101
102
    private function setBreadCrumb(Request $request, ResourceNode $resourceNode)
103
    {
104
        return false;
105
        $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...
106
        $resourceNodeId = $request->get('id');
107
        $routeParams = $this->getResourceParams($request);
108
        $baseNodeId = $this->getCourse()->getResourceNode()->getId();
109
110
        if (!empty($resourceNodeId)) {
111
            $breadcrumb = $this->getBreadCrumb();
112
            $toolParams = $routeParams;
113
            $toolParams['id'] = null;
114
115
            // Root tool link
116
            $breadcrumb->addChild(
117
                $this->trans($tool),
118
                [
119
                    'uri' => $this->generateUrl('chamilo_core_resource_index', $toolParams),
120
                ]
121
            );
122
123
            $repo = $this->getRepositoryFromRequest($request);
124
            $settings = $repo->getResourceSettings();
125
126
            /** @var ResourceInterface $originalResource */
127
            //$originalResource = $repo->findOneBy(['resourceNode' => $resourceNodeId]);
128
            if (null === $resourceNode) {
129
                return;
130
            }
131
            //var_dump($resourceNode->getTitle());            throw new \Exception('22');
132
            $parentList = $resourceNode->getPathForDisplayToArray($baseNodeId);
133
134
//            var_dump($originalParent->getPath(), $originalParent->getPathForDisplay());
135
136
//            $parentList = [];
137
            /*          while (null !== $parent) {
138
                          if ($type !== $parent->getResourceType()->getName()) {
139
                              break;
140
                          }
141
                          $parent = $parent->getParent();
142
                          if ($parent) {
143
                              $resource = $repo->findOneBy(['resourceNode' => $parent->getId()]);
144
                              if ($resource) {
145
                                  $parentList[] = $resource;
146
                              }
147
                          }
148
                      }
149
                      $parentList = array_reverse($parentList);
150
                      foreach ($parentList as $item) {
151
                          $params = $routeParams;
152
                          $params['id'] = $item->getResourceNode()->getId();
153
                          $breadcrumb->addChild(
154
                              $item->getResourceName(),
155
                              [
156
                                  'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
157
                              ]
158
                          );
159
                      }*/
160
161
            foreach ($parentList as $id => $title) {
162
                $params = $routeParams;
163
                $params['id'] = $id;
164
                $breadcrumb->addChild(
165
                    $title,
166
                    [
167
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
168
                    ]
169
                );
170
            }
171
172
            $params = $routeParams;
173
            $params['id'] = $resourceNode->getId();
174
            if (false === $settings->isAllowNodeCreation() || $resourceNode->hasResourceFile()) {
175
                $breadcrumb->addChild($resourceNode->getTitle());
176
            } else {
177
                $breadcrumb->addChild(
178
                    $resourceNode->getTitle(),
179
                    [
180
                        'uri' => $this->generateUrl('chamilo_core_resource_list', $params),
181
                    ]
182
                );
183
            }
184
        }
185
    }
186
}
187