Completed
Push — master ( 4874d1...5ea477 )
by Julito
10:24
created

ResourceRepository::addResourceToCourse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 6
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Repository;
5
6
use APY\DataGridBundle\Grid\Action\RowAction;
7
use APY\DataGridBundle\Grid\Row;
8
use Chamilo\CoreBundle\Entity\Course;
9
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
10
use Chamilo\CoreBundle\Entity\Resource\ResourceFile;
11
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
12
use Chamilo\CoreBundle\Entity\Resource\ResourceNode;
13
use Chamilo\CoreBundle\Entity\Resource\ResourceRight;
14
use Chamilo\CoreBundle\Entity\Resource\ResourceType;
15
use Chamilo\CoreBundle\Entity\Session;
16
use Chamilo\CoreBundle\Entity\Tool;
17
use Chamilo\CoreBundle\Entity\Usergroup;
18
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
19
use Chamilo\CoreBundle\ToolChain;
20
use Chamilo\CourseBundle\Entity\CDocument;
21
use Chamilo\CourseBundle\Entity\CGroupInfo;
22
use Chamilo\UserBundle\Entity\User;
23
use Cocur\Slugify\Slugify;
24
use Cocur\Slugify\SlugifyInterface;
25
use Doctrine\ORM\EntityManager;
26
use Doctrine\ORM\Query\Expr\Join;
27
use Doctrine\ORM\QueryBuilder;
28
use League\Flysystem\FilesystemInterface;
29
use League\Flysystem\MountManager;
30
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
31
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
32
use Symfony\Component\Form\FormFactory;
33
use Symfony\Component\Form\FormInterface;
34
use Symfony\Component\HttpFoundation\File\UploadedFile;
35
use Symfony\Component\Routing\RouterInterface;
36
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
37
38
/**
39
 * Class ResourceRepository.
40
 */
