Passed
Push — master ( 8276d9...eeb046 )
by Julito
11:16
created

getRepositoryFromRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Traits;
6
7
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
8
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
9
use Chamilo\CoreBundle\Repository\ResourceRepository;
10
use Chamilo\UserBundle\Entity\User;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
15
trait ResourceControllerTrait
16
{
17
    public function getRepositoryFromRequest(Request $request): ResourceRepository
18
    {
19
        $tool = $request->get('tool');
20
        $type = $request->get('type');
21
22
        return $this->getRepository($tool, $type);
23
    }
24
25
    public function getRepository($tool, $type): ResourceRepository
26
    {
27
        return $this->getResourceRepositoryFactory()->createRepository($tool, $type);
0 ignored issues
show
Bug introduced by
It seems like getResourceRepositoryFactory() 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

27
        return $this->/** @scrutinizer ignore-call */ getResourceRepositoryFactory()->createRepository($tool, $type);
Loading history...
28
    }
29
30
    public function denyAccessUnlessValidResource(AbstractResource $resource)
31
    {
32
        if (null === $resource) {
33
            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

33
            throw new NotFoundHttpException($this->/** @scrutinizer ignore-call */ trans('Resource doesn\'t exists.'));
Loading history...
34
        }
35
36
        $resourceNode = $resource->getResourceNode();
37
38
        if (null === $resourceNode) {
39
            throw new NotFoundHttpException($this->trans('Resource doesn\'t have a node.'));
40
        }
41
    }
42
43
    protected function getParentResourceNode(Request $request): ResourceNode
44
    {
45
        $parentNodeId = $request->get('id');
46
47
        $parentResourceNode = null;
48
        if (empty($parentNodeId)) {
49
            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

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

50
                $parentResourceNode = $this->/** @scrutinizer ignore-call */ getCourse()->getResourceNode();
Loading history...
51
            } else {
52
                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

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

54
                    $parentResourceNode = $this->/** @scrutinizer ignore-call */ getUser()->getResourceNode();
Loading history...
55
                }
56
            }
57
        } else {
58
            $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

58
            $repo = $this->/** @scrutinizer ignore-call */ getDoctrine()->getRepository('ChamiloCoreBundle:Resource\ResourceNode');
Loading history...
59
            $parentResourceNode = $repo->find($parentNodeId);
60
        }
61
62
        if (null === $parentResourceNode) {
63
            throw new AccessDeniedException();
64
        }
65
66
        return $parentResourceNode;
67
    }
68
69
    public function getResourceParams(Request $request): array
70
    {
71
        $tool = $request->get('tool');
72
        $type = $request->get('type');
73
        $id = (int) $request->get('id');
74
75
        $courseId = null;
76
        $sessionId = null;
77
78
        if ($this->hasCourse()) {
79
            $courseId = $this->getCourse()->getId();
80
            $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

80
            /** @scrutinizer ignore-call */ 
81
            $session = $this->getCourseSession();
Loading history...
81
            $sessionId = $session ? $session->getId() : 0;
82
        }
83
84
        return [
85
            'id' => $id,
86
            'tool' => $tool,
87
            'type' => $type,
88
            'cid' => $courseId,
89
            'sid' => $sessionId,
90
        ];
91
    }
92
}
93