Passed
Push — master ( 6397a8...6ccf45 )
by Julito
11:04
created

CCalendarEventExtension::addWhere()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 72
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 40
nc 6
nop 2
dl 0
loc 72
rs 8.9688
c 0
b 0
f 0

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\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
10
//use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
11
//use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
12
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
13
use Chamilo\CoreBundle\Entity\Message;
14
use Chamilo\CoreBundle\Entity\User;
15
use Chamilo\CourseBundle\Entity\CCalendarEvent;
16
use Doctrine\ORM\Query\Expr\Join;
17
use Doctrine\ORM\QueryBuilder;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
20
use Symfony\Component\Security\Core\Security;
21
22
final class CCalendarEventExtension implements QueryCollectionExtensionInterface //, QueryItemExtensionInterface
23
{
24
    private Security $security;
25
    private RequestStack $requestStack;
26
27
    public function __construct(Security $security, RequestStack $request)
28
    {
29
        $this->security = $security;
30
        $this->requestStack = $request;
31
    }
32
33
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void
34
    {
35
        /*if ($this->security->isGranted('ROLE_ADMIN')) {
36
            return;
37
        }*/
38
        /*
39
        if ('collection_query' === $operationName) {
40
            if (null === $user = $this->security->getUser()) {
41
                throw new AccessDeniedException('Access Denied.');
42
            }
43
44
            $rootAlias = $queryBuilder->getRootAliases()[0];
45
            $queryBuilder->andWhere(sprintf('%s.user = :current_user', $rootAlias));
46
            $queryBuilder->setParameter('current_user', $user);
47
        }*/
48
49
        $this->addWhere($queryBuilder, $resourceClass);
50
    }
51
52
    public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
53
    {
54
        //$this->addWhere($queryBuilder, $resourceClass);
55
    }
56
57
    private function addWhere(QueryBuilder $qb, string $resourceClass): void
58
    {
59
        if (CCalendarEvent::class !== $resourceClass) {
60
            return;
61
        }
62
63
        /*if ($this->security->isGranted('ROLE_ADMIN')) {
64
            return;
65
        }*/
66
67
        /** @var User $user */
68
        $user = $this->security->getUser();
69
        $alias = $qb->getRootAliases()[0];
70
71
        $qb
72
            ->innerJoin("$alias.resourceNode", 'node')
73
            ->leftJoin('node.resourceLinks', 'links')
74
        ;
75
76
        $request = $this->requestStack->getCurrentRequest();
77
        $courseId = $request->query->get('cid');
78
        $sessionId = $request->query->get('sid');
79
        $groupId = $request->query->get('gid');
80
81
        $startDate = $request->query->get('startDate');
82
        $endDate = $request->query->get('endDate');
83
84
        $qb->andWhere(
85
            "
86
                $alias.startDate BETWEEN :start AND :end OR
87
                $alias.endDate BETWEEN :start AND :end 
88
            "
89
        );
90
91
        /*OR
92
        (
93
            $alias.startDate IS NOT NULL AND $alias.endDate IS NOT NULL AND
94
    YEAR($alias.startDate) = YEAR($alias.endDate) AND
95
    MONTH(':start') BETWEEN MONTH($alias.startDate) AND MONTH($alias.endDate)
96
                 )*/
97
98
        $qb
99
            ->setParameter('start', $startDate)
100
            ->setParameter('end', $endDate)
101
        ;
102
103
        if (empty($courseId)) {
104
            $qb
105
                ->andWhere('links.user = :user OR node.creator = :user')
106
                ->setParameter('user', $user)
107
            ;
108
        } else {
109
            $qb
110
                ->andWhere('links.course = :course')
111
                ->setParameter('course', $courseId)
112
            ;
113
114
            if (empty($sessionId)) {
115
                $qb->andWhere('links.session IS NULL');
116
            } else {
117
                $qb
118
                    ->andWhere('links.session = :session')
119
                    ->setParameter('session', $sessionId)
120
                ;
121
            }
122
123
            if (empty($groupId)) {
124
                $qb->andWhere('links.group IS NULL');
125
            } else {
126
                $qb
127
                    ->andWhere('links.group = :group')
128
                    ->setParameter('group', $groupId)
129
                ;
130
            }
131
        }
132
133
        //$qb->leftJoin("$alias.receivers", 'r');
134
        //$qb->leftJoin("$alias.receivers", 'r', Join::WITH, "r.receiver = :current OR $alias.sender = :current ");
135
        //$qb->leftJoin("$alias.receivers", 'r');
136
        /*$qb->andWhere(
137
            $qb->expr()->orX(
138
                $qb->andWhere(
139
                    $qb->expr()->eq("$alias.sender", $user->getId()),
140
                    $qb->expr()->eq("$alias.msgType", Message::MESSAGE_TYPE_OUTBOX)
141
                ),
142
                $qb->andWhere(
143
                    $qb->expr()->in("r", $user->getId()),
144
                    $qb->expr()->eq("$alias.msgType", Message::MESSAGE_TYPE_INBOX)
145
                )
146
            ),
147
        );*/
148
    }
149
150
    /*public function generateBetweenRange($qb, $alias, $field, $range)
151
    {
152
        $value = $range['between'];
153
        $rangeValue = explode('..', $value);
154
        $valueParameter = $field.'1';
155
        $qb
156
            ->andWhere(sprintf('%1$s.%2$s BETWEEN :%3$s_1 AND :%3$s_2', $alias, $field, $valueParameter))
157
            ->setParameter(sprintf('%s_1', $valueParameter), $rangeValue[0])
158
            ->setParameter(sprintf('%s_2', $valueParameter), $rangeValue[1]);
159
160
        return $qb;
161
    }*/
162
}
163