41
class ResourceRepository extends EntityRepository
42
{
43
    /**
44
     * @var EntityRepository
45
     */
46
    protected $repository;
47
48
    /**
49
     * @var FilesystemInterface
50
     */
51
    protected $fs;
52
53
    /**
54
     * @var EntityManager
55
     */
56
    protected $entityManager;
57
58
    /**
59
     * The entity class FQN.
60
     *
61
     * @var string
62
     */
63
    protected $className;
64
65
    /** @var RouterInterface */
66
    protected $router;
67
68
    protected $resourceNodeRepository;
69
70
    /**
71
     * @var AuthorizationCheckerInterface
72
     */
73
    protected $authorizationChecker;
74
75
    /**
76
     * @var MountManager
77
     */
78
    protected $mountManager;
79
    protected $slugify;
80
81
    /**
82
     * ResourceRepository constructor.
83
     *
84
     * @param AuthorizationCheckerInterface $authorizationChecker
85
     * @param EntityManager                 $entityManager
86
     * @param MountManager                  $mountManager
87
     * @param RouterInterface               $router
88
     * @param string                        $className
89
     */
90
    public function __construct(
91
        AuthorizationCheckerInterface $authorizationChecker,
92
        EntityManager $entityManager,
93
        MountManager $mountManager,
94
        RouterInterface $router,
95
        SlugifyInterface $slugify,
96
        string $className
97
    ) {
98
        $this->repository = $entityManager->getRepository($className);
99
        // Flysystem mount name is saved in config/packages/oneup_flysystem.yaml @todo add it as a service.
100
        $this->fs = $mountManager->getFilesystem('resources_fs');
101
        $this->mountManager = $mountManager;
102
        $this->router = $router;
103
        //$this->toolChain = $toolChain;
104
        $this->resourceNodeRepository = $entityManager->getRepository('ChamiloCoreBundle:Resource\ResourceNode');
105
        $this->authorizationChecker = $authorizationChecker;
106
        $this->slugify = $slugify;
107
    }
108
109
    /**
110
     * @return AuthorizationCheckerInterface
111
     */
112
    public function getAuthorizationChecker(): AuthorizationCheckerInterface
113
    {
114
        return $this->authorizationChecker;
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function create()
121
    {
122
        return new $this->className();
123
    }
124
125
    /**
126
     * @return RouterInterface
127
     */
128
    public function getRouter(): RouterInterface
129
    {
130
        return $this->router;
131
    }
132
133
    /**
134
     * @return ResourceNodeRepository
135
     */
136
    public function getResourceNodeRepository()
137
    {
138
        return $this->resourceNodeRepository;
139
    }
140
141
    /**
142
     * @return FilesystemInterface
143
     */
144
    public function getFileSystem()
145
    {
146
        return $this->fs;
147
    }
148
149
    /**
150
     * @return EntityManager
151
     */
152
    public function getEntityManager(): EntityManager
153
    {
154
        return $this->getRepository()->getEntityManager();
155
    }
156
157
    /**
158
     * @return EntityRepository
159
     */
160
    public function getRepository()
161
    {
162
        return $this->repository;
163
    }
164
165
    /**
166
     * @param FormFactory $formFactory
167
     *
168
     * @return FormInterface
169
     */
170
    public function getForm(FormFactory $formFactory, AbstractResource $resource = null, $options = [])
171
    {
172
        $className = $this->repository->getClassName();
173
        $shortName = (new \ReflectionClass($className))->getShortName();
174
175
        $formType = str_replace('Entity\CDocument', 'Form\\Type', $className).'\\'.$shortName.'Type';
176
177
        if ($resource === null){
178
            $resource = new $className;
179
        }
180
181
        return $formFactory->create($formType, $resource, $options);
182
    }
183
184
    /**
185
     * @param mixed $id
186
     * @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...
187
     * @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...
188
     *
189
     * @return AbstractResource|null
190
     */
191
    public function find($id, $lockMode = null, $lockVersion = null): ?AbstractResource
192
    {
193
        return $this->getRepository()->find($id);
194
    }
195
196
    /**
197
     * @return AbstractResource|null
198
     */
199
    public function findOneBy(array $criteria, array $orderBy = null): ?AbstractResource
200
    {
201
        return $this->getRepository()->findOneBy($criteria, $orderBy);
202
    }
203
204
    /**
205
     * @return ResourceFile|null
206
     */
207
    public function addFile(ResourceNode $resourceNode, UploadedFile $file): ?ResourceFile
208
    {
209
        $resourceFile = $resourceNode->getResourceFile();
210
        if (null === $resourceFile) {
211
            $resourceFile = new ResourceFile();
212
        }
213
214
        $em = $this->getEntityManager();
215
216
        $resourceFile->setFile($file);
217
        $resourceFile->setName($resourceNode->getName());
218
        $em->persist($resourceFile);
219
        $resourceNode->setResourceFile($resourceFile);
220
        $em->persist($resourceNode);
221
        $em->flush();
222
223
        return $resourceFile;
224
    }
225
226
    /**
227
     * @param AbstractResource  $resource
228
     * @param User              $creator
229
     * @param ResourceNode|null $parent
230
     *
231
     * @return ResourceNode
232
     */
233
    public function addResourceNodeParent(AbstractResource $resource, User $creator, ResourceNode $parent = null): ResourceNode
234
    {
235
        $em = $this->getEntityManager();
236
237
        $resourceType = $this->getResourceType();
238
        $resourceNode = new ResourceNode();
239
        $resourceNode
240
            ->setName($resource->getResourceName())
241
            ->setCreator($creator)
242
            ->setResourceType($resourceType)
243
        ;
244
245
        if (null !== $parent) {
246
            $resourceNode->setParent($parent);
247
        }
248
249
        $resource->setResourceNode($resourceNode);
250
251
        $em->persist($resourceNode);
252
        $em->persist($resource);
253
        $em->flush();
254
255
        return $resourceNode;
256
    }
257
258
    /**
259
     * @param AbstractResource      $resource
260
     * @param User                  $creator
261
     * @param AbstractResource|null $parent
262
     *
263
     * @return ResourceNode
264
     */
265
    public function addResourceNode(AbstractResource $resource, User $creator, AbstractResource $parent = null): ResourceNode
266
    {
267
        if (null !== $parent) {
268
            $parent = $parent->getResourceNode();
269
        }
270
271
        return $this->addResourceNodeParent($resource, $creator, $parent);
272
    }
273
274
    /**
275
     * @param AbstractResource $resource
276
     * @param int              $visibility
277
     * @param User             $creator
278
     * @param Course           $course
279
     * @param Session|null     $session
280
     * @param CGroupInfo|null  $group
281
     */
282
    public function addResourceToCourse(AbstractResource $resource, int $visibility, User $creator, Course $course, Session $session = null, CGroupInfo $group = null)
283
    {
284
        $node = $this->addResourceNode($resource, $creator, $course);
285
        $this->addResourceNodeToCourse($node, $visibility, $course, $session, $group);
286
    }
287
288
    /**
289
     * @param ResourceNode $resourceNode
290
     * @param int          $visibility
291
     * @param Course       $course
292
     * @param Session      $session
293
     * @param CGroupInfo   $group
294
     */
295
    public function addResourceNodeToCourse(ResourceNode $resourceNode, $visibility, $course, $session, $group): void
296
    {
297
        $visibility = (int) $visibility;
298
        if (empty($visibility)) {
299
            $visibility = ResourceLink::VISIBILITY_PUBLISHED;
300
        }
301
302
        $link = new ResourceLink();
303
        $link
304
            ->setCourse($course)
305
            ->setSession($session)
306
            ->setGroup($group)
307
            //->setUser($toUser)
308
            ->setResourceNode($resourceNode)
309
            ->setVisibility($visibility)
310
        ;
311
312
        $rights = [];
313
        switch ($visibility) {
314
            case ResourceLink::VISIBILITY_PENDING:
315
            case ResourceLink::VISIBILITY_DRAFT:
316
                $editorMask = ResourceNodeVoter::getEditorMask();
317
                $resourceRight = new ResourceRight();
318
                $resourceRight
319
                    ->setMask($editorMask)
320
                    ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
321
                ;
322
                $rights[] = $resourceRight;
323
                break;
324
        }
325
326
        if (!empty($rights)) {
327
            foreach ($rights as $right) {
328
                $link->addResourceRight($right);
329
            }
330
        }
331
332
        $em = $this->getEntityManager();
333
        $em->persist($link);
334
        $em->flush();
335
    }
336
337
    /**
338
     * @return ResourceType
339
     */
340
    public function getResourceType()
341
    {
342
        $em = $this->getEntityManager();
343
        $entityName = $this->getRepository()->getClassName();
344
345
        return $em->getRepository('ChamiloCoreBundle:Resource\ResourceType')->findOneBy(
346
            ['entityName' => $entityName]
347
        );
348
    }
349
350
    public function addResourceToMe(ResourceNode $resourceNode): ResourceLink
351
    {
352
        $resourceLink = new ResourceLink();
353
        $resourceLink
354
            ->setResourceNode($resourceNode)
355
            ->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

355
            ->/** @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...
356
357
        $this->getEntityManager()->persist($resourceLink);
358
        $this->getEntityManager()->flush();
359
360
        return $resourceLink;
361
    }
362
363
    public function addResourceToEveryone(ResourceNode $resourceNode, ResourceRight $right): ResourceLink
364
    {
365
        $resourceLink = new ResourceLink();
366
        $resourceLink
367
            ->setResourceNode($resourceNode)
368
            ->addResourceRight($right)
369
        ;
370
371
        $this->getEntityManager()->persist($resourceLink);
372
        $this->getEntityManager()->flush();
373
374
        return $resourceLink;
375
    }
376
377
    public function addResourceToCourse2(ResourceNode $resourceNode, Course $course, ResourceRight $right): ResourceLink
378
    {
379
        $resourceLink = new ResourceLink();
380
        $resourceLink
381
            ->setResourceNode($resourceNode)
382
            ->setCourse($course)
383
            ->addResourceRight($right);
384
        $this->getEntityManager()->persist($resourceLink);
385
        $this->getEntityManager()->flush();
386
387
        return $resourceLink;
388
    }
389
390
    public function addResourceToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
391
    {
392
        $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
393
        $this->getEntityManager()->persist($resourceLink);
394
395
        return $resourceLink;
396
    }
397
398
    public function addResourceNodeToUser(ResourceNode $resourceNode, User $toUser): ResourceLink
399
    {
400
        $resourceLink = new ResourceLink();
401
        $resourceLink
402
            ->setResourceNode($resourceNode)
403
            ->setUser($toUser);
404
405
        return $resourceLink;
406
    }
407
408
    public function addResourceToSession(
409
        ResourceNode $resourceNode,
410
        Course $course,
411
        Session $session,
412
        ResourceRight $right
413
    ) {
414
        $resourceLink = $this->addResourceToCourse(
0 ignored issues
show
Bug introduced by
The call to Chamilo\CoreBundle\Repos...::addResourceToCourse() has too few arguments starting with course. ( Ignorable by Annotation )

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

414
        /** @scrutinizer ignore-call */ 
415
        $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...
415
            $resourceNode,
416
            $course,
417
            $right
418
        );
419
        $resourceLink->setSession($session);
420
        $this->getEntityManager()->persist($resourceLink);
421
422
        return $resourceLink;
423
    }
424
425
    /**
426
     * @return ResourceLink
427
     */
428
    public function addResourceToCourseGroup(
429
        ResourceNode $resourceNode,
430
        Course $course,
431
        CGroupInfo $group,
432
        ResourceRight $right
433
    ) {
434
        $resourceLink = $this->addResourceToCourse(
0 ignored issues
show
Bug introduced by
The call to Chamilo\CoreBundle\Repos...::addResourceToCourse() has too few arguments starting with course. ( Ignorable by Annotation )

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

434
        /** @scrutinizer ignore-call */ 
435
        $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...
435
            $resourceNode,
436
            $course,
437
            $right
438
        );
439
        $resourceLink->setGroup($group);
440
        $this->getEntityManager()->persist($resourceLink);
441
442
        return $resourceLink;
443
    }
444
445
    /**
446
     * @return ResourceLink
447
     */
448
    public function addResourceToGroup(
449
        ResourceNode $resourceNode,
450
        Usergroup $group,
451
        ResourceRight $right
452
    ) {
453
        $resourceLink = new ResourceLink();
454
        $resourceLink
455
            ->setResourceNode($resourceNode)
456
            ->setUserGroup($group)
457
            ->addResourceRight($right);
458
459
        return $resourceLink;
460
    }
461
462
    /**
463
     * @param array $userList User id list
464
     */
465
    public function addResourceToUserList(ResourceNode $resourceNode, array $userList)
466
    {
467
        $em = $this->getEntityManager();
468
469
        if (!empty($userList)) {
470
            foreach ($userList as $userId) {
471
                $toUser = $em->getRepository('ChamiloUserBundle:User')->find($userId);
472
473
                $resourceLink = $this->addResourceNodeToUser($resourceNode, $toUser);
474
                $em->persist($resourceLink);
475
            }
476
        }
477
    }
478
479
    /**
480
     * @param Course          $course
481
     * @param Session|null    $session
482
     * @param CGroupInfo|null $group
483
     *
484
     * @return QueryBuilder
485
     */
486
    public function getResourcesByCourse(Course $course, Session $session = null, CGroupInfo $group = null, ResourceNode $parentNode = null)
487
    {
488
        $repo = $this->getRepository();
489
        $className = $repo->getClassName();
490
        $checker = $this->getAuthorizationChecker();
491
492
        // Check if this resource type requires to load the base course resources when using a session
493
        $loadBaseSessionContent = $repo->getClassMetadata()->getReflectionClass()->hasProperty(
494
            'loadCourseResourcesInSession'
495
        );
496
        $type = $this->getResourceType();
497
498
        $qb = $repo->getEntityManager()->createQueryBuilder()
499
            ->select('resource')
500
            ->from($className, 'resource')
501
            ->innerJoin(
502
                ResourceNode::class,
503
                'node',
504
                Join::WITH,
505
                'resource.resourceNode = node.id'
506
            )
507
            ->innerJoin('node.resourceLinks', 'links')
508
            ->where('node.resourceType = :type')
509
            ->andWhere('links.course = :course')
510
            ->setParameters(
511
                [
512
                    'type' => $type,
513
                    'course' => $course,
514
                ]
515
            );
516
517
        $isAdmin = $checker->isGranted('ROLE_ADMIN') ||
518
            $checker->isGranted('ROLE_CURRENT_COURSE_TEACHER');
519
520
        if (false === $isAdmin) {
521
            $qb
522
                ->andWhere('links.visibility = :visibility')
523
                ->setParameter('visibility', ResourceLink::VISIBILITY_PUBLISHED)
524
            ;
525
            // @todo Add start/end visibility restrictrions
526
        }
527
528
        if (null === $session) {
529
            $qb->andWhere('links.session IS NULL');
530
        } else {
531
            if ($loadBaseSessionContent) {
532
                // Load course base content.
533
                $qb->andWhere('links.session = :session OR links.session IS NULL');
534
                $qb->setParameter('session', $session);
535
            } else {
536
                // Load only session resources.
537
                $qb->andWhere('links.session = :session');
538
                $qb->setParameter('session', $session);
539
            }
540
        }
541
542
        if (null !== $parentNode) {
543
            $qb->andWhere('node.parent = :parentNode');
544
            $qb->setParameter('parentNode', $parentNode);
545
        }
546
547
        if (null === $group) {
548
            $qb->andWhere('links.group IS NULL');
549
        }
550
551
        //echo ($qb->getQuery()->getSQL());exit;
552
553
        return $qb;
554
    }
555
556
    /**
557
     * @param RowAction $action
558
     * @param Row       $row
559
     * @param Session   $session
560
     *
561
     * @return RowAction|null
562
     */
563
    public function rowCanBeEdited(RowAction $action, Row $row, Session $session = null): ?RowAction
564
    {
565
        if (null !== $session) {
566
            /** @var AbstractResource $entity */
567
            $entity = $row->getEntity();
568
            $hasSession = $entity->getResourceNode()->hasSession($session);
569
            if ($hasSession->count() > 0) {
570
                return $action;
571
            }
572
573
            return null;
574
        }
575
576
        return $action;
577
    }
578
579
    /**
580
     * @param string $tool
581
     *
582
     * @return Tool|null
583
     */
584
    private function getTool($tool)
0 ignored issues
show
Unused Code introduced by
The method getTool() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
585
    {
586
        return $this
587
            ->getEntityManager()
588
            ->getRepository('ChamiloCoreBundle:Tool')
589
            ->findOneBy(['name' => $tool]);
590
    }
591
592
    /**
593
     * Deletes several entities: AbstractResource (Ex: CDocument, CQuiz), ResourceNode,
594
     * ResourceLinks and ResourceFile (including files via Flysystem).
595
     */
596
    public function hardDelete(AbstractResource $resource)
597
    {
598
        $em = $this->getEntityManager();
599
        $em->remove($resource);
600
        $em->flush();
601
    }
602
603
    /**
604
     * Change all links visibility to DELETED.
605
     */
606
    public function softDelete(AbstractResource $resource)
607
    {
608
        $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_DELETED);
609
    }
