Passed
Push — master ( 3a1d7a...a972a6 )
by Julito
08:34
created

SessionRepository::addUserInCourse()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 31
c 1
b 0
f 0
nc 10
nop 4
dl 0
loc 50
rs 7.6666

How to fix   Complexity   

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
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\AccessUrl;
10
use Chamilo\CoreBundle\Entity\AccessUrlRelUser;
11
use Chamilo\CoreBundle\Entity\Course;
12
use Chamilo\CoreBundle\Entity\Session;
13
use Chamilo\CoreBundle\Entity\SessionRelCourse;
14
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
15
use Chamilo\CoreBundle\Entity\SessionRelUser;
16
use Chamilo\CoreBundle\Entity\User;
17
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
18
use Doctrine\ORM\Query\Expr\Join;
19
use Doctrine\ORM\QueryBuilder;
20
use Doctrine\Persistence\ManagerRegistry;
21
use Exception;
22
23
/**
24
 * @author Julio Montoya <[email protected]>
25
 */
26
class SessionRepository extends ServiceEntityRepository
27
{
28
    public function __construct(ManagerRegistry $registry)
29
    {
30
        parent::__construct($registry, Session::class);
31
    }
32
33
    public function create(): ?Session
34
    {
35
        return new Session();
36
    }
37
38
    public function update(Session $session): void
39
    {
40
        $this->getEntityManager()->persist($session);
41
        $this->getEntityManager()->flush();
42
    }
43
44
    /**
45
     * @return array<SessionRelUser>
46
     */
47
    public function getUsersByAccessUrl(Session $session, AccessUrl $url, array $relationTypeList = []): array
48
    {
49
        if (0 === $session->getUsers()->count()) {
50
            return [];
51
        }
52
53
        $qb = $this->addSessionRelUserFilterByUrl($session, $url);
54
        $qb->orderBy('sru.relationType');
55
56
        if ($relationTypeList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationTypeList of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57
            $qb->andWhere(
58
                $qb->expr()->in('sru.relationType', $relationTypeList)
59
            );
60
        }
61
62
        return $qb->getQuery()->getResult();
63
    }
64
65
    /**
66
     * @return Session[]
67
     */
68
    public function getSessionsByUser(User $user, AccessUrl $url)
69
    {
70
        $qb = $this->createQueryBuilder('s');
71
        $qb
72
            ->select('s')
73
            ->innerJoin('s.users', 'sru')
74
            ->leftJoin(AccessUrlRelUser::class, 'uru', Join::WITH, 'uru.user = sru.user')
75
            ->andWhere('sru.user = :user AND uru.url = :url')
76
            ->setParameters([
77
                'user' => $user,
78
                'url' => $url,
79
            ])
80
        ;
81
82
        return $qb->getQuery()->getResult();
83
    }
84
85
    public function addUserInCourse(int $status, User $user, Course $course, Session $session): void
86
    {
87
        if (!$session->isActive()) {
88
            throw new Exception('Session not active');
89
        }
90
91
        if (!$user->isActive()) {
92
            throw new Exception('User not active');
93
        }
94
95
        if (!$course->isActive()) {
96
            throw new Exception('Course not active');
97
        }
98
99
        if (!$session->hasCourse($course)) {
100
            $msg = sprintf('Course %s is not subscribed to the session %s', $course->getTitle(), $session->getName());
101
102
            throw new Exception($msg);
103
        }
104
105
        switch ($status) {
106
            case Session::DRH:
107
                if ($user->hasRole('ROLE_RRHH')) {
108
                    $session->addUserInSession(Session::DRH, $user);
109
                }
110
111
                break;
112
            case Session::STUDENT:
113
                $session
114
                    ->addUserInSession(Session::STUDENT, $user)
115
                    ->addUserInCourse(
116
                        Session::STUDENT,
117
                        $user,
118
                        $course
119
                    )
120
                ;
121
122
                break;
123
            case Session::COURSE_COACH:
124
                if ($user->hasRole('ROLE_TEACHER')) {
125
                    $session->addUserInCourse(
126
                        Session::COURSE_COACH,
127
                        $user,
128
                        $course
129
                    );
130
                }
131
132
                break;
133
            default:
134
                throw new Exception(sprintf('Cannot handle status %s', $status));
135
        }
136
    }
137
138
    /**
139
     * @return array<SessionRelCourse>
140
     */
141
    public function getSessionCoursesByStatusInUserSubscription(User $user, Session $session, int $relationType, AccessUrl $url = null): array
142
    {
143
        $qb = $this->getEntityManager()->createQueryBuilder();
144
145
        $qb->select('src')
146
            ->from(SessionRelCourse::class, 'src')
147
            ->innerJoin(
148
                SessionRelUser::class,
149
                'sru',
150
                Join::WITH,
151
                'src.session = sru.session'
152
            )
153
            ->innerJoin('src.session', 'session')
154
            ->where(
155
                $qb->expr()->eq('session', ':session')
156
            )
157
            ->andWhere(
158
                $qb->expr()->eq('sru.user', ':user')
159
            )
160
            ->andWhere(
161
                $qb->expr()->eq('sru.relationType', ':relation_type')
162
            )
163
        ;
164
165
        $parameters = [
166
            'session' => $session,
167
            'user' => $user,
168
            'relation_type' => $relationType,
169
        ];
170
171
        if ($url) {
172
            $qb->innerJoin('session.urls', 'urls')
173
                ->andWhere(
174
                    $qb->expr()->eq('urls.url', ':url')
175
                )
176
            ;
177
178
            $parameters['url'] = $url;
179
        }
180
181
        $qb->setParameters($parameters);
182
183
        return $qb->getQuery()->getResult();
184
    }
185
186
    /**
187
     * @return array<SessionRelCourse>
188
     */
189
    public function getSessionCoursesByStatusInCourseSuscription(User $user, Session $session, int $status, AccessUrl $url = null): array
190
    {
191
        $qb = $this->getEntityManager()->createQueryBuilder();
192
193
        $qb->select('src')
194
            ->from(SessionRelCourse::class, 'src')
195
            ->innerJoin(
196
                SessionRelCourseRelUser::class,
197
                'srcru',
198
                Join::WITH,
199
                'src.session = srcru.session AND src.course = srcru.course'
200
            )
201
            ->innerJoin('srcru.session', 'session')
202
            ->where(
203
                $qb->expr()->eq('session', ':session')
204
            )
205
            ->andWhere(
206
                $qb->expr()->eq('srcru.user', ':user')
207
            )
208
            ->andWhere(
209
                $qb->expr()->eq('srcru.status', ':status')
210
            )
211
        ;
212
213
        $parameters = [
214
            'session' => $session,
215
            'user' => $user,
216
            'status' => $status,
217
        ];
218
219
        if ($url) {
220
            $qb->innerJoin('session.urls', 'urls')
221
                ->andWhere(
222
                    $qb->expr()->eq('urls.url', ':url')
223
                )
224
            ;
225
226
            $parameters['url'] = $url;
227
        }
228
229
        $qb->setParameters($parameters);
230
231
        return $qb->getQuery()->getResult();
232
    }
233
234
    private function addSessionRelUserFilterByUrl(Session $session, AccessUrl $url): QueryBuilder
235
    {
236
        $qb = $this->getEntityManager()->createQueryBuilder();
237
        $qb
238
            ->select('sru')
239
            ->from(User::class, 'u')
240
            ->innerJoin(SessionRelUser::class, 'sru', Join::WITH, 'sru.user = u.id')
241
            ->innerJoin(AccessUrlRelUser::class, 'uru', Join::WITH, 'uru.user = u.id')
242
            ->andWhere('sru.session = :session AND uru.url = :url')
243
            ->setParameters([
244
                'session' => $session,
245
                'url' => $url,
246
            ])
247
        ;
248
249
        return $qb;
250
    }
251
}
252