Passed
Push — master ( 6c23c3...c1688f )
by Julito
09:45
created

Version20201216122012::up()   C

Complexity

Conditions 10
Paths 25

Size

Total Lines 121
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 78
c 1
b 0
f 0
nc 25
nop 1
dl 0
loc 121
rs 6.6133

How to fix   Long Method    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
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
12
use Chamilo\CoreBundle\Repository\Node\UserRepository;
13
use Chamilo\CoreBundle\Repository\SessionRepository;
14
use Chamilo\CourseBundle\Entity\CLp;
15
use Chamilo\CourseBundle\Entity\CLpCategory;
16
use Chamilo\CourseBundle\Entity\CLpItem;
17
use Chamilo\CourseBundle\Repository\CGroupRepository;
18
use Chamilo\CourseBundle\Repository\CLpCategoryRepository;
19
use Chamilo\CourseBundle\Repository\CLpRepository;
20
use Doctrine\DBAL\Connection;
21
use Doctrine\DBAL\Schema\Schema;
22
23
final class Version20201216122012 extends AbstractMigrationChamilo
24
{
25
    public function getDescription(): string
26
    {
27
        return 'Migrate c_lp, c_lp_category';
28
    }
29
30
    public function up(Schema $schema): void
31
    {
32
        $container = $this->getContainer();
33
        $doctrine = $container->get('doctrine');
34
        $em = $doctrine->getManager();
35
        /** @var Connection $connection */
36
        $connection = $em->getConnection();
37
38
        $lpCategoryRepo = $container->get(CLpCategoryRepository::class);
39
        $lpRepo = $container->get(CLpRepository::class);
40
41
        $courseRepo = $container->get(CourseRepository::class);
42
        $sessionRepo = $container->get(SessionRepository::class);
43
        $groupRepo = $container->get(CGroupRepository::class);
44
        $userRepo = $container->get(UserRepository::class);
45
46
        $lpItemRepo = $container->get(CLpItem::class);
47
48
        $admin = $this->getAdmin();
49
50
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
51
        /** @var Course $course */
52
        foreach ($q->toIterable() as $course) {
53
            $courseId = $course->getId();
54
            $course = $courseRepo->find($courseId);
55
56
            // c_lp_category.
57
            $sql = "SELECT * FROM c_lp_category WHERE c_id = {$courseId}
58
                    ORDER BY iid";
59
            $result = $connection->executeQuery($sql);
60
            $items = $result->fetchAllAssociative();
61
            foreach ($items as $itemData) {
62
                $id = $itemData['iid'];
63
                /** @var CLpCategory $resource */
64
                $resource = $lpCategoryRepo->find($id);
65
                if ($resource->hasResourceNode()) {
66
                    continue;
67
                }
68
69
                $result = $this->fixItemProperty(
70
                    'learnpath_category',
71
                    $lpCategoryRepo,
72
                    $course,
73
                    $admin,
74
                    $resource,
75
                    $course
76
                );
77
78
                if (false === $result) {
79
                    continue;
80
                }
81
82
                $em->persist($resource);
83
                $em->flush();
84
            }
85
86
            $em->flush();
87
            $em->clear();
88
89
            // c_lp.
90
            $sql = "SELECT * FROM c_lp WHERE c_id = {$courseId}
91
                    ORDER BY iid";
92
            $result = $connection->executeQuery($sql);
93
            $lps = $result->fetchAllAssociative();
94
            foreach ($lps as $lp) {
95
                $lpId = $lp['iid'];
96
97
                /** @var CLp $resource */
98
                $resource = $lpRepo->find($lpId);
99
                if ($resource->hasResourceNode()) {
100
                    continue;
101
                }
102
103
                $course = $courseRepo->find($courseId);
104
105
                $result = $this->fixItemProperty(
106
                    'learnpath',
107
                    $lpRepo,
108
                    $course,
109
                    $admin,
110
                    $resource,
111
                    $course
112
                );
113
114
                if (false === $result) {
115
                    continue;
116
                }
117
118
                $em->persist($resource);
119
                $em->flush();
120
121
                $itemRoot = $lpItemRepo->getItemRoot($lpId);
122
123
                if (!empty($itemRoot)) {
124
                    continue;
125
                }
126
127
                $lpItem = new CLpItem();
128
                $lpItem
129
                    ->setTitle('root')
130
                    ->setPath('root')
131
                    ->setLp($resource)
132
                    ->setItemType('root');
133
                $em->persist($lpItem);
134
                $em->flush();
135
136
                // Migrate c_lp_item
137
                $sql = "SELECT * FROM c_lp_item WHERE lp_id = $lpId
138
                        ORDER BY display_order";
139
140
                $resultItems = $connection->executeQuery($sql);
141
                $lpItems = $resultItems->fetchAllAssociative();
142
                $orderList = [];
143
                foreach ($lpItems as $item) {
144
                    $object = new \stdClass();
145
                    $object->id = $item['iid'];
146
                    $object->parent_id = $item['parentId'];
147
                    $orderList[] = $object;
148
                }
149
150
                \learnpath::sortItemByOrderList($lpId, $orderList);
151
            }
152
        }
153
    }
154
}
155