Passed
Push — alpha ( a09b96...74d5c7 )
by Yannick
08:19
created

Version20240327172830::up()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 28
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 49
rs 7.3166

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CourseBundle\Repository\CLpCategoryRepository;
11
use Chamilo\CourseBundle\Repository\CLpRepository;
12
use Chamilo\CourseBundle\Repository\CShortcutRepository;
13
use Doctrine\DBAL\Schema\Schema;
14
15
class Version20240327172830 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Create shortcuts for c_lp and c_lp_category published on course home';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        // File generated in the Version20180928172830 migration
25
        $toolLinksContent = $this->readFile('tool_links');
26
27
        if (empty($toolLinksContent)) {
28
            $this->write('tool_links file not found. Exiting.');
29
30
            return;
31
        }
32
33
        $toolLinks = unserialize($toolLinksContent);
34
35
        $lpRepo = $this->container->get(CLpRepository::class);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $lpRepo = $this->container->get(CLpRepository::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        $lpCategoryRepo = $this->container->get(CLpCategoryRepository::class);
37
        $shortcutRepo = $this->container->get(CShortcutRepository::class);
38
39
        foreach ($toolLinks as $toolLink) {
40
            $url = parse_url($toolLink['link']);
41
            $query = [];
42
            parse_str($url['query'] ?? '', $query);
43
44
            $admin = $this->getAdmin();
45
            $course = $this->findCourse($toolLink['c_id']);
46
            $session = $this->findSession($toolLink['session_id']);
47
            $resource = null;
48
49
            if (str_contains($url['path'], 'lp_controller.php') && isset($query['action'])) {
50
                if (isset($query['lp_id']) && 'view' === $query['action']) {
51
                    $resource = $lpRepo->find($query['lp_id']);
52
                } elseif (isset($query['id']) && 'view_category' === $query['action']) {
53
                    $resource = $lpCategoryRepo->find($query['id']);
54
                }
55
            }
56
57
            if ($resource) {
58
                $shortcut = $shortcutRepo->getShortcutFromResource($resource);
59
60
                if ($shortcut) {
61
                    continue;
62
                }
63
64
                $shortcutRepo->addShortCut($resource, $admin, $course, $session);
65
            }
66
        }
67
68
        $this->entityManager->flush();
0 ignored issues
show
Bug introduced by
The method flush() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $this->entityManager->/** @scrutinizer ignore-call */ 
69
                              flush();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
70
        $this->removeFile('tool_links');
71
    }
72
}
73