Passed
Push — master ( 373e44...f7c0d8 )
by Angel Fernando Quiroz
06:55
created

TrackEAttemptQualifyExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A applyToCollection() 0 32 5
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\DataProvider\Extension;
8
9
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
10
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
11
use ApiPlatform\Metadata\Operation;
12
use Chamilo\CoreBundle\Entity\TrackEAttemptQualify;
13
use Chamilo\CoreBundle\Entity\User;
14
use Chamilo\CoreBundle\ServiceHelper\IsAllowedToEditHelper;
15
use Doctrine\ORM\QueryBuilder;
16
use Symfony\Bundle\SecurityBundle\Security;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
19
readonly class TrackEAttemptQualifyExtension implements QueryCollectionExtensionInterface
20
{
21
    public function __construct(
22
        private Security $security,
23
        private IsAllowedToEditHelper $isAllowedToEditHelper,
24
    ) {}
25
26
    public function applyToCollection(
27
        QueryBuilder $queryBuilder,
28
        QueryNameGeneratorInterface $queryNameGenerator,
29
        string $resourceClass,
30
        Operation $operation = null,
31
        array $context = []
32
    ): void {
33
        if (TrackEAttemptQualify::class !== $resourceClass) {
34
            return;
35
        }
36
37
        if ($this->security->isGranted('ROLE_ADMIN')) {
38
            return;
39
        }
40
41
        $user = $this->security->getUser();
42
43
        if (!$user instanceof UserInterface) {
44
            return;
45
        }
46
47
        assert($user instanceof User);
48
49
        $alias = $queryBuilder->getRootAliases()[0];
50
51
        // @todo Check permissions with other roles of the current user
52
53
        if ($user->isStudent()) {
54
            $queryBuilder
55
                ->innerJoin("$alias.trackExercise", 'tee')
56
                ->andWhere('tee.user = :user')
57
                ->setParameter('user', $user->getId())
58
            ;
59
        }
60
    }
61
}