Version20201212195011::up()   F
last analyzed

Complexity

Conditions 15
Paths 315

Size

Total Lines 112
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 72
nc 315
nop 1
dl 0
loc 112
rs 3.6458
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\AccessUrlRepository;
16
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
17
use Chamilo\CoreBundle\Repository\Node\UserRepository;
18
use Chamilo\CoreBundle\Repository\SessionRepository;
19
use Chamilo\CourseBundle\Entity\CTool;
20
use Chamilo\CourseBundle\Repository\CToolRepository;
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
        $courseRepo = $this->container->get(CourseRepository::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

32
        /** @scrutinizer ignore-call */ 
33
        $courseRepo = $this->container->get(CourseRepository::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...
33
        $sessionRepo = $this->container->get(SessionRepository::class);
34
        $toolRepo = $this->container->get(CToolRepository::class);
35
        $urlRepo = $this->container->get(AccessUrlRepository::class);
36
        $userRepo = $this->container->get(UserRepository::class);
37
38
        $batchSize = self::BATCH_SIZE;
39
        $admin = $this->getAdmin();
40
        $adminId = $admin->getId();
41
42
        // Adding courses to the resource node tree.
43
        $urls = $urlRepo->findAll();
44
45
        /** @var AccessUrl $url */
46
        foreach ($urls as $url) {
47
            $counter = 1;
48
49
            /** @var AccessUrl $urlEntity */
50
            $urlEntity = $urlRepo->find($url->getId());
51
            $accessUrlRelCourses = $urlEntity->getCourses();
52
53
            /** @var AccessUrlRelCourse $accessUrlRelCourse */
54
            foreach ($accessUrlRelCourses as $accessUrlRelCourse) {
55
                $course = $accessUrlRelCourse->getCourse();
56
                $course = $courseRepo->find($course->getId());
57
                if ($course->hasResourceNode()) {
58
                    continue;
59
                }
60
                $urlEntity = $urlRepo->find($url->getId());
61
                $adminEntity = $userRepo->find($adminId);
62
                $courseRepo->addResourceNode($course, $adminEntity, $urlEntity);
63
                $this->entityManager->persist($course);
0 ignored issues
show
Bug introduced by
The method persist() 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

63
                $this->entityManager->/** @scrutinizer ignore-call */ 
64
                                      persist($course);

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...
64
65
                // Add groups.
66
                // $course = $course->getGroups();
67
                if (($counter % $batchSize) === 0) {
68
                    $this->entityManager->flush();
69
                    $this->entityManager->clear(); // Detaches all objects from Doctrine!
70
                }
71
                $counter++;
72
            }
73
        }
74
75
        $this->entityManager->flush();
76
        $this->entityManager->clear();
77
78
        // Special course.
79
        $extraFieldType = ExtraField::COURSE_FIELD_TYPE;
80
        $sql = "SELECT id FROM extra_field
81
                WHERE item_type = $extraFieldType AND variable = 'special_course'";
82
        $result = $this->connection->executeQuery($sql);
83
        $extraFieldId = $result->fetchOne();
84
85
        $specialCourses = [];
86
        if (!empty($extraFieldId)) {
87
            $extraFieldId = (int) $extraFieldId;
88
            $sql = "SELECT DISTINCT(item_id)
89
                    FROM extra_field_values
90
                    WHERE field_id = $extraFieldId AND field_value= 1 ";
91
            $result = $this->connection->executeQuery($sql);
92
            $specialCourses = $result->fetchAllAssociative();
93
            if (!empty($specialCourses)) {
94
                $specialCourses = array_map('intval', array_column($specialCourses, 'item_id'));
95
            }
96
        }
97
98
        // Migrating c_tool.
99
        $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
100
101
        /** @var Course $course */
102
        foreach ($q->toIterable() as $course) {
103
            $counter = 1;
104
            $courseId = $course->getId();
105
            if (!empty($specialCourses) && \in_array($courseId, $specialCourses, true)) {
106
                $this->addSql("UPDATE course SET sticky = 1 WHERE id = $courseId ");
107
            }
108
109
            $sql = "SELECT * FROM c_tool
110
                    WHERE c_id = {$courseId} ";
111
            $result = $this->connection->executeQuery($sql);
112
            $tools = $result->fetchAllAssociative();
113
114
            foreach ($tools as $toolData) {
115
                /** @var CTool $tool */
116
                $tool = $toolRepo->find($toolData['iid']);
117
                if ($tool->hasResourceNode()) {
118
                    continue;
119
                }
120
121
                $course = $courseRepo->find($courseId);
122
                $session = null;
123
                if (!empty($toolData['session_id'])) {
124
                    $session = $sessionRepo->find($toolData['session_id']);
125
                }
126
127
                $admin = $this->getAdmin();
128
                $tool->setParent($course);
129
                $toolRepo->addResourceNode($tool, $admin, $course);
130
                $newVisibility = 1 === (int) $toolData['visibility'] ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT;
131
                $tool->addCourseLink($course, $session, null, $newVisibility);
132
                $this->entityManager->persist($tool);
133
                if (($counter % $batchSize) === 0) {
134
                    $this->entityManager->flush();
135
                    $this->entityManager->clear(); // Detaches all objects from Doctrine!
136
                }
137
                $counter++;
138
            }
139
        }
140
        $this->entityManager->flush();
141
        $this->entityManager->clear();
142
    }
143
}
144