Passed
Push — master ( 0b60a0...db12d9 )
by Julito
12:26
created

Version20201212195011::up()   C

Complexity

Conditions 12
Paths 60

Size

Total Lines 84
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 12
eloc 58
nc 60
nop 1
dl 0
loc 84
rs 6.4896
c 2
b 1
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
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
4
5
use Chamilo\CoreBundle\Entity\AccessUrl;
6
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
7
use Chamilo\CoreBundle\Entity\ResourceLink;
8
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
9
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
10
use Chamilo\CoreBundle\Repository\SessionRepository;
11
use Chamilo\CourseBundle\Entity\CTool;
12
use Chamilo\CourseBundle\Repository\CToolRepository;
13
use Doctrine\DBAL\Connection;
14
use Doctrine\DBAL\Schema\Schema;
15
16
final class Version20201212195011 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Migrate courses, c_tool ';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $container = $this->getContainer();
26
        $doctrine = $container->get('doctrine');
27
        $em = $doctrine->getManager();
28
        /** @var Connection $connection */
29
        $connection = $em->getConnection();
30
        $courseRepo = $container->get(CourseRepository::class);
31
        $sessionRepo = $container->get(SessionRepository::class);
32
        $toolRepo = $container->get(CToolRepository::class);
33
        $urlRepo = $em->getRepository(AccessUrl::class);
34
35
        $batchSize = self::BATCH_SIZE;
36
        $admin = $this->getAdmin();
37
38
        // Adding courses to the resource node tree.
39
        $urls = $urlRepo->findAll();
40
        /** @var AccessUrl $url */
41
        foreach ($urls as $url) {
42
            $counter = 1;
43
            $url = $urlRepo->find($url->getId());
44
            $accessUrlRelCourses = $url->getCourses();
45
            /** @var AccessUrlRelCourse $accessUrlRelCourse */
46
            foreach ($accessUrlRelCourses as $accessUrlRelCourse) {
47
                $course = $accessUrlRelCourse->getCourse();
48
                $course = $courseRepo->find($course->getId());
49
                if ($course->hasResourceNode()) {
50
                    continue;
51
                }
52
                $courseRepo->addResourceNode($course, $admin, $url);
53
                $em->persist($course);
54
55
                // Add groups.
56
                //$course = $course->getGroups();
57
                if (0 === $counter % $batchSize) {
58
                    $em->flush();
59
                    $em->clear(); // Detaches all objects from Doctrine!
60
                }
61
                $counter++;
62
            }
63
        }
64
        $em->flush();
65
        $em->clear();
66
67
        foreach ($urls as $url) {
68
            $accessUrlRelCourses = $url->getCourses();
69
            /** @var AccessUrlRelCourse $accessUrlRelCourse */
70
            foreach ($accessUrlRelCourses as $accessUrlRelCourse) {
71
                $counter = 1;
72
                $course = $accessUrlRelCourse->getCourse();
73
                $courseId = $course->getId();
74
                $sql = "SELECT * FROM c_tool
75
                        WHERE c_id = $courseId ";
76
                $result = $connection->executeQuery($sql);
77
                $tools = $result->fetchAllAssociative();
78
                foreach ($tools as $toolData) {
79
                    /** @var CTool $tool */
80
                    $tool = $toolRepo->find($toolData['iid']);
81
                    if ($tool->hasResourceNode()) {
82
                        continue;
83
                    }
84
                    $course = $courseRepo->find($course->getId());
85
                    $session = null;
86
                    if (!empty($toolData['session_id'])) {
87
                        $session = $sessionRepo->find($toolData['session_id']);
88
                    }
89
90
                    $admin = $this->getAdmin();
91
                    $tool->setParent($course);
92
                    $toolRepo->addResourceNode($tool, $admin, $course);
93
                    $newVisibility = 1 === $toolData['visibility'] ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_PENDING;
94
                    $tool->addCourseLink($course, $session, null, $newVisibility);
95
                    $em->persist($tool);
96
                    if (0 === $counter % $batchSize) {
97
                        $em->flush();
98
                        $em->clear(); // Detaches all objects from Doctrine!
99
                    }
100
                    $counter++;
101
                }
102
            }
103
        }
104
105
        $em->flush();
106
        $em->clear();
107
    }
108
}
109