Passed
Push — master ( 33172e...a587b8 )
by Julito
116:21
created

Version20201217124011::up()   F

Complexity

Conditions 15
Paths 337

Size

Total Lines 175
Code Lines 116

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 116
c 0
b 0
f 0
nc 337
nop 1
dl 0
loc 175
rs 2.6966

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\Entity\ResourceLink;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
13
use Chamilo\CoreBundle\Repository\Node\UserRepository;
14
use Chamilo\CoreBundle\Repository\SessionRepository;
15
use Chamilo\CourseBundle\Entity\CStudentPublication;
16
use Chamilo\CourseBundle\Entity\CStudentPublicationComment;
17
use Chamilo\CourseBundle\Entity\CStudentPublicationCorrection;
18
use Chamilo\CourseBundle\Repository\CGroupRepository;
19
use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository;
20
use Chamilo\CourseBundle\Repository\CStudentPublicationCorrectionRepository;
21
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
22
use Chamilo\Kernel;
23
use Doctrine\DBAL\Connection;
24
use Doctrine\DBAL\Schema\Schema;
25
26
final class Version20201217124011 extends AbstractMigrationChamilo
27
{
28
    public function getDescription(): string
29
    {
30
        return 'Migrate c_student_publication';
31
    }
32
33
    public function up(Schema $schema): void
34
    {
35
        $container = $this->getContainer();
36
        $doctrine = $container->get('doctrine');
37
        $em = $doctrine->getManager();
38
        /** @var Connection $connection */
39
        $connection = $em->getConnection();
40
41
        $studentPublicationRepo = $container->get(CStudentPublicationRepository::class);
42
        $studentPublicationCommentRepo = $container->get(CStudentPublicationCommentRepository::class);
43
        $studentPublicationCorrectionRepo = $container->get(CStudentPublicationCorrectionRepository::class);
44
45
        $courseRepo = $container->get(CourseRepository::class);
46
        $sessionRepo = $container->get(SessionRepository::class);
47
        $groupRepo = $container->get(CGroupRepository::class);
48
        //$userRepo = $container->get(UserRepository::class);
49
        /** @var Kernel $kernel */
50
        $kernel = $container->get('kernel');
51
        $rootPath = $kernel->getProjectDir();
52
53
        $admin = $this->getAdmin();
54
55
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
56
        /** @var Course $course */
57
        foreach ($q->toIterable() as $course) {
58
            $courseId = $course->getId();
59
            $course = $courseRepo->find($courseId);
60
61
            // Assignments folders.
62
            $sql = "SELECT * FROM c_student_publication WHERE contains_file = 0 AND c_id = {$courseId}
63
                    ORDER BY iid";
64
            $result = $connection->executeQuery($sql);
65
            $items = $result->fetchAllAssociative();
66
            foreach ($items as $itemData) {
67
                $id = $itemData['iid'];
68
                /** @var CStudentPublication $resource */
69
                $resource = $studentPublicationRepo->find($id);
70
                if ($resource->hasResourceNode()) {
71
                    continue;
72
                }
73
74
                $result = $this->fixItemProperty(
75
                    'work',
76
                    $studentPublicationRepo,
77
                    $course,
78
                    $admin,
79
                    $resource,
80
                    $course
81
                );
82
83
                if (false === $result) {
84
                    continue;
85
                }
86
87
                $em->persist($resource);
88
                $em->flush();
89
            }
90
91
            $em->flush();
92
            $em->clear();
93
94
            // Assignments files.
95
            $sql = "SELECT * FROM c_student_publication
96
                    WHERE
97
                          contains_file = 1 AND
98
                          c_id = {$courseId}
99
                    ORDER BY iid";
100
            $result = $connection->executeQuery($sql);
101
            $items = $result->fetchAllAssociative();
102
            foreach ($items as $itemData) {
103
                $course = $courseRepo->find($courseId);
104
                $id = $itemData['iid'];
105
                $path = $itemData['url'];
106
                $title = $itemData['title'];
107
                $parentId = $itemData['parent_id'];
108
                /** @var CStudentPublication $resource */
109
                $resource = $studentPublicationRepo->find($id);
110
                if ($resource->hasResourceNode()) {
111
                    continue;
112
                }
113
114
                $parent = $studentPublicationRepo->find($parentId);
115
116
                $result = $this->fixItemProperty(
117
                    'work',
118
                    $studentPublicationRepo,
119
                    $course,
120
                    $admin,
121
                    $resource,
122
                    $parent
123
                );
124
125
                if (false === $result) {
126
                    continue;
127
                }
128
129
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$path;
130
                $this->addLegacyFileToResource($filePath, $studentPublicationRepo, $resource, $id, $title);
131
                $em->persist($resource);
132
                $em->flush();
133
            }
134
135
            $em->flush();
136
            $em->clear();
137
138
            $admin = $this->getAdmin();
139
140
            // Corrections.
141
            $sql = "SELECT * FROM c_student_publication
142
                    WHERE
143
                          (title_correction <> '' OR title_correction IS NOT NULL) AND
144
                          c_id = {$courseId}
145
                    ORDER BY iid";
146
            $result = $connection->executeQuery($sql);
147
            $items = $result->fetchAllAssociative();
148
            foreach ($items as $itemData) {
149
                $id = $itemData['iid'];
150
                $title = $itemData['title_correction'];
151
                $path = $itemData['url_correction'];
152
153
                $course = $courseRepo->find($courseId);
154
                /** @var CStudentPublication $studentPublication */
155
                $studentPublication = $studentPublicationRepo->find($id);
156
                $correction = $studentPublication->getCorrection();
157
                if (null !== $correction) {
158
                    continue;
159
                }
160
161
                $correction = new CStudentPublicationCorrection();
162
                $correction->setTitle($title);
163
                $correction->setParent($studentPublication);
164
                $studentPublicationCorrectionRepo->addResourceNode($correction, $admin, $studentPublication);
165
                $em->persist($correction);
166
167
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$path;
168
                $this->addLegacyFileToResource($filePath, $studentPublicationCorrectionRepo, $correction, null, $title);
169
                $em->persist($correction);
170
                $em->flush();
171
            }
172
173
            $em->flush();
174
            $em->clear();
175
176
            // Comments.
177
            $sql = "SELECT * FROM c_student_publication_comment WHERE c_id = {$courseId}
178
                    ORDER BY iid";
179
            $result = $connection->executeQuery($sql);
180
            $items = $result->fetchAllAssociative();
181
            foreach ($items as $itemData) {
182
                $id = $itemData['iid'];
183
                $file = $itemData['file'];
184
                $workId = $itemData['work_id'];
185
                /** @var CStudentPublicationComment $resource */
186
                $resource = $studentPublicationCommentRepo->find($id);
187
                if ($resource->hasResourceNode()) {
188
                    continue;
189
                }
190
                /** @var CStudentPublication $parent */
191
                $parent = $studentPublicationRepo->find($workId);
192
                $sql = "SELECT * FROM c_student_publication WHERE c_id = {$courseId} AND id = {$workId}";
193
                $result = $connection->executeQuery($sql);
194
                $work = $result->fetchAssociative();
195
                if (empty($work)) {
196
                    continue;
197
                }
198
                $session = empty($work['session_id']) ? null : $sessionRepo->find($work['session_id']);
199
                $group = empty($work['post_group_id']) ? null : $groupRepo->find($work['post_group_id']);
200
201
                $resource->setParent($parent);
202
                $resource->addCourseLink($course, $session, $group, ResourceLink::VISIBILITY_PUBLISHED);
203
204
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$file;
205
                $this->addLegacyFileToResource($filePath, $studentPublicationRepo, $resource, $id, $title);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
206
                $em->persist($resource);
207
                $em->flush();
208
            }
209
        }
210
    }
211
}
212