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