Passed
Pull Request — master (#5329)
by Angel Fernando Quiroz
06:56
created

Version20201212195112   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
B up() 0 90 10
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\Entity\ResourceLink;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
13
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
14
use Chamilo\CoreBundle\Repository\SessionRepository;
15
use Chamilo\CourseBundle\Entity\CGroup;
16
use Chamilo\CourseBundle\Entity\CGroupCategory;
17
use Chamilo\CourseBundle\Repository\CGroupCategoryRepository;
18
use Chamilo\CourseBundle\Repository\CGroupRepository;
19
use Doctrine\DBAL\Schema\Schema;
20
21
final class Version20201212195112 extends AbstractMigrationChamilo
22
{
23
    public function getDescription(): string
24
    {
25
        return 'Migrate c_group_info ';
26
    }
27
28
    public function up(Schema $schema): void
29
    {
30
        $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

30
        /** @scrutinizer ignore-call */ 
31
        $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...
31
        $sessionRepo = $this->container->get(SessionRepository::class);
32
        $groupRepo = $this->container->get(CGroupRepository::class);
33
        $groupCategoryRepo = $this->container->get(CGroupCategoryRepository::class);
34
35
        $batchSize = self::BATCH_SIZE;
36
37
        // Migrating c_tool.
38
        $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
0 ignored issues
show
Bug introduced by
The method createQuery() 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

38
        /** @scrutinizer ignore-call */ 
39
        $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');

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...
39
40
        /** @var Course $course */
41
        foreach ($q->toIterable() as $course) {
42
            // Categories
43
            $counter = 1;
44
            $courseId = $course->getId();
45
            $sql = "SELECT * FROM c_group_category
46
                    WHERE c_id = {$courseId} ";
47
            $result = $this->connection->executeQuery($sql);
48
            $categories = $result->fetchAllAssociative();
49
50
            foreach ($categories as $categoryData) {
51
                /** @var CGroupCategory $category */
52
                $category = $groupCategoryRepo->find($categoryData['iid']);
53
                if ($category->hasResourceNode()) {
54
                    continue;
55
                }
56
57
                $course = $courseRepo->find($courseId);
58
                $session = null;
59
                /*if (!empty($groupData['session_id'])) {
60
                    $session = $sessionRepo->find($groupData['session_id']);
61
                }*/
62
63
                $admin = $this->getAdmin();
64
                $category->setParent($course);
65
                $groupRepo->addResourceNode($category, $admin, $course);
66
                $newVisibility = ResourceLink::VISIBILITY_PUBLISHED;
67
                $category->addCourseLink($course, $session, null, $newVisibility);
68
                $this->entityManager->persist($category);
69
                if (($counter % $batchSize) === 0) {
70
                    $this->entityManager->flush();
71
                    $this->entityManager->clear();
72
                }
73
                $counter++;
74
            }
75
76
            $this->entityManager->flush();
77
            $this->entityManager->clear();
78
79
            // Groups
80
            $counter = 1;
81
            $courseId = $course->getId();
82
            $sql = "SELECT * FROM c_group_info
83
                    WHERE c_id = {$courseId} ";
84
            $result = $this->connection->executeQuery($sql);
85
            $groups = $result->fetchAllAssociative();
86
87
            foreach ($groups as $groupData) {
88
                /** @var CGroup $group */
89
                $group = $groupRepo->find($groupData['iid']);
90
                if ($group->hasResourceNode()) {
91
                    continue;
92
                }
93
94
                $course = $courseRepo->find($courseId);
95
                $session = null;
96
                if (!empty($groupData['session_id'])) {
97
                    $session = $sessionRepo->find($groupData['session_id']);
98
                }
99
100
                $admin = $this->getAdmin();
101
                $group->setParent($course);
102
                $groupRepo->addResourceNode($group, $admin, $course);
103
                $newVisibility = ResourceLink::VISIBILITY_PENDING;
104
                if (1 === $group->getStatus()) {
105
                    $newVisibility = ResourceLink::VISIBILITY_PUBLISHED;
106
                }
107
                $group->addCourseLink($course, $session, null, $newVisibility);
108
                $this->entityManager->persist($group);
109
                if (($counter % $batchSize) === 0) {
110
                    $this->entityManager->flush();
111
                    $this->entityManager->clear();
112
                }
113
                $counter++;
114
            }
115
        }
116
        $this->entityManager->flush();
117
        $this->entityManager->clear();
118
    }
119
}
120