610
611
    /**
612
     * @param AbstractResource $resource
613
     */
614
    public function setVisibilityPublished(AbstractResource $resource)
615
    {
616
        $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_PUBLISHED);
617
    }
618
619
    /**
620
     * @param AbstractResource $resource
621
     */
622
    public function setVisibilityDraft(AbstractResource $resource)
623
    {
624
        $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_DRAFT);
625
    }
626
627
    /**
628
     * @param AbstractResource $resource
629
     */
630
    public function setVisibilityPending(AbstractResource $resource)
631
    {
632
        $this->setLinkVisibility($resource, ResourceLink::VISIBILITY_PENDING);
633
    }
634
635
    /**
636
     * @param AbstractResource $resource
637
     * @param                  $visibility
638
     * @param bool             $recursive
639
     *
640
     * @return bool
641
     */
642
    private function setLinkVisibility(AbstractResource $resource, $visibility, $recursive = true)
643
    {
644
        $resourceNode = $resource->getResourceNode();
645
646
        if (null === $resourceNode) {
647
            return false;
648
        }
649
650
        $em = $this->getEntityManager();
651
        if ($recursive) {
652
            $children = $resourceNode->getChildren();
653
            if (!empty($children)) {
654
                /** @var ResourceNode $child */
655
                foreach ($children as $child) {
656
                    $criteria = ['resourceNode' => $child];
657
                    $childDocument = $this->getRepository()->findOneBy($criteria);
658
                    if ($childDocument) {
659
                        $this->setLinkVisibility($childDocument, $visibility);
660
                    }
661
                }
662
            }
663
        }
664
665
        $links = $resourceNode->getResourceLinks();
666
667
        if (!empty($links)) {
668
            /** @var ResourceLink $link */
669
            foreach ($links as $link) {
670
                $link->setVisibility($visibility);
671
                if (ResourceLink::VISIBILITY_DRAFT === $visibility) {
672
                    $editorMask = ResourceNodeVoter::getEditorMask();
673
                    $rights = [];
674
                    $resourceRight = new ResourceRight();
675
                    $resourceRight
676
                        ->setMask($editorMask)
677
                        ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)
678
                        ->setResourceLink($link)
679
                    ;
680
                    $rights[] = $resourceRight;
681
682
                    if (!empty($rights)) {
683
                        $link->setResourceRight($rights);
684
                    }
685
                } else {
686
                    $link->setResourceRight([]);
687
                }
688
                $em->merge($link);
689
            }
690
        }
