Completed
Push — master ( 9f183a...8cd85a )
by Julito
13:16
created

ResourceRepository::getFileSystem()   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
nop 0
dl 0
loc 3
rs 10
nc 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
8
use Chamilo\CoreBundle\Entity\Resource\ResourceFile;
9
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
10
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
11
use Chamilo\CoreBundle\Entity\Resource\ResourceRight;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Entity\Tool;
14
use Chamilo\CoreBundle\Entity\Usergroup;
15
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
16
use Chamilo\CourseBundle\Entity\CGroupInfo;
17
use Chamilo\UserBundle\Entity\User;
18
use Doctrine\Common\Collections\Criteria;
19
use Doctrine\ORM\EntityManager;
20
use Doctrine\ORM\Query\Expr\Join;
21
use League\Flysystem\FilesystemInterface;
22
use League\Flysystem\MountManager;
23
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
24
use Symfony\Component\HttpFoundation\File\UploadedFile;
25
26
/**
27
 * Class ResourceRepository.
28
 */
29
class ResourceRepository extends EntityRepository
30
{
31
    /**
32
     * @var EntityRepository
33
     */
34
    protected $repository;
35
36
    /**
37
     * @var FilesystemInterface
38
     */
39
    protected $fs;
40
41
    /**
42
     * @var EntityManager
43
     */
44
    protected $entityManager;
45
46
    /**
47
     * The entity class FQN.
48
     *
49
     * @var string
50
     */
51
    protected $className;
52
53
    /**
54
     * ResourceRepository constructor.
55
     *
56
     * @param EntityManager $entityManager
57
     * @param MountManager  $mountManager
58
     * @param string        $className
59
     */
60
    public function __construct(EntityManager $entityManager, MountManager $mountManager, string $className)
61
    {
62
        $this->repository = $entityManager->getRepository($className);
63
        // Flysystem mount name is saved in config/packages/oneup_flysystem.yaml
64
        $this->fs = $mountManager->getFilesystem('resources_fs');
65
    }
66
67
    /**
68
     * @return FilesystemInterface
69
     */
70
    public function getFileSystem()
71
    {
72
        return $this->fs;
73
    }
74
75
    /**
76
     * @return EntityManager
77
     */
78
    public function getEntityManager(): EntityManager
79
    {
80
        return $this->getRepository()->getEntityManager();
81
    }
82
83
    /**
84
     * @return EntityRepository
85
     */
86
    public function getRepository()
87
    {
88
        return $this->repository;
89
    }
90
91
    /**
92
     * @param mixed $id
93
     * @param null  $lockMode
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $lockMode is correct as it would always require null to be passed?
Loading history...
94
     * @param null  $lockVersion
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $lockVersion is correct as it would always require null to be passed?
Loading history...
95
     *
96
     * @return AbstractResource|null
97
     */
98
    public function find($id, $lockMode = null, $lockVersion = null)
99
    {
100
        return $this->getRepository()->find($id);
101
    }
102
103
    /**
104
     * @param array      $criteria
105
     * @param array|null $orderBy
106
     *
107
     * @return AbstractResource
108
     */
109
    public function findOneBy(array $criteria, array $orderBy = null)
110
    {
111
        return $this->getRepository()->findOneBy($criteria, $orderBy);
112
    }
113
114
    /**
115
     * @param AbstractResource $resource
116
     *
117
     * @return ResourceNode|mixed
118
     */
119
    /*public function getIllustration(AbstractResource $resource)
120
    {
121
        $node = $resource->getResourceNode();
122
        // @todo also filter by the resource type = Illustration
123
        $criteria = Criteria::create()->where(
124
            Criteria::expr()->eq('name', 'course_picture')
125
        );
126
127
        $illustration = $node->getChildren()->matching($criteria)->first();
128
129
        return $illustration;
130
    }*/
131
132
    /**
133
     * @param AbstractResource $resource
134
     * @param UploadedFile     $file
135
     *
136
     * @return ResourceFile
137
     */
138
    public function addFileToResource(AbstractResource $resource, UploadedFile $file)
139
    {
140
        $resourceNode = $resource->getResourceNode();
141
142
        if (!$resourceNode) {
0 ignored issues
show
introduced by
$resourceNode is of type Chamilo\CoreBundle\Entity\Resource\ResourceNode, thus it always evaluated to true.
Loading history...
143
            return false;
144
        }
145
146
        $resourceFile = $resourceNode->getResourceFile();
147
        if ($resourceFile === null) {
148
            $resourceFile = new ResourceFile();
149
        }
150
151
        $em = $this->getEntityManager();
152
153
        $resourceFile->setFile($file);
154
        $resourceFile->setName($resource->getResourceName());
155
        $em->persist($resourceFile);
156
        $resourceNode->setResourceFile($resourceFile);
157
        $em->persist($resourceNode);
158
        $em->flush();
159
160
        return $resourceFile;
161
    }
162
163
164
    /**
165
     * Creates a ResourceNode.
166
     *
167
     * @param AbstractResource $resource
168
     * @param User             $creator
169
     * @param AbstractResource $parent
170
     *
171
     * @return ResourceNode
172
     */
173
    public function addResourceNode(
174
        AbstractResource $resource,
175
        User $creator,
176
        AbstractResource $parent = null
177
    ): ResourceNode {
178
        $em = $this->getEntityManager();
179
180
        $resourceType = $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
181
            ['name' => $resource->getToolName()]
182
        );
183
184
        $resourceNode = new ResourceNode();
185
        $resourceNode
186
            ->setName($resource->getResourceName())
187
            ->setCreator($creator)
188
            ->setResourceType($resourceType)
189
        ;
190
191
        if ($parent !== null) {
192
            $resourceNode->setParent($parent->getResourceNode());
193
        }
194
195
        $resource->setResourceNode($resourceNode);
196
197
        $em->persist($resourceNode);
198
        $em->persist($resource);
199
200
        return $resourceNode;
201
    }
