| Conditions | 16 |
| Paths | 1156 |
| Total Lines | 106 |
| Code Lines | 65 |
| 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 |
||
| 239 | public function fixItemProperty( |
||
| 240 | $tool, |
||
| 241 | ResourceRepository $repo, |
||
| 242 | $course, |
||
| 243 | $admin, |
||
| 244 | ResourceInterface $resource, |
||
| 245 | $parent, |
||
| 246 | array $items = [] |
||
| 247 | ) { |
||
| 248 | $container = $this->getContainer(); |
||
| 249 | $doctrine = $container->get('doctrine'); |
||
| 250 | $em = $doctrine->getManager(); |
||
| 251 | /** @var Connection $connection */ |
||
| 252 | $connection = $em->getConnection(); |
||
| 253 | |||
| 254 | $courseId = $course->getId(); |
||
| 255 | $id = $resource->getResourceIdentifier(); |
||
| 256 | |||
| 257 | if (empty($items)) { |
||
| 258 | $sql = "SELECT * FROM c_item_property |
||
| 259 | WHERE tool = '{$tool}' AND c_id = {$courseId} AND ref = {$id}"; |
||
| 260 | $result = $connection->executeQuery($sql); |
||
| 261 | $items = $result->fetchAllAssociative(); |
||
| 262 | } |
||
| 263 | |||
| 264 | // For some reason the resource doesnt have a c_item_property value. |
||
| 265 | if (empty($items)) { |
||
| 266 | return false; |
||
| 267 | } |
||
| 268 | |||
| 269 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 270 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 271 | $userRepo = $container->get(UserRepository::class); |
||
| 272 | |||
| 273 | $resource->setParent($parent); |
||
| 274 | $resourceNode = null; |
||
| 275 | $userList = []; |
||
| 276 | $groupList = []; |
||
| 277 | $sessionList = []; |
||
| 278 | foreach ($items as $item) { |
||
| 279 | $visibility = $item['visibility']; |
||
| 280 | $userId = $item['insert_user_id']; |
||
| 281 | $sessionId = $item['session_id'] ?? 0; |
||
| 282 | $groupId = $item['to_group_id'] ?? 0; |
||
| 283 | |||
| 284 | $newVisibility = ResourceLink::VISIBILITY_PENDING; |
||
| 285 | // Old visibility (item property) is based in this switch: |
||
| 286 | switch ($visibility) { |
||
| 287 | case 0: |
||
| 288 | $newVisibility = ResourceLink::VISIBILITY_PENDING; |
||
| 289 | |||
| 290 | break; |
||
| 291 | case 1: |
||
| 292 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
| 293 | |||
| 294 | break; |
||
| 295 | case 2: |
||
| 296 | $newVisibility = ResourceLink::VISIBILITY_DELETED; |
||
| 297 | |||
| 298 | break; |
||
| 299 | } |
||
| 300 | |||
| 301 | // If c_item_property.insert_user_id doesn't exist we use the first admin id. |
||
| 302 | $user = null; |
||
| 303 | if (isset($userList[$userId])) { |
||
| 304 | $user = $userList[$userId]; |
||
| 305 | } else { |
||
| 306 | if (!empty($userId)) { |
||
| 307 | $userFound = $userRepo->find($userId); |
||
| 308 | if ($userFound) { |
||
| 309 | $user = $userList[$userId] = $userRepo->find($userId); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | if (null === $user) { |
||
| 315 | $user = $admin; |
||
| 316 | } |
||
| 317 | |||
| 318 | $session = null; |
||
| 319 | if (!empty($sessionId)) { |
||
| 320 | if (isset($sessionList[$sessionId])) { |
||
| 321 | $session = $sessionList[$sessionId]; |
||
| 322 | } else { |
||
| 323 | $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | $group = null; |
||
| 328 | if (!empty($groupId)) { |
||
| 329 | if (isset($groupList[$groupId])) { |
||
| 330 | $group = $groupList[$groupId]; |
||
| 331 | } else { |
||
| 332 | $group = $groupList[$groupId] = $groupRepo->find($groupId); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | if (null === $resourceNode) { |
||
| 337 | $resourceNode = $repo->addResourceNode($resource, $user, $parent); |
||
| 338 | $em->persist($resourceNode); |
||
| 339 | } |
||
| 340 | $resource->addCourseLink($course, $session, $group, $newVisibility); |
||
| 341 | $em->persist($resource); |
||
| 342 | } |
||
| 343 | |||
| 344 | return true; |
||
| 345 | } |
||
| 347 |