| Total Complexity | 62 |
| Total Lines | 439 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SequenceResourceRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SequenceResourceRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class SequenceResourceRepository extends EntityRepository |
||
| 24 | { |
||
| 25 | public const VERTICES_TYPE_REQ = 'requirements'; |
||
| 26 | public const VERTICES_TYPE_DEP = 'dependents'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Find the SequenceResource based in the resourceId and type. |
||
| 30 | * |
||
| 31 | * @param int $resourceId |
||
| 32 | * @param int $type |
||
| 33 | */ |
||
| 34 | public function findRequirementForResource($resourceId, $type): ?SequenceResource |
||
| 35 | { |
||
| 36 | return $this->findOneBy(['resourceId' => $resourceId, 'type' => $type]); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @todo implement for all types only work for sessions |
||
| 41 | * |
||
| 42 | * @param int $resourceId |
||
| 43 | * @param int $type |
||
| 44 | */ |
||
| 45 | public function getRequirementAndDependencies($resourceId, $type): array |
||
| 46 | { |
||
| 47 | $sequence = $this->findRequirementForResource($resourceId, $type); |
||
| 48 | $result = ['requirements' => [], 'dependencies' => []]; |
||
| 49 | if ($sequence && $sequence->hasGraph()) { |
||
| 50 | $graph = $sequence->getSequence()->getUnSerializeGraph(); |
||
| 51 | $vertex = $graph->getVertex($resourceId); |
||
| 52 | $from = $vertex->getVerticesEdgeFrom(); |
||
| 53 | |||
| 54 | foreach ($from as $subVertex) { |
||
| 55 | $vertexId = $subVertex->getId(); |
||
| 56 | $sessionInfo = api_get_session_info($vertexId); |
||
| 57 | $sessionInfo['admin_link'] = '<a href="'.SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>'; |
||
|
|
|||
| 58 | $result['requirements'][] = $sessionInfo; |
||
| 59 | } |
||
| 60 | |||
| 61 | $to = $vertex->getVerticesEdgeTo(); |
||
| 62 | foreach ($to as $subVertex) { |
||
| 63 | $vertexId = $subVertex->getId(); |
||
| 64 | $sessionInfo = api_get_session_info($vertexId); |
||
| 65 | $sessionInfo['admin_link'] = '<a href="'.SessionManager::getAdminPath($vertexId).'">'.$sessionInfo['name'].'</a>'; |
||
| 66 | $result['dependencies'][] = $sessionInfo; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return $result; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Deletes a node and check in all the dependencies if the node exists in |
||
| 75 | * order to delete. |
||
| 76 | * |
||
| 77 | * @param int $resourceId |
||
| 78 | * @param int $type |
||
| 79 | * |
||
| 80 | * @throws ORMException |
||
| 81 | * @throws OptimisticLockException |
||
| 82 | */ |
||
| 83 | public function deleteResource($resourceId, $type) |
||
| 84 | { |
||
| 85 | $sequence = $this->findRequirementForResource($resourceId, $type); |
||
| 86 | if ($sequence && $sequence->hasGraph()) { |
||
| 87 | $em = $this->getEntityManager(); |
||
| 88 | $graph = $sequence->getSequence()->getUnSerializeGraph(); |
||
| 89 | $mainVertex = $graph->getVertex($resourceId); |
||
| 90 | $vertices = $graph->getVertices(); |
||
| 91 | |||
| 92 | /** @var Vertex $vertex */ |
||
| 93 | foreach ($vertices as $vertex) { |
||
| 94 | $subResourceId = $vertex->getId(); |
||
| 95 | $subSequence = $this->findRequirementForResource($subResourceId, $type); |
||
| 96 | if ($sequence && $subSequence->hasGraph()) { |
||
| 97 | $graph = $subSequence->getSequence()->getUnSerializeGraph(); |
||
| 98 | $subMainVertex = $graph->getVertex($resourceId); |
||
| 99 | $subMainVertex->destroy(); |
||
| 100 | $subSequence->getSequence()->setGraphAndSerialize($graph); |
||
| 101 | $em->persist($subSequence); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | $mainVertex->destroy(); |
||
| 106 | $em->remove($sequence); |
||
| 107 | $em->flush(); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the requirements for a resource only. |
||
| 113 | * |
||
| 114 | * @param int $resourceId The resource ID |
||
| 115 | * @param int $type The type of sequence resource |
||
| 116 | */ |
||
| 117 | public function getRequirements($resourceId, $type): array |
||
| 118 | { |
||
| 119 | return $this->getRequirementsOrDependents($resourceId, $type, self::VERTICES_TYPE_REQ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get the requirements for a resource only. |
||
| 124 | */ |
||
| 125 | public function getDependents(int $resourceId, int $type): array |
||
| 126 | { |
||
| 127 | return $this->getRequirementsOrDependents($resourceId, $type, self::VERTICES_TYPE_DEP); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the requirements and dependencies within a sequence for a resource. |
||
| 132 | */ |
||
| 133 | public function getRequirementsAndDependenciesWithinSequences(int $resourceId, int $type): array |
||
| 134 | { |
||
| 135 | $sequencesResource = $this->findBy([ |
||
| 136 | 'resourceId' => $resourceId, |
||
| 137 | 'type' => $type, |
||
| 138 | ]); |
||
| 139 | |||
| 140 | $result = []; |
||
| 141 | |||
| 142 | /** @var SequenceResource $sequenceResource */ |
||
| 143 | foreach ($sequencesResource as $sequenceResource) { |
||
| 144 | if (!$sequenceResource->hasGraph()) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | $sequence = $sequenceResource->getSequence(); |
||
| 149 | $graph = $sequence->getUnSerializeGraph(); |
||
| 150 | $vertex = $graph->getVertex($resourceId); |
||
| 151 | $from = $vertex->getVerticesEdgeFrom(); |
||
| 152 | $to = $vertex->getVerticesEdgeTo(); |
||
| 153 | |||
| 154 | $requirements = $this->findVerticesEdges($from, $type); |
||
| 155 | $dependencies = $this->findVerticesEdges($to, $type); |
||
| 156 | |||
| 157 | $result[$sequence->getId()] = [ |
||
| 158 | 'name' => $sequence->getName(), |
||
| 159 | 'requirements' => $requirements, |
||
| 160 | 'dependencies' => $dependencies, |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | |||
| 164 | return $result; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Check if the ser has completed the requirements for the sequences. |
||
| 169 | * |
||
| 170 | * @param array $sequences The sequences |
||
| 171 | * @param int $type The type of sequence resource |
||
| 172 | * @param int $userId |
||
| 173 | * @param int $sessionId |
||
| 174 | */ |
||
| 175 | public function checkRequirementsForUser(array $sequences, int $type, $userId, $sessionId = 0): array |
||
| 176 | { |
||
| 177 | return $this->checkRequirementsOrDependentsForUser( |
||
| 178 | $sequences, |
||
| 179 | $type, |
||
| 180 | self::VERTICES_TYPE_REQ, |
||
| 181 | $userId, |
||
| 182 | $sessionId |
||
| 183 | ); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Check if the ser has completed the requirements for the sequences. |
||
| 188 | * |
||
| 189 | * @param array $sequences The sequences |
||
| 190 | * @param int $type The type of sequence resource |
||
| 191 | * @param int $userId |
||
| 192 | * @param int $sessionId |
||
| 193 | */ |
||
| 194 | public function checkDependentsForUser(array $sequences, int $type, $userId, $sessionId = 0): array |
||
| 195 | { |
||
| 196 | return $this->checkRequirementsOrDependentsForUser( |
||
| 197 | $sequences, |
||
| 198 | $type, |
||
| 199 | self::VERTICES_TYPE_DEP, |
||
| 200 | $userId, |
||
| 201 | $sessionId |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function checkCourseRequirements($userId, Course $course, $sessionId): bool |
||
| 206 | { |
||
| 207 | $em = $this->getEntityManager(); |
||
| 208 | $sessionId = (int) $sessionId; |
||
| 209 | |||
| 210 | $gradebookCategoryRepo = $em->getRepository('ChamiloCoreBundle:GradebookCategory'); |
||
| 211 | $gradebooks = $gradebookCategoryRepo->findBy( |
||
| 212 | [ |
||
| 213 | 'courseCode' => $course->getCode(), |
||
| 214 | 'sessionId' => $sessionId, |
||
| 215 | 'isRequirement' => true, |
||
| 216 | ] |
||
| 217 | ); |
||
| 218 | |||
| 219 | if (empty($gradebooks)) { |
||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | $status = true; |
||
| 224 | foreach ($gradebooks as $gradebook) { |
||
| 225 | $category = Category::createCategoryObjectFromEntity($gradebook); |
||
| 226 | $userFinishedCourse = Category::userFinishedCourse( |
||
| 227 | $userId, |
||
| 228 | $category, |
||
| 229 | true |
||
| 230 | ); |
||
| 231 | if (0 === $sessionId) { |
||
| 232 | if (false === $userFinishedCourse) { |
||
| 233 | $status = false; |
||
| 234 | break; |
||
| 235 | } |
||
| 236 | } else { |
||
| 237 | if (false === $userFinishedCourse) { |
||
| 238 | $status = false; |
||
| 239 | break; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | return $status; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Check if at least one sequence are completed. |
||
| 249 | */ |
||
| 250 | public function checkSequenceAreCompleted(array $sequences, $itemType = self::VERTICES_TYPE_REQ): bool |
||
| 251 | { |
||
| 252 | foreach ($sequences as $sequence) { |
||
| 253 | $status = true; |
||
| 254 | |||
| 255 | foreach ($sequence[$itemType] as $item) { |
||
| 256 | $status = $status && $item['status']; |
||
| 257 | } |
||
| 258 | |||
| 259 | if ($status) { |
||
| 260 | return true; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | return false; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get sessions from vertices. |
||
| 269 | */ |
||
| 270 | protected function findVerticesEdges(Vertices $verticesEdges, int $type): array |
||
| 271 | { |
||
| 272 | $sessionVertices = []; |
||
| 273 | $em = $this->getEntityManager(); |
||
| 274 | |||
| 275 | foreach ($verticesEdges as $supVertex) { |
||
| 276 | $vertexId = $supVertex->getId(); |
||
| 277 | switch ($type) { |
||
| 278 | case SequenceResource::SESSION_TYPE: |
||
| 279 | $resource = $em->getRepository('ChamiloCoreBundle:Session')->find($vertexId); |
||
| 280 | break; |
||
| 281 | case SequenceResource::COURSE_TYPE: |
||
| 282 | $resource = $em->getRepository('ChamiloCoreBundle:Course')->find($vertexId); |
||
| 283 | break; |
||
| 284 | } |
||
| 285 | |||
| 286 | if (empty($resource)) { |
||
| 287 | continue; |
||
| 288 | } |
||
| 289 | |||
| 290 | $sessionVertices[$vertexId] = $resource; |
||
| 291 | } |
||
| 292 | |||
| 293 | return $sessionVertices; |
||
| 294 | } |
||
| 295 | |||
| 296 | private function checkRequirementsOrDependentsForUser( |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get the requirements or dependants for a resource only. |
||
| 406 | */ |
||
| 407 | private function getRequirementsOrDependents($resourceId, int $resourceType, string $itemType): array |
||
| 462 | } |
||
| 463 | } |
||
| 464 |