| Conditions | 14 |
| Paths | 320 |
| Total Lines | 194 |
| Code Lines | 114 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 240 | public function getPersonalSessionCourses( |
||
| 241 | User $user, |
||
| 242 | AccessUrl $url, |
||
| 243 | bool $isAllowedToCreateCourse, |
||
| 244 | ?int $sessionLimit = null, |
||
| 245 | ): array { |
||
| 246 | $em = $this->getEntityManager(); |
||
| 247 | |||
| 248 | /** @var SessionRepository $sessionRepo */ |
||
| 249 | $sessionRepo = $em->getRepository(Session::class); |
||
| 250 | $sessionCourseUserRepo = $em->getRepository(SessionRelCourseRelUser::class); |
||
| 251 | |||
| 252 | $qb = $em->createQueryBuilder(); |
||
| 253 | |||
| 254 | $courseListSqlResult = $qb |
||
| 255 | ->select('c.id AS cid') |
||
| 256 | ->from(CourseRelUser::class, 'cru') |
||
| 257 | ->leftJoin('cru.course', 'c') |
||
| 258 | ->leftJoin('c.urls', 'urc') |
||
| 259 | ->where($qb->expr()->eq('cru.user', ':user')) |
||
| 260 | ->andWhere($qb->expr()->neq('cru.relationType', ':relationType')) |
||
| 261 | ->andWhere($qb->expr()->eq('urc.url', ':url')) |
||
| 262 | ->setParameters([ |
||
| 263 | 'user' => $user->getId(), |
||
| 264 | 'relationType' => COURSE_RELATION_TYPE_RRHH, |
||
| 265 | 'url' => $url->getId(), |
||
| 266 | ]) |
||
| 267 | ->getQuery() |
||
| 268 | ->getResult() |
||
| 269 | ; |
||
| 270 | |||
| 271 | $personalCourseList = $courseListSqlResult; |
||
| 272 | |||
| 273 | $sessionListFromCourseCoach = []; |
||
| 274 | // Getting sessions that are related to a coach in the session_rel_course_rel_user table |
||
| 275 | if ($isAllowedToCreateCourse) { |
||
| 276 | $sessionListFromCourseCoach = array_map( |
||
| 277 | fn (SessionRelCourseRelUser $srcru) => $srcru->getSession()->getId(), |
||
| 278 | $sessionCourseUserRepo->findBy(['user' => $user->getId(), 'status' => Session::COURSE_COACH]) |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | |||
| 282 | // Get the list of sessions where the user is subscribed |
||
| 283 | // This is divided into two different queries |
||
| 284 | /** @var array<int, Session> $sessions */ |
||
| 285 | $sessions = []; |
||
| 286 | |||
| 287 | $qb = $sessionRepo->createQueryBuilder('s'); |
||
| 288 | $qbParams = [ |
||
| 289 | 'user' => $user->getId(), |
||
| 290 | 'relationType' => Session::STUDENT, |
||
| 291 | ]; |
||
| 292 | |||
| 293 | $qb |
||
| 294 | ->innerJoin('s.users', 'su') |
||
| 295 | ->where( |
||
| 296 | $qb->expr()->andX( |
||
| 297 | $qb->expr()->eq('su.user', ':user'), |
||
| 298 | $qb->expr()->neq('su.relationType', ':relationType') |
||
| 299 | ) |
||
| 300 | ) |
||
| 301 | ; |
||
| 302 | |||
| 303 | if ($sessionListFromCourseCoach) { |
||
| 304 | $qb->orWhere($qb->expr()->in('s.id', ':sessionListFromCourseCoach')); |
||
| 305 | |||
| 306 | $qbParams['coachCourseConditions'] = $sessionListFromCourseCoach; |
||
| 307 | } |
||
| 308 | |||
| 309 | $result = $qb |
||
| 310 | ->orderBy('s.accessStartDate') |
||
| 311 | ->addOrderBy('s.accessEndDate') |
||
| 312 | ->addOrderBy('s.title') |
||
| 313 | ->setMaxResults($sessionLimit) |
||
| 314 | ->setParameters($qbParams) |
||
| 315 | ->getQuery() |
||
| 316 | ->getResult() |
||
| 317 | ; |
||
| 318 | |||
| 319 | /** @var Session $row */ |
||
| 320 | foreach ($result as $row) { |
||
| 321 | $row->setAccessVisibilityByUser($user); |
||
| 322 | |||
| 323 | $sessions[$row->getId()] = $row; |
||
| 324 | } |
||
| 325 | |||
| 326 | $qb = $sessionRepo->createQueryBuilder('s'); |
||
| 327 | $qbParams = [ |
||
| 328 | 'user' => $user->getId(), |
||
| 329 | 'relationType' => Session::GENERAL_COACH, |
||
| 330 | ]; |
||
| 331 | |||
| 332 | $qb |
||
| 333 | ->innerJoin('s.users', 'sru') |
||
| 334 | ->where( |
||
| 335 | $qb->expr()->andX( |
||
| 336 | $qb->expr()->eq('sru.user', ':user'), |
||
| 337 | $qb->expr()->neq('sru.relationType', ':relationType') |
||
| 338 | ) |
||
| 339 | ) |
||
| 340 | ; |
||
| 341 | |||
| 342 | if ($sessionListFromCourseCoach) { |
||
| 343 | $qb->orWhere($qb->expr()->in('s.id', ':sessionListFromCourseCoach')); |
||
| 344 | |||
| 345 | $qbParams['coachCourseConditions'] = $sessionListFromCourseCoach; |
||
| 346 | } |
||
| 347 | |||
| 348 | $result = $qb |
||
| 349 | ->orderBy('s.accessStartDate') |
||
| 350 | ->addOrderBy('s.accessEndDate') |
||
| 351 | ->addOrderBy('s.title') |
||
| 352 | ->setParameters($qbParams) |
||
| 353 | ->getQuery() |
||
| 354 | ->getResult() |
||
| 355 | ; |
||
| 356 | |||
| 357 | /** @var Session $row */ |
||
| 358 | foreach ($result as $row) { |
||
| 359 | $row->setAccessVisibilityByUser($user); |
||
| 360 | |||
| 361 | $sessions[$row->getId()] = $row; |
||
| 362 | } |
||
| 363 | |||
| 364 | if ($isAllowedToCreateCourse) { |
||
| 365 | foreach ($sessions as $enreg) { |
||
| 366 | if (Session::INVISIBLE == $enreg->getAccessVisibility()) { |
||
| 367 | continue; |
||
| 368 | } |
||
| 369 | |||
| 370 | $coursesAsGeneralCoach = $sessionRepo->getSessionCoursesByStatusInUserSubscription( |
||
| 371 | $user, |
||
| 372 | $enreg, |
||
| 373 | Session::GENERAL_COACH, |
||
| 374 | $url |
||
| 375 | ); |
||
| 376 | $coursesAsCourseCoach = $sessionRepo->getSessionCoursesByStatusInCourseSubscription( |
||
| 377 | $user, |
||
| 378 | $enreg, |
||
| 379 | Session::COURSE_COACH, |
||
| 380 | $url |
||
| 381 | ); |
||
| 382 | |||
| 383 | // This query is horribly slow when more than a few thousand |
||
| 384 | // users and just a few sessions to which they are subscribed |
||
| 385 | $coursesInSession = array_merge($coursesAsGeneralCoach, $coursesAsCourseCoach); |
||
| 386 | |||
| 387 | /** @var SessionRelCourse $resultRow */ |
||
| 388 | foreach ($coursesInSession as $resultRow) { |
||
| 389 | $sid = $resultRow->getSession()->getId(); |
||
| 390 | $cid = $resultRow->getCourse()->getId(); |
||
| 391 | |||
| 392 | $personalCourseList["$sid - $cid"] = [ |
||
| 393 | 'cid' => $cid, |
||
| 394 | 'sid' => $sid, |
||
| 395 | ]; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | foreach ($sessions as $enreg) { |
||
| 401 | if (Session::INVISIBLE == $enreg->getAccessVisibility()) { |
||
| 402 | continue; |
||
| 403 | } |
||
| 404 | |||
| 405 | // This query is very similar to the above query, |
||
| 406 | // but it will check the session_rel_course_user table if there are courses registered to our user or not */ |
||
| 407 | $qb = $sessionCourseUserRepo->createQueryBuilder('scu'); |
||
| 408 | |||
| 409 | $result = $qb |
||
| 410 | ->select('c.id as cid', 's.id AS sid') |
||
| 411 | ->innerJoin('scu.course', 'c', Join::WITH, 'scu.session = :session') |
||
| 412 | ->innerJoin('scu.session', 's') |
||
| 413 | ->leftJoin('scu.user', 'u') |
||
| 414 | ->where($qb->expr()->eq('scu.user', ':user')) |
||
| 415 | ->orderBy('c.title') |
||
| 416 | ->setParameters([ |
||
| 417 | 'session' => $enreg->getId(), |
||
| 418 | 'user' => $user->getId(), |
||
| 419 | ]) |
||
| 420 | ->getQuery() |
||
| 421 | ->getResult() |
||
| 422 | ; |
||
| 423 | |||
| 424 | foreach ($result as $resultRow) { |
||
| 425 | $key = $resultRow['sid'].' - '.$resultRow['cid']; |
||
| 426 | |||
| 427 | if (!isset($personalCourseList[$key])) { |
||
| 428 | $personalCourseList[$key] = $resultRow; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 | |||
| 433 | return $personalCourseList; |
||
| 434 | } |
||
| 472 |