202
203
    /**
204
     * @param ResourceNode $resourceNode
205
     *
206
     * @return ResourceLink
207
     */
208
    public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
209
    {
210
        $resourceLink = new ResourceLink();
211
        $resourceLink
212
            ->setResourceNode($resourceNode)
213
            ->setPrivate(true);
0 ignored issues
show
Bug introduced by
The method setPrivate() does not exist on Chamilo\CoreBundle\Entity\Resource\ResourceLink. ( Ignorable by Annotation )

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

213
            ->/** @scrutinizer ignore-call */ setPrivate(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
214
215
        $this->getEntityManager()->persist($resourceLink);
216
        $this->getEntityManager()->flush();
217
218
        return $resourceLink;
219
    }
220
221
    /**
222
     * @param ResourceNode  $resourceNode
223
     * @param ResourceRight $right
224
     *
225
     * @return ResourceLink
226
     */
227
    public function addResourceToEveryone(ResourceNode $resourceNode, ResourceRight $right): ResourceLink
228
    {
229
        $resourceLink = new ResourceLink();
230
        $resourceLink
231
            ->setResourceNode($resourceNode)
232
            ->addResourceRight($right)
233
        ;
234
235
        $this->getEntityManager()->persist($resourceLink);
236
        $this->getEntityManager()->flush();
237
238
        return $resourceLink;
239
    }
240
241
    /**
242
     * @param ResourceNode $resourceNode
243
     * @param int          $visibility
244
     * @param Course       $course
245
     * @param Session      $session
246
     * @param CGroupInfo   $group
247
     */
248
    public function addResourceToCourse(ResourceNode $resourceNode, $visibility, $course, $session, $group): void
249
    {
250
        $visibility = (int) $visibility;
251
        if (empty($visibility)) {
252
            $visibility = ResourceLink::VISIBILITY_PUBLISHED;
253
        }
254
255
        $link = new ResourceLink();
256
        $link
257
            ->setCourse($course)
258
            ->setSession($session)
259
            ->setGroup($group)
260
            //->setUser($toUser)
261
            ->setResourceNode($resourceNode)
262
            ->setVisibility($visibility)
263
        ;
264
265
        $rights = [];
266
        switch ($visibility) {
267
            case ResourceLink::VISIBILITY_PENDING:
268
            case ResourceLink::VISIBILITY_DRAFT:
269
                $editorMask = ResourceNodeVoter::getEditorMask();
270
                $resourceRight = new ResourceRight();
271
                $resourceRight
272
                    ->setMask($editorMask)
273
                    ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
274
                ;
275
                $rights[] = $resourceRight;
276
                break;
277
        }
278
279
        if (!empty($rights)) {
280
            foreach ($rights as $right) {
281
                $link->addResourceRight($right);
282
            }
283
        }
284
285
        $em = $this->getEntityManager();
286
        $em->persist($link);
287
    }
288
289
    /**
290
     * @param ResourceNode  $resourceNode
291
     * @param Course        $course
292
     * @param ResourceRight $right
293
     *
294
     * @return ResourceLink
295
     */
296
    public function addResourceToCourse2(ResourceNode $resourceNode, Course $course, ResourceRight $right): ResourceLink
297
    {
298
        $resourceLink = new ResourceLink();
299
        $resourceLink
300
            ->setResourceNode($resourceNode)
301
            ->setCourse($course)
302
            ->addResourceRight($right);
303
        $this->getEntityManager()->persist($resourceLink);
304
        $this->getEntityManager()->flush();
305
306
        return $resourceLink;
307
    }
308
309
    /**
310
     * @param ResourceNode $resourceNode
311
     * @param User         $toUser
312
     *
313
     * @return ResourceLink
314
     */
315
    public function addResourceToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
316
    {
317
        $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
318
        $this->getEntityManager()->persist($resourceLink);
319
320
        return $resourceLink;
321
    }
322
323
    /**
324
     * @param ResourceNode $resourceNode
325
     * @param User         $toUser
326
     *
327
     * @return ResourceLink
328
     */
329
    public function addResourceNodeToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
330
    {
331
        $resourceLink = new ResourceLink();
332
        $resourceLink
333
            ->setResourceNode($resourceNode)
334
            ->setUser($toUser);
335
336
        return $resourceLink;
337
    }
338
339
    /**
340
     * @param ResourceNode  $resourceNode
341
     * @param Course        $course
342
     * @param Session       $session
343
     * @param ResourceRight $right
344
     *
345
     * @return ResourceLink
346
     */
347
    public function addResourceToSession(
348
        ResourceNode $resourceNode,
349
        Course $course,
350
        Session $session,
351
        ResourceRight $right
352
    ) {
353
        $resourceLink = $this->addResourceToCourse(
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $resourceLink is correct as $this->addResourceToCour...eNode, $course, $right) targeting Chamilo\CoreBundle\Repos...::addResourceToCourse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The call to Chamilo\CoreBundle\Repos...::addResourceToCourse() has too few arguments starting with session. ( Ignorable by Annotation )

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

353
        /** @scrutinizer ignore-call */ 
354
        $resourceLink = $this->addResourceToCourse(

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...
354
            $resourceNode,
355
            $course,
356
            $right
357
        );
358
        $resourceLink->setSession($session);
359
        $this->getEntityManager()->persist($resourceLink);
360
361
        return $resourceLink;
362
    }
363
364
    /**
365
     * @param ResourceNode  $resourceNode
366
     * @param Course        $course
367
     * @param CGroupInfo    $group
368
     * @param ResourceRight $right
369
     *
370
     * @return ResourceLink
371
     */
372
    public function addResourceToCourseGroup(
373
        ResourceNode $resourceNode,
374
        Course $course,
375
        CGroupInfo $group,
376
        ResourceRight $right
377
    ) {
378
        $resourceLink = $this->addResourceToCourse(
0 ignored issues
show
Bug introduced by
The call to Chamilo\CoreBundle\Repos...::addResourceToCourse() has too few arguments starting with session. ( Ignorable by Annotation )

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

378
        /** @scrutinizer ignore-call */ 
379
        $resourceLink = $this->addResourceToCourse(

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...
Bug introduced by
Are you sure the assignment to $resourceLink is correct as $this->addResourceToCour...eNode, $course, $right) targeting Chamilo\CoreBundle\Repos...::addResourceToCourse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
379
            $resourceNode,
380
            $course,
381
            $right
382
        );
383
        $resourceLink->setGroup($group);
384
        $this->getEntityManager()->persist($resourceLink);
385
386
        return $resourceLink;
387
    }
388
389
    /**
390
     * @param ResourceNode  $resourceNode
391
     * @param Usergroup     $group
392
     * @param ResourceRight $right
393
     *
394
     * @return ResourceLink
395
     */
396
    public function addResourceToGroup(
397
        ResourceNode $resourceNode,
398
        Usergroup $group,
399
        ResourceRight $right
400
    ) {
401
        $resourceLink = new ResourceLink();
402
        $resourceLink
403
            ->setResourceNode($resourceNode)
404
            ->setUserGroup($group)
405
            ->addResourceRight($right);
406
407
        return $resourceLink;
408
    }
409
410
    /**
411
     * @param ResourceNode $resourceNode
412
     * @param array        $userList     User id list
413
     */
414
    public function addResourceToUserList(ResourceNode $resourceNode, array $userList)
415
    {
416
        $em = $this->getEntityManager();
417
418
        if (!empty($userList)) {
419
            foreach ($userList as $userId) {
420
                $toUser = $em->getRepository('ChamiloUserBundle:User')->find($userId);
421
422
                $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
423
                $em->persist($resourceLink);
424
            }
425
        }
426
    }
427
428
    /**
429
     * @param Course           $course
430
     * @param Tool             $tool
431
     * @param AbstractResource $parent
432
     *
433
     * @return ResourceLink
434
     */
435
    public function getResourceByCourse(Course $course, Tool $tool, AbstractResource $parent = null)
436
    {
437
        $query = $this->getEntityManager()->createQueryBuilder()
438
            ->select('resource')
439
            ->from('Chamilo\CoreBundle\Entity\Resource\ResourceNode', 'node')
440
            ->innerJoin('node.links', 'links')
441
            ->innerJoin(
442
                $this->getClassName(),
443
                'resource',
444
                Join::WITH,
445
                'resource.course = links.course AND resource.resourceNode = node.id'
446
            )
447
            ->where('node.tool = :tool')
448
            ->andWhere('links.course = :course')
449
            //->where('link.cId = ?', $course->getId())
450
            //->where('node.cId = 0')
451
            //->orderBy('node');
452
            ->setParameters(
453
                [
454
                    'tool' => $tool,
455
                    'course' => $course,
456
                ]
457
            );
458
459
        if ($parent !== null) {
460
            $query->andWhere('node.parent = :parentId');
461
            $query->setParameter('parentId', $parent->getResourceNode()->getId());
462
        } else {
463
            $query->andWhere('node.parent IS NULL');
464
        }
465
466
        $query = $query->getQuery();
467
468
        /*$query = $this->getEntityManager()->createQueryBuilder()
469
            ->select('notebook')
470
            ->from('ChamiloNotebookBundle:CNotebook', 'notebook')
471
            ->innerJoin('notebook.resourceNodes', 'node')
472
            //->innerJoin('node.links', 'link')
473
            ->where('node.tool = :tool')
474
            //->where('link.cId = ?', $course->getId())
475
            //->where('node.cId = 0')
476
            //->orderBy('node');
477
            ->setParameters(array(
478
                    'tool'=> 'notebook'
479
                )
480
            )
481
            ->getQuery()
482
        ;*/
483
484
        return $query->getResult();
485
    }
486
487
    /**
488
     * @param string $tool
489
     *
490
     * @return Tool|null
491
     */
492
    public function getTool($tool)
493
    {
494
        return $this
495
            ->getEntityManager()
496
            ->getRepository('ChamiloCoreBundle:Tool')
497
            ->findOneBy(['name' => $tool]);
498
    }
499
500
    /**
501
     * @return mixed
502
     */
503
    public function create()
504
    {
505
        return new $this->className();
506
    }
507
}
508