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