Passed
Push — master ( 50ceb2...ce9491 )
by Angel Fernando Quiroz
13:40 queued 05:22
created

CalendarEventStateProvider::getSessionList()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 61
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 34
nc 256
nop 3
dl 0
loc 61
rs 6.5222
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
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\State;
6
7
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
8
use ApiPlatform\Metadata\CollectionOperationInterface;
9
use ApiPlatform\Metadata\Operation;
10
use ApiPlatform\State\ProviderInterface;
11
use Chamilo\CoreBundle\ApiResource\CalendarEvent;
12
use Chamilo\CoreBundle\Entity\AccessUrl;
13
use Chamilo\CoreBundle\Entity\User;
14
use Chamilo\CoreBundle\Repository\SessionRepository;
15
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
16
use Chamilo\CoreBundle\Settings\SettingsManager;
17
use Symfony\Bundle\SecurityBundle\Security;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
20
/**
21
 * @template-implements ProviderInterface<CalendarEvent[]>
22
 */
23
final class CalendarEventStateProvider implements ProviderInterface
24
{
25
    public function __construct(
26
        private readonly CollectionProvider $collectionProvider,
27
        private readonly Security $security,
28
        private readonly AccessUrlHelper $accessUrlService,
29
        private readonly SessionRepository $sessionRepository,
30
        private readonly RequestStack $requestStack,
31
        private readonly SettingsManager $settingsManager,
32
    ) {}
33
34
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable
35
    {
36
        /** @var User|null $user */
37
        $user = $this->security->getUser();
38
39
        $accessUrl = $this->accessUrlService->getCurrent();
40
41
        if ($operation instanceof CollectionOperationInterface) {
42
            $cCalendarEvents = $this->collectionProvider->provide($operation, $uriVariables, $context);
43
            $userSessions = [];
44
45
            $request = $this->requestStack->getMainRequest();
46
            $courseId = $request->query->getInt('cid');
47
            $sessionId = $request->query->getInt('sid');
48
49
            $inCourseBase = !empty($courseId);
50
            $inSession = !empty($sessionId);
51
            $inCourseSession = $inCourseBase && $inSession;
52
53
            $inPersonalAgenda = !$inCourseBase && !$inCourseSession;
54
55
            if ($inPersonalAgenda
56
                && 'true' === $this->settingsManager->getSetting('agenda.personal_calendar_show_sessions_occupation')
57
            ) {
58
                $userSessions = $this->getSessionList($user, $accessUrl, $context);
59
            }
60
61
            return array_merge($cCalendarEvents, $userSessions);
62
        }
63
64
        return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return iterable.
Loading history...
65
    }
66
67
    private function getSessionList(User $user, AccessUrl $accessUrl, array $context = []): array
68
    {
69
        $qb = $this->sessionRepository->getUserFollowedSessionsInAccessUrl($user, $accessUrl);
70
71
        if (!empty($context['filters']['startDate']['before'])) {
72
            $qb
73
                ->andWhere($qb->expr()->lte('s.displayStartDate', ':value_start'))
74
                ->setParameter('value_start', $context['filters']['startDate']['before'])
75
            ;
76
        }
77
78
        if (!empty($context['filters']['startDate']['after'])) {
79
            $qb
80
                ->andWhere($qb->expr()->gte('s.displayStartDate', ':value_start'))
81
                ->setParameter('value_start', $context['filters']['startDate']['after'])
82
            ;
83
        }
84
85
        if (!empty($context['filters']['startDate']['strictly_before'])) {
86
            $qb
87
                ->andWhere($qb->expr()->lt('s.displayStartDate', ':value_start'))
88
                ->setParameter('value_start', $context['filters']['startDate']['strictly_before'])
89
            ;
90
        }
91
92
        if (!empty($context['filters']['startDate']['strictly_after'])) {
93
            $qb
94
                ->andWhere($qb->expr()->gt('s.displayStartDate', ':value_start'))
95
                ->setParameter('value_start', $context['filters']['startDate']['strictly_after'])
96
            ;
97
        }
98
99
        if (!empty($context['filters']['endDate']['before'])) {
100
            $qb
101
                ->andWhere($qb->expr()->lte('s.displayEndDate', ':value_end'))
102
                ->setParameter('value_end', $context['filters']['endDate']['before'])
103
            ;
104
        }
105
106
        if (!empty($context['filters']['endDate']['after'])) {
107
            $qb
108
                ->andWhere($qb->expr()->gte('s.displayEndDate', ':value_end'))
109
                ->setParameter('value_end', $context['filters']['endDate']['after'])
110
            ;
111
        }
112
113
        if (!empty($context['filters']['endDate']['strictly_before'])) {
114
            $qb
115
                ->andWhere($qb->expr()->lt('s.displayEndDate', ':value_end'))
116
                ->setParameter('value_end', $context['filters']['endDate']['strictly_before'])
117
            ;
118
        }
119
120
        if (!empty($context['filters']['endDate']['strictly_after'])) {
121
            $qb
122
                ->andWhere($qb->expr()->gt('s.displayEndDate', ':value_end'))
123
                ->setParameter('value_end', $context['filters']['endDate']['strictly_after'])
124
            ;
125
        }
126
127
        return $qb->getQuery()->getResult();
128
    }
129
}
130