Passed
Push — master ( 9b28ce...1fed75 )
by Julito
11:04
created

IllustrationRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
c 0
b 0
f 0
dl 0
loc 138
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getIllustrationUrl() 0 12 2
A getIllustrationUrlFromNode() 0 19 3
A getIllustrationNodeFromParent() 0 20 1
A deleteIllustration() 0 7 2
A addIllustration() 0 35 5
A hasIllustration() 0 5 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository\Node;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\Illustration;
11
use Chamilo\CoreBundle\Entity\ResourceFile;
12
use Chamilo\CoreBundle\Entity\ResourceIllustrationInterface;
13
use Chamilo\CoreBundle\Entity\ResourceInterface;
14
use Chamilo\CoreBundle\Entity\ResourceNode;
15
use Chamilo\CoreBundle\Entity\Session;
16
use Chamilo\CoreBundle\Entity\User;
17
use Chamilo\CoreBundle\Repository\ResourceRepository;
18
use Chamilo\CourseBundle\Entity\CGroup;
19
use Doctrine\ORM\QueryBuilder;
20
use Doctrine\Persistence\ManagerRegistry;
21
use Symfony\Component\HttpFoundation\File\UploadedFile;
22
23
final class IllustrationRepository extends ResourceRepository
24
{
25
    public function __construct(ManagerRegistry $registry)
26
    {
27
        parent::__construct($registry, Illustration::class);
28
    }
29
30
    /*public function getResources(User $user, ResourceNode $parentNode, Course $course = null, Session $session = null, CGroup $group = null): QueryBuilder
31
    {
32
        $qb = $this->createQueryBuilder('resource')
33
            ->select('resource')
34
            ->innerJoin(
35
                'resource.resourceNode',
36
                'node'
37
            )
38
        ;
39
40
        $qb->andWhere('node.creator = :creator');
41
        $qb->setParameter('creator', $user);
42
43
        return $qb;
44
    }*/
45
46
    /**
47
     * @param ResourceInterface|User $resource
48
     */
49
    public function addIllustration(
50
        ResourceInterface $resource,
51
        User $creator,
52
        UploadedFile $uploadFile = null,
53
        string $crop = ''
54
    ): ?ResourceFile {
55
        if (null === $uploadFile) {
56
            return null;
57
        }
58
59
        $illustrationNode = $this->getIllustrationNodeFromParent($resource->getResourceNode());
60
        $em = $this->getEntityManager();
61
62
        if (null === $illustrationNode) {
63
            $illustration = (new Illustration())
64
                ->setCreator($creator)
65
                ->setParent($resource)
66
            ;
67
            $em->persist($illustration);
68
        } else {
69
            $illustration = $this->findOneBy([
70
                'resourceNode' => $illustrationNode,
71
            ]);
72
        }
73
74
        $file = $this->addFile($illustration, $uploadFile);
75
        if (null !== $file) {
76
            if (!empty($crop)) {
77
                $file->setCrop($crop);
78
            }
79
            $em->persist($file);
80
        }
81
        $em->flush();
82
83
        return $file;
84
    }
85
86
    public function getIllustrationNodeFromParent(ResourceNode $resourceNode): ?ResourceNode
87
    {
88
        $nodeRepo = $this->getResourceNodeRepository();
89
        $name = $this->getResourceTypeName();
90
91
        $qb = $nodeRepo->createQueryBuilder('n')
92
            ->select('node')
93
            ->from(ResourceNode::class, 'node')
94
            ->innerJoin('node.resourceType', 'type')
95
            ->innerJoin('node.resourceFile', 'file')
96
            ->where('node.parent = :parent')
97
            ->andWhere('type.name = :name')
98
            ->setParameters([
99
                'parent' => $resourceNode->getId(),
100
                'name' => $name,
101
            ])
102
            ->setMaxResults(1)
103
        ;
104
105
        return $qb->getQuery()->getOneOrNullResult();
106
    }
107
108
    public function deleteIllustration(ResourceInterface $resource): void
109
    {
110
        $node = $this->getIllustrationNodeFromParent($resource->getResourceNode());
111
112
        if (null !== $node) {
113
            $this->getEntityManager()->remove($node);
114
            $this->getEntityManager()->flush();
115
        }
116
    }
117
118
    public function hasIllustration(ResourceIllustrationInterface $resource): bool
119
    {
120
        $node = $this->getIllustrationNodeFromParent($resource->getResourceNode());
121
122
        return null !== $node;
123
    }
124
125
    /**
126
     * @param string $filter See: services.yaml parameter "glide_media_filters" to see the list of filters.
127
     */
128
    public function getIllustrationUrl(
129
        ResourceIllustrationInterface $resource,
130
        string $filter = '',
131
        int $size = 32
132
    ): string {
133
        $illustration = $this->getIllustrationUrlFromNode($resource->getResourceNode(), $filter);
134
135
        if (empty($illustration)) {
136
            $illustration = $resource->getDefaultIllustration($size);
137
        }
138
139
        return $illustration;
140
    }
141
142
    private function getIllustrationUrlFromNode(ResourceNode $node, string $filter = ''): string
143
    {
144
        $node = $this->getIllustrationNodeFromParent($node);
145
146
        if (null !== $node) {
147
            $params = [
148
                'id' => $node->getId(),
149
                'tool' => $node->getResourceType()->getTool(),
150
                'type' => $node->getResourceType()->getName(),
151
            ];
152
153
            if (!empty($filter)) {
154
                $params['filter'] = $filter;
155
            }
156
157
            return $this->getRouter()->generate('chamilo_core_resource_view', $params);
158
        }
159
160
        return '';
161
    }
162
}
163