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