Passed
Push — master ( 8cd85a...5627cd )
by Julito
09:53 queued 10s
created

IllustrationRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addIllustration() 0 12 2
A deleteIllustration() 0 7 2
A getIllustrationNodeFromResource() 0 17 1
A getIllustrationUrl() 0 17 3
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\Illustration;
7
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
8
use Chamilo\CoreBundle\Entity\Resource\ResourceFile;
9
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
10
use Chamilo\UserBundle\Entity\User;
11
use Symfony\Component\HttpFoundation\File\UploadedFile;
12
13
/**
14
 * Class IllustrationRepository.
15
 */
16
class IllustrationRepository extends ResourceRepository
17
{
18
    /**
19
     * @param AbstractResource $resource
20
     * @param User             $user
21
     * @param UploadedFile     $uploadFile
22
     *
23
     * @return ResourceFile
24
     */
25
    public function  addIllustration(AbstractResource $resource, User $user, $uploadFile)
26
    {
27
        $illustrationNode = $this->getIllustrationNodeFromResource($resource);
28
        $em = $this->getEntityManager();
29
30
        if ($illustrationNode === null) {
31
            $illustration = new Illustration();
32
            $em->persist($illustration);
33
            $illustrationNode = $this->addResourceNode($illustration, $user, $resource);
34
            $this->addResourceToEveryone($illustrationNode);
0 ignored issues
show
Bug introduced by
The call to Chamilo\CoreBundle\Repos...addResourceToEveryone() has too few arguments starting with right. ( Ignorable by Annotation )

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

34
            $this->/** @scrutinizer ignore-call */ 
35
                   addResourceToEveryone($illustrationNode);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
35
        }
36
        return $this->addFile($illustrationNode, $uploadFile);
37
    }
38
39
    /**
40
     * @param AbstractResource $resource
41
     *
42
     * @return ResourceNode
43
     */
44
    public function getIllustrationNodeFromResource(AbstractResource $resource)
45
    {
46
        $nodeRepo = $this->getResourceNodeRepository();
47
        $em = $this->getEntityManager();
48
49
        $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
50
            ['name' => $this->getShortClassName()]
51
        );
52
53
        //var_dump($resource->getResourceNode()->getId());exit;
54
55
        /** @var ResourceNode $node */
56
        $node = $nodeRepo->findOneBy(
57
            ['parent' => $resource->getResourceNode(), 'resourceType' => $resourceType]
58
        );
59
60
        return $node;
61
    }
62
63
    /**
64
     * @param AbstractResource $resource
65
     */
66
    public function deleteIllustration(AbstractResource $resource)
67
    {
68
        $node = $this->getIllustrationNodeFromResource($resource);
69
70
        if ($node !== null) {
71
            $this->getEntityManager()->remove($node);
72
            $this->getEntityManager()->flush();
73
        }
74
    }
75
76
    /**
77
     * @param AbstractResource $resource
78
     * @param string           $filter
79
     *
80
     * @return string
81
     */
82
    public function getIllustrationUrl(AbstractResource $resource, $filter = '')
83
    {
84
        $node = $this->getIllustrationNodeFromResource($resource);
85
86
        if ($node !== null) {
87
            $params = ['id' => $node->getId()];
88
            if (!empty($filter)) {
89
                $params['filter'] = $filter;
90
            }
91
92
            return $this->router->generate(
93
                'core_tool_resource',
94
                $params
95
            );
96
        }
97
98
        return '';
99
    }
100
}
101