| Conditions | 5 |
| Paths | 12 |
| Total Lines | 61 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 19 | public function getResources(User $user, ResourceNode $parentNode, Course $course = null, Session $session = null, CGroupInfo $group = null): QueryBuilder |
||
| 20 | { |
||
| 21 | $repo = $this->getRepository(); |
||
| 22 | $className = $repo->getClassName(); |
||
| 23 | $checker = $this->getAuthorizationChecker(); |
||
| 24 | |||
| 25 | $reflectionClass = $repo->getClassMetadata()->getReflectionClass(); |
||
| 26 | |||
| 27 | // Check if this resource type requires to load the base course resources when using a session |
||
| 28 | $loadBaseSessionContent = $reflectionClass->hasProperty('loadCourseResourcesInSession'); |
||
| 29 | |||
| 30 | $type = $this->getResourceType(); |
||
| 31 | |||
| 32 | $qb = $repo->getEntityManager()->createQueryBuilder() |
||
| 33 | ->select('resource') |
||
| 34 | ->from($className, 'resource') |
||
| 35 | ->innerJoin( |
||
| 36 | ResourceNode::class, |
||
| 37 | 'node', |
||
| 38 | Join::WITH, |
||
| 39 | 'resource.resourceNode = node.id' |
||
| 40 | ) |
||
| 41 | ->innerJoin('node.resourceLinks', 'links') |
||
| 42 | ->where('node.resourceType = :type') |
||
| 43 | ->setParameter('type', $type); |
||
| 44 | $qb |
||
| 45 | ->andWhere('links.course = :course') |
||
| 46 | ->setParameter('course', $course) |
||
| 47 | ; |
||
| 48 | |||
| 49 | $isAdmin = $checker->isGranted('ROLE_ADMIN') || $checker->isGranted('ROLE_CURRENT_COURSE_TEACHER'); |
||
| 50 | |||
| 51 | if (false === $isAdmin) { |
||
| 52 | $qb |
||
| 53 | ->andWhere('links.visibility = :visibility') |
||
| 54 | ->setParameter('visibility', ResourceLink::VISIBILITY_PUBLISHED) |
||
| 55 | ; |
||
| 56 | } |
||
| 57 | |||
| 58 | if (null === $session) { |
||
| 59 | $qb->andWhere('links.session IS NULL'); |
||
| 60 | } else { |
||
| 61 | if ($loadBaseSessionContent) { |
||
| 62 | // Load course base content. |
||
| 63 | $qb->andWhere('links.session = :session OR links.session IS NULL'); |
||
| 64 | $qb->setParameter('session', $session); |
||
| 65 | } else { |
||
| 66 | // Load only session resources. |
||
| 67 | $qb->andWhere('links.session = :session'); |
||
| 68 | $qb->setParameter('session', $session); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | $qb->andWhere('node.parent = :parentNode'); |
||
| 73 | $qb->setParameter('parentNode', $parentNode); |
||
| 74 | |||
| 75 | $qb->andWhere('links.group IS NULL'); |
||
| 76 | |||
| 77 | ///var_dump($qb->getQuery()->getSQL(), $type->getId(), $parentNode->getId());exit; |
||
| 78 | |||
| 79 | return $qb; |
||
| 80 | } |
||
| 82 |