691
        $em->flush();
692
693
        return true;
694
    }
695
696
    /**
697
     * @param AbstractResource $resource
698
     *
699
     * @return string
700
     */
701
    public function getResourceFileContent(AbstractResource $resource): string
702
    {
703
        try {
704
            $resourceNode = $resource->getResourceNode();
705
            if ($resourceNode->hasResourceFile()) {
706
                $resourceFile = $resourceNode->getResourceFile();
707
                $fileName = $resourceFile->getFile()->getPathname();
708
709
                return $this->fs->read($fileName);
710
            }
711
712
            return '';
713
        } catch (\Throwable $exception) {
714
            throw new FileNotFoundException($id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
715
        }
716
    }
717
718
    /**
719
     * @param AbstractResource $resource
720
     * @param string $content
721
     *
722
     * @return bool
723
     */
724
    public function updateResourceFileContent(AbstractResource $resource, $content)
725
    {
726
        try {
727
            $resourceNode = $resource->getResourceNode();
728
            if ($resourceNode->hasResourceFile()) {
729
                $resourceFile = $resourceNode->getResourceFile();
730
                $fileName = $resourceFile->getFile()->getPathname();
731
732
                $this->fs->update($fileName, $content);
733
                $size = $this->fs->getSize($fileName);
734
                $resource->setSize($size);
735
                $this->entityManager->persist($resource);
736
737
                return true;
738
            }
739
740
            return false;
741
        } catch (\Throwable $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
742
        }
743
    }
744
745
}
746