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