Passed
Push — master ( 158d97...1c5d74 )
by Julito
11:02
created

Version20201212195011::up()   F

Complexity

Conditions 15
Paths 315

Size

Total Lines 113
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 74
nc 315
nop 1
dl 0
loc 113
rs 3.4839
c 0
b 0
f 0

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\AccessUrl;
10
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
11
use Chamilo\CoreBundle\Entity\Course;
12
use Chamilo\CoreBundle\Entity\ExtraField;
13
use Chamilo\CoreBundle\Entity\ResourceLink;
14
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
15
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
16
use Chamilo\CoreBundle\Repository\Node\UserRepository;
17
use Chamilo\CoreBundle\Repository\SessionRepository;
18
use Chamilo\CourseBundle\Entity\CTool;
19
use Chamilo\CourseBundle\Repository\CToolRepository;
20
use Doctrine\DBAL\Connection;
21
use Doctrine\DBAL\Schema\Schema;
22
23
final class Version20201212195011 extends AbstractMigrationChamilo
24
{
25
    public function getDescription(): string
26
    {
27
        return 'Migrate courses, c_tool ';
28
    }
29
30
    public function up(Schema $schema): void
31
    {
32
        $container = $this->getContainer();
33
        $em = $this->getEntityManager();
34
        $connection = $em->getConnection();
35
36
        $courseRepo = $container->get(CourseRepository::class);
37
        $sessionRepo = $container->get(SessionRepository::class);
38
        $toolRepo = $container->get(CToolRepository::class);
39
        $urlRepo = $em->getRepository(AccessUrl::class);
40
        $userRepo = $container->get(UserRepository::class);
41
42
        $batchSize = self::BATCH_SIZE;
43
        $admin = $this->getAdmin();
44
        $adminId = $admin->getId();
45
46
        // Adding courses to the resource node tree.
47
        $urls = $urlRepo->findAll();
48
49
        /** @var AccessUrl $url */
50
        foreach ($urls as $url) {
51
            $counter = 1;
52
            /** @var AccessUrl $urlEntity */
53
            $urlEntity = $urlRepo->find($url->getId());
54
            $accessUrlRelCourses = $urlEntity->getCourses();
55
            /** @var AccessUrlRelCourse $accessUrlRelCourse */
56
            foreach ($accessUrlRelCourses as $accessUrlRelCourse) {
57
                $course = $accessUrlRelCourse->getCourse();
58
                $course = $courseRepo->find($course->getId());
59
                if ($course->hasResourceNode()) {
60
                    continue;
61
                }
62
                $urlEntity = $urlRepo->find($url->getId());
63
                $adminEntity = $userRepo->find($adminId);
64
                $courseRepo->addResourceNode($course, $adminEntity, $urlEntity);
65
                $em->persist($course);
66
67
                // Add groups.
68
                //$course = $course->getGroups();
69
                if (($counter % $batchSize) === 0) {
70
                    $em->flush();
71
                    $em->clear(); // Detaches all objects from Doctrine!
72
                }
73
                $counter++;
74
            }
75
        }
76
77
        $em->flush();
78
        $em->clear();
79
80
        // Special course.
81
        $extraFieldType = ExtraField::COURSE_FIELD_TYPE;
82
        $sql = "SELECT id FROM extra_field
83
                WHERE extra_field_type = $extraFieldType AND variable = 'special_course'";
84
        $result = $connection->executeQuery($sql);
85
        $extraFieldId = $result->fetchOne();
86
87
        $specialCourses = '';
88
        if (!empty($extraFieldId)) {
89
            $sql = 'SELECT DISTINCT(item_id)
90
                    FROM extra_field_values
91
                    WHERE field_id = '.$extraFieldId." AND value = '1'";
92
            $result = $connection->executeQuery($sql);
93
            $specialCourses = $result->fetchAllAssociative();
94
            if (!empty($specialCourses)) {
95
                $specialCourses = array_column($specialCourses, 'item_id');
96
            }
97
        }
98
99
        // Migrating c_tool.
100
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
101
        /** @var Course $course */
102
        foreach ($q->toIterable() as $course) {
103
            $counter = 1;
104
            $courseId = $course->getId();
105
106
            if (!empty($specialCourses) && in_array($courseId, $specialCourses)) {
107
                $this->addSql("UPDATE course SET sticky = 1 WHERE id = $courseId ");
108
            }
109
110
            $sql = "SELECT * FROM c_tool
111
                    WHERE c_id = {$courseId} ";
112
            $result = $connection->executeQuery($sql);
113
            $tools = $result->fetchAllAssociative();
114
115
            foreach ($tools as $toolData) {
116
                /** @var CTool $tool */
117
                $tool = $toolRepo->find($toolData['iid']);
118
                if ($tool->hasResourceNode()) {
119
                    continue;
120
                }
121
122
                $course = $courseRepo->find($courseId);
123
                $session = null;
124
                if (!empty($toolData['session_id'])) {
125
                    $session = $sessionRepo->find($toolData['session_id']);
126
                }
127
128
                $admin = $this->getAdmin();
129
                $tool->setParent($course);
130
                $toolRepo->addResourceNode($tool, $admin, $course);
131
                $newVisibility = 1 === (int) $toolData['visibility'] ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_PENDING;
132
                $tool->addCourseLink($course, $session, null, $newVisibility);
133
                $em->persist($tool);
134
                if (($counter % $batchSize) === 0) {
135
                    $em->flush();
136
                    $em->clear(); // Detaches all objects from Doctrine!
137
                }
138
                $counter++;
139
            }
140
        }
141
        $em->flush();
142
        $em->clear();
143
    }
144
}
145