| Conditions | 5 |
| Paths | 1 |
| Total Lines | 62 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 106 | public function getToolUsageReportByTools(array $toolIds): array |
||
| 107 | { |
||
| 108 | $queryBuilder = $this->_em->createQueryBuilder(); |
||
| 109 | |||
| 110 | $queryBuilder |
||
| 111 | ->select( |
||
| 112 | 'COUNT(rl.id) AS resource_count', |
||
| 113 | 'IDENTITY(rl.course) AS course_id', |
||
| 114 | 'IDENTITY(rl.session) AS session_id', |
||
| 115 | 'IDENTITY(c.resourceNode) AS course_resource_node_id', |
||
| 116 | 't.title AS tool_name', |
||
| 117 | 'c.title AS course_name', |
||
| 118 | 's.title AS session_name', |
||
| 119 | 'MAX(rl.updatedAt) AS last_updated' |
||
| 120 | ) |
||
| 121 | ->from('ChamiloCoreBundle:ResourceLink', 'rl') |
||
| 122 | ->innerJoin('ChamiloCoreBundle:ResourceType', 'rt', 'WITH', 'rt.id = rl.resourceTypeGroup') |
||
| 123 | ->innerJoin('ChamiloCoreBundle:Tool', 't', 'WITH', 't.id = rt.tool') |
||
| 124 | ->innerJoin('ChamiloCoreBundle:Course', 'c', 'WITH', 'c.id = rl.course') |
||
| 125 | ->leftJoin('ChamiloCoreBundle:Session', 's', 'WITH', 's.id = rl.session') |
||
| 126 | ->where($queryBuilder->expr()->in('t.id', ':toolIds')) |
||
| 127 | ->groupBy('rl.course, rl.session, t.title') |
||
| 128 | ->orderBy('t.title', 'ASC') |
||
| 129 | ->addOrderBy('c.title', 'ASC') |
||
| 130 | ->addOrderBy('s.title', 'ASC') |
||
| 131 | ->setParameter('toolIds', $toolIds); |
||
| 132 | |||
| 133 | $result = $queryBuilder->getQuery()->getArrayResult(); |
||
| 134 | |||
| 135 | return array_map(function ($row) { |
||
| 136 | $toolName = $row['tool_name']; |
||
| 137 | $baseLink = $this->toolList[$toolName] ?? null; |
||
| 138 | $link = '-'; |
||
| 139 | if ($baseLink) { |
||
| 140 | $link = str_replace( |
||
| 141 | ['%resource_node_id%'], |
||
| 142 | [$row['course_resource_node_id']], |
||
| 143 | $baseLink |
||
| 144 | ); |
||
| 145 | |||
| 146 | $queryParams = [ |
||
| 147 | 'cid' => $row['course_id'], |
||
| 148 | ]; |
||
| 149 | |||
| 150 | if (!empty($row['session_id'])) { |
||
| 151 | $queryParams['sid'] = $row['session_id']; |
||
| 152 | } |
||
| 153 | |||
| 154 | $link .= '?' . http_build_query($queryParams); |
||
| 155 | } |
||
| 156 | |||
| 157 | return [ |
||
| 158 | 'tool_name' => $toolName, |
||
| 159 | 'session_id' => $row['session_id'], |
||
| 160 | 'session_name' => $row['session_name'] ?: '-', |
||
| 161 | 'course_id' => $row['course_id'], |
||
| 162 | 'course_name' => $row['course_name'], |
||
| 163 | 'resource_count' => (int) $row['resource_count'], |
||
| 164 | 'last_updated' => $row['last_updated'] ?: '-', |
||
| 165 | 'link' => $link, |
||
| 166 | ]; |
||
| 167 | }, $result); |
||
| 168 | } |
||
| 170 |