Passed
Push — master ( 07f112...fdb78e )
by Angel Fernando Quiroz
07:56 queued 24s
created

SessionRelUserExtension::addWhere()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 63
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 40
c 1
b 0
f 0
nc 13
nop 2
dl 0
loc 63
rs 8.3466

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\SessionRelUser;
13
use Chamilo\CoreBundle\Entity\User;
14
use DateTime;
15
use DateTimeZone;
16
use Doctrine\ORM\QueryBuilder;
17
use Symfony\Bundle\SecurityBundle\Security;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
20
21
// use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
22
23
final class SessionRelUserExtension implements QueryCollectionExtensionInterface // , QueryItemExtensionInterface
24
{
25
    public function __construct(
26
        private readonly Security $security,
27
        private readonly RequestStack $requestStack
28
    ) {}
29
30
    public function applyToCollection(
31
        QueryBuilder $queryBuilder,
32
        QueryNameGeneratorInterface $queryNameGenerator,
33
        string $resourceClass,
34
        ?Operation $operation = null,
35
        array $context = []
36
    ): void {
37
        $this->addWhere($queryBuilder, $resourceClass);
38
    }
39
40
    /*public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
41
    {
42
        error_log('applyToItem');
43
        $this->addWhere($queryBuilder, $resourceClass);
44
    }*/
45
46
    private function addWhere(QueryBuilder $qb, string $resourceClass): void
47
    {
48
        if (SessionRelUser::class !== $resourceClass) {
49
            return;
50
        }
51
52
        $request = $this->requestStack->getCurrentRequest();
53
        $alias = $qb->getRootAliases()[0];
54
        $content = $request->getContent();
55
56
        $now = new DateTime('now', new DateTimeZone('UTC'));
57
        $date = $now->format('Y-m-d H:i:s');
58
59
        $qb->innerJoin("$alias.session", 's');
60
61
        if (str_contains($content, 'getCurrentSessions')) {
62
            $qb->andWhere(
63
                $qb->expr()->orX(
64
                    $qb->expr()->andX(
65
                        $qb->expr()->isNotNull('s.accessStartDate'),
66
                        $qb->expr()->isNull('s.accessEndDate'),
67
                        $qb->expr()->lte('s.accessStartDate', "'$date'")
68
                    ),
69
                    $qb->expr()->andX(
70
                        $qb->expr()->isNotNull('s.accessStartDate'),
71
                        $qb->expr()->isNotNull('s.accessEndDate'),
72
                        $qb->expr()->lte('s.accessStartDate', "'$date'"),
73
                        $qb->expr()->gte('s.accessEndDate', "'$date'")
74
                    ),
75
                    $qb->expr()->andX(
76
                        $qb->expr()->isNull('s.accessStartDate'),
77
                        $qb->expr()->isNotNull('s.accessEndDate'),
78
                        $qb->expr()->gte('s.accessEndDate', "'$date'")
79
                    )
80
                )
81
            );
82
        } elseif (str_contains($content, 'getUpcommingSessions')) {
83
            $qb->andWhere(
84
                $qb->expr()->andX(
85
                    $qb->expr()->isNotNull('s.accessStartDate'),
86
                    $qb->expr()->gt('s.accessStartDate', "'$date'")
87
                )
88
            );
89
        } elseif (str_contains($content, 'getPastSessions')) {
90
            $qb->andWhere(
91
                $qb->expr()->andX(
92
                    $qb->expr()->isNotNull('s.accessEndDate'),
93
                    $qb->expr()->lt('s.accessEndDate', "'$date'")
94
                )
95
            );
96
        }
97
98
        if ($this->security->isGranted('ROLE_ADMIN')) {
99
            return;
100
        }
101
102
        /** @var User|null $user */
103
        if (null === $user = $this->security->getUser()) {
104
            throw new AccessDeniedException('Access Denied SessionRelUser');
105
        }
106
107
        $qb->andWhere(sprintf('%s.user = :current_user', $alias));
108
        $qb->setParameter('current_user', $user->getId());
109
    }
110
}
111