| Conditions | 16 |
| Paths | 1732 |
| Total Lines | 109 |
| 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 |
||
| 223 | public function fixItemProperty( |
||
| 224 | $tool, |
||
| 225 | ResourceRepository $repo, |
||
| 226 | $course, |
||
| 227 | $admin, |
||
| 228 | ResourceInterface $resource, |
||
| 229 | $parentResource, |
||
| 230 | array $items = [] |
||
| 231 | ) { |
||
| 232 | $container = $this->getContainer(); |
||
| 233 | $em = $this->getEntityManager(); |
||
| 234 | $connection = $em->getConnection(); |
||
| 235 | |||
| 236 | $courseId = $course->getId(); |
||
| 237 | $id = $resource->getResourceIdentifier(); |
||
| 238 | |||
| 239 | if (empty($items)) { |
||
| 240 | $sql = "SELECT * FROM c_item_property |
||
| 241 | WHERE tool = '{$tool}' AND c_id = {$courseId} AND ref = {$id}"; |
||
| 242 | $result = $connection->executeQuery($sql); |
||
| 243 | $items = $result->fetchAllAssociative(); |
||
| 244 | } |
||
| 245 | |||
| 246 | // For some reason the resource doesn't have a c_item_property value. |
||
| 247 | if (empty($items)) { |
||
| 248 | return false; |
||
| 249 | } |
||
| 250 | |||
| 251 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 252 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 253 | $userRepo = $container->get(UserRepository::class); |
||
| 254 | |||
| 255 | $resource->setParent($parentResource); |
||
|
|
|||
| 256 | $resourceNode = null; |
||
| 257 | $userList = []; |
||
| 258 | $groupList = []; |
||
| 259 | $sessionList = []; |
||
| 260 | foreach ($items as $item) { |
||
| 261 | $visibility = (int) $item['visibility']; |
||
| 262 | $userId = (int) $item['insert_user_id']; |
||
| 263 | $sessionId = $item['session_id'] ?? 0; |
||
| 264 | $groupId = $item['to_group_id'] ?? 0; |
||
| 265 | $lastUpdatedAt = new \DateTime($item['lastedit_date'], new \DateTimeZone('UTC')); |
||
| 266 | |||
| 267 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
| 268 | |||
| 269 | // Old 1.11.x visibility (item property) is based in this switch: |
||
| 270 | switch ($visibility) { |
||
| 271 | case 0: |
||
| 272 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
| 273 | |||
| 274 | break; |
||
| 275 | |||
| 276 | case 1: |
||
| 277 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
| 278 | |||
| 279 | break; |
||
| 280 | } |
||
| 281 | |||
| 282 | // If c_item_property.insert_user_id doesn't exist we use the first admin id. |
||
| 283 | $user = null; |
||
| 284 | if (isset($userList[$userId])) { |
||
| 285 | $user = $userList[$userId]; |
||
| 286 | } else { |
||
| 287 | if (!empty($userId)) { |
||
| 288 | $userFound = $userRepo->find($userId); |
||
| 289 | if ($userFound) { |
||
| 290 | $user = $userList[$userId] = $userRepo->find($userId); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | if (null === $user) { |
||
| 296 | $user = $admin; |
||
| 297 | } |
||
| 298 | |||
| 299 | $session = null; |
||
| 300 | if (!empty($sessionId)) { |
||
| 301 | if (isset($sessionList[$sessionId])) { |
||
| 302 | $session = $sessionList[$sessionId]; |
||
| 303 | } else { |
||
| 304 | $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $group = null; |
||
| 309 | if (!empty($groupId)) { |
||
| 310 | if (isset($groupList[$groupId])) { |
||
| 311 | $group = $groupList[$groupId]; |
||
| 312 | } else { |
||
| 313 | $group = $groupList[$groupId] = $groupRepo->find($groupId); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | if (null === $resourceNode) { |
||
| 318 | $resourceNode = $repo->addResourceNode($resource, $user, $parentResource); |
||
| 319 | $em->persist($resourceNode); |
||
| 320 | } |
||
| 321 | $resource->addCourseLink($course, $session, $group, $newVisibility); |
||
| 322 | |||
| 323 | if (2 === $visibility) { |
||
| 324 | $link = $resource->getResourceNode()->getResourceLinkByContext($course, $session, $group); |
||
| 325 | $link->setDeletedAt($lastUpdatedAt); |
||
| 326 | } |
||
| 327 | |||
| 328 | $em->persist($resource); |
||
| 329 | } |
||
| 330 | |||
| 331 | return true; |
||
| 332 | } |
||
| 357 |