| Conditions | 14 |
| Paths | 580 |
| Total Lines | 98 |
| Code Lines | 64 |
| 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 |
||
| 218 | public function fixItemProperty( |
||
| 219 | $tool, |
||
| 220 | $repo, |
||
| 221 | $course, |
||
| 222 | $admin, |
||
| 223 | ResourceInterface $resource, |
||
| 224 | $parent, |
||
| 225 | array $items = [] |
||
| 226 | ) { |
||
| 227 | $container = $this->getContainer(); |
||
| 228 | $doctrine = $container->get('doctrine'); |
||
| 229 | $em = $doctrine->getManager(); |
||
| 230 | /** @var Connection $connection */ |
||
| 231 | $connection = $em->getConnection(); |
||
| 232 | |||
| 233 | $courseId = $course->getId(); |
||
| 234 | $id = $resource->getResourceIdentifier(); |
||
| 235 | |||
| 236 | if (empty($items)) { |
||
| 237 | $sql = "SELECT * FROM c_item_property |
||
| 238 | WHERE tool = '$tool' AND c_id = $courseId AND ref = $id"; |
||
| 239 | $result = $connection->executeQuery($sql); |
||
| 240 | $items = $result->fetchAllAssociative(); |
||
| 241 | } |
||
| 242 | |||
| 243 | // For some reason the resource doesnt have a c_item_property value. |
||
| 244 | if (empty($items)) { |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | |||
| 248 | $container = $this->getContainer(); |
||
| 249 | $doctrine = $container->get('doctrine'); |
||
| 250 | $em = $doctrine->getManager(); |
||
| 251 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 252 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 253 | $userRepo = $container->get(UserRepository::class); |
||
| 254 | |||
| 255 | $resource->setParent($parent); |
||
| 256 | $resourceNode = null; |
||
| 257 | $userList = []; |
||
| 258 | $groupList = []; |
||
| 259 | $sessionList = []; |
||
| 260 | foreach ($items as $item) { |
||
| 261 | $visibility = $item['visibility']; |
||
| 262 | $userId = $item['insert_user_id']; |
||
| 263 | $sessionId = $item['session_id'] ?? 0; |
||
| 264 | $groupId = $item['to_group_id'] ?? 0; |
||
| 265 | |||
| 266 | $newVisibility = ResourceLink::VISIBILITY_PENDING; |
||
| 267 | switch ($visibility) { |
||
| 268 | case 0: |
||
| 269 | $newVisibility = ResourceLink::VISIBILITY_PENDING; |
||
| 270 | break; |
||
| 271 | case 1: |
||
| 272 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
| 273 | break; |
||
| 274 | case 2: |
||
| 275 | $newVisibility = ResourceLink::VISIBILITY_DELETED; |
||
| 276 | break; |
||
| 277 | } |
||
| 278 | |||
| 279 | if (isset($userList[$userId])) { |
||
| 280 | $user = $userList[$userId]; |
||
| 281 | } else { |
||
| 282 | $user = $userList[$userId] = $userRepo->find($userId); |
||
| 283 | } |
||
| 284 | |||
| 285 | if (null === $user) { |
||
| 286 | $user = $admin; |
||
| 287 | } |
||
| 288 | |||
| 289 | $session = null; |
||
| 290 | if (!empty($sessionId)) { |
||
| 291 | if (isset($sessionList[$sessionId])) { |
||
| 292 | $session = $sessionList[$sessionId]; |
||
| 293 | } else { |
||
| 294 | $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | $group = null; |
||
| 299 | if (!empty($groupId)) { |
||
| 300 | if (isset($groupList[$groupId])) { |
||
| 301 | $group = $groupList[$groupId]; |
||
| 302 | } else { |
||
| 303 | $group = $groupList[$groupId] = $groupRepo->find($groupId); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | if (null === $resourceNode) { |
||
| 308 | $resourceNode = $repo->addResourceNode($resource, $user, $parent); |
||
| 309 | $em->persist($resourceNode); |
||
| 310 | } |
||
| 311 | $resource->addCourseLink($course, $session, $group, $newVisibility); |
||
| 312 | $em->persist($resource); |
||
| 313 | } |
||
| 314 | |||
| 315 | return true; |
||
| 316 | } |
||
| 318 |