Passed
Push — master ( cd85be...b9a35c )
by Julito
12:36
created

Version20201215160445::up()   F

Complexity

Conditions 21
Paths 4001

Size

Total Lines 216
Code Lines 138

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 21
eloc 138
c 2
b 0
f 0
nc 4001
nop 1
dl 0
loc 216
rs 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
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
9
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
10
use Chamilo\CoreBundle\Repository\Node\UserRepository;
11
use Chamilo\CoreBundle\Repository\SessionRepository;
12
use Chamilo\CourseBundle\Entity\CForumCategory;
13
use Chamilo\CourseBundle\Entity\CForumForum;
14
use Chamilo\CourseBundle\Entity\CForumPost;
15
use Chamilo\CourseBundle\Entity\CForumThread;
16
use Chamilo\CourseBundle\Repository\CForumAttachmentRepository;
17
use Chamilo\CourseBundle\Repository\CForumCategoryRepository;
18
use Chamilo\CourseBundle\Repository\CForumForumRepository;
19
use Chamilo\CourseBundle\Repository\CForumPostRepository;
20
use Chamilo\CourseBundle\Repository\CForumThreadRepository;
21
use Chamilo\CourseBundle\Repository\CGroupRepository;
22
use Chamilo\Kernel;
23
use Doctrine\DBAL\Connection;
24
use Doctrine\DBAL\Schema\Schema;
25
26
final class Version20201215160445 extends AbstractMigrationChamilo
27
{
28
    public function getDescription(): string
29
    {
30
        return 'Migrate c_forum tables';
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
        $forumCategoryRepo = $container->get(CForumCategoryRepository::class);
42
        $forumRepo = $container->get(CForumForumRepository::class);
43
        $forumAttachmentRepo = $container->get(CForumAttachmentRepository::class);
44
        $forumThreadRepo = $container->get(CForumThreadRepository::class);
45
        $forumPostRepo = $container->get(CForumPostRepository::class);
46
47
        $courseRepo = $container->get(CourseRepository::class);
48
        $sessionRepo = $container->get(SessionRepository::class);
49
        $groupRepo = $container->get(CGroupRepository::class);
50
        $userRepo = $container->get(UserRepository::class);
51
52
        /** @var Kernel $kernel */
53
        $kernel = $container->get('kernel');
54
        $rootPath = $kernel->getProjectDir();
55
56
        $admin = $this->getAdmin();
57
58
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
59
        /** @var Course $course */
60
        foreach ($q->toIterable() as $course) {
61
            $courseId = $course->getId();
62
            $course = $courseRepo->find($courseId);
63
64
            // Categories.
65
            $sql = "SELECT * FROM c_forum_category WHERE c_id = $courseId
66
                    ORDER BY iid";
67
            $result = $connection->executeQuery($sql);
68
            $items = $result->fetchAllAssociative();
69
            foreach ($items as $itemData) {
70
                $id = $itemData['iid'];
71
                /** @var CForumCategory $resource */
72
                $resource = $forumCategoryRepo->find($id);
73
                if ($resource->hasResourceNode()) {
74
                    continue;
75
                }
76
                $result = $this->fixItemProperty(
77
                    'forum_category',
78
                    $forumCategoryRepo,
79
                    $course,
80
                    $admin,
81
                    $resource,
82
                    $course
83
                );
84
85
                if (false === $result) {
86
                    continue;
87
                }
88
89
                $em->persist($resource);
90
                $em->flush();
91
            }
92
93
            $em->flush();
94
            $em->clear();
95
96
            // Forums.
97
            $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId
98
                    ORDER BY iid";
99
            $result = $connection->executeQuery($sql);
100
            $items = $result->fetchAllAssociative();
101
            foreach ($items as $itemData) {
102
                $id = $itemData['iid'];
103
                /** @var CForumForum $resource */
104
                $resource = $forumRepo->find($id);
105
                if ($resource->hasResourceNode()) {
106
                    continue;
107
                }
108
109
                $categoryId = $itemData['forum_category'];
110
                $parent = $forumCategoryRepo->find($categoryId);
111
                // Parent should not be null, because every forum must have a category, in this case use the course
112
                // as parent.
113
                if (null === $parent) {
114
                    $parent = $course;
115
                }
116
117
                $result = $this->fixItemProperty(
118
                    'forum',
119
                    $forumRepo,
120
                    $course,
121
                    $admin,
122
                    $resource,
123
                    $parent
124
                );
125
126
                $forumImage = $itemData['forum_image'];
127
                if (!empty($forumImage)) {
128
                    $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/forum/images/'.$forumImage;
129
                    $this->addLegacyFileToResource($filePath, $forumRepo, $resource, $id, $forumImage);
130
                }
131
132
                if (false === $result) {
133
                    continue;
134
                }
135
                $em->persist($resource);
136
                $em->flush();
137
            }
138
            $em->flush();
139
            $em->clear();
140
141
            // Threads.
142
            $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId
143
                    ORDER BY iid";
144
            $result = $connection->executeQuery($sql);
145
            $items = $result->fetchAllAssociative();
146
            foreach ($items as $itemData) {
147
                $id = $itemData['iid'];
148
                /** @var CForumThread $resource */
149
                $resource = $forumThreadRepo->find($id);
150
                if ($resource->hasResourceNode()) {
151
                    continue;
152
                }
153
154
                $forumId = $itemData['forum_id'];
155
                if (empty($forumId)) {
156
                    continue;
157
                }
158
159
                /** @var CForumForum $resource */
160
                $forum = $forumRepo->find($forumId);
161
162
                $result = $this->fixItemProperty(
163
                    'forum_thread',
164
                    $forumThreadRepo,
165
                    $course,
166
                    $admin,
167
                    $resource,
168
                    $forum
169
                );
170
171
                if (false === $result) {
172
                    continue;
173
                }
174
175
                $em->persist($resource);
176
                $em->flush();
177
            }
178
179
            $em->flush();
180
            $em->clear();
181
182
            // Posts.
183
            $sql = "SELECT * FROM c_forum_post WHERE c_id = $courseId
184
                    ORDER BY iid";
185
            $result = $connection->executeQuery($sql);
186
            $items = $result->fetchAllAssociative();
187
            foreach ($items as $itemData) {
188
                $id = $itemData['iid'];
189
                /** @var CForumPost $resource */
190
                $resource = $forumPostRepo->find($id);
191
                if ($resource->hasResourceNode()) {
192
                    continue;
193
                }
194
195
                $threadId = $itemData['thread_id'];
196
                if (empty($threadId)) {
197
                    continue;
198
                }
199
200
                /** @var CForumThread $resource */
201
                $thread = $forumThreadRepo->find($threadId);
202
203
                $result = $this->fixItemProperty(
204
                    'forum_thread',
205
                    $forumPostRepo,
206
                    $course,
207
                    $admin,
208
                    $resource,
209
                    $thread
210
                );
211
212
                if (false === $result) {
213
                    continue;
214
                }
215
216
                $em->persist($resource);
217
                $em->flush();
218
            }
219
220
            $em->flush();
221
            $em->clear();
222
223
            // Post attachments
224
            $sql = "SELECT * FROM c_forum_attachment WHERE c_id = $courseId
225
                    ORDER BY iid";
226
            $result = $connection->executeQuery($sql);
227
            $items = $result->fetchAllAssociative();
228
            foreach ($items as $itemData) {
229
                $id = $itemData['iid'];
230
                $postId = $itemData['post_id'];
231
                $path = $itemData['path'];
232
                $fileName = $itemData['filename'];
233
234
                /** @var CForumPost $resource */
235
                $post = $forumPostRepo->find($postId);
236
                if (null === $post) {
237
                    continue;
238
                }
239
240
                if (!empty($forumImage)) {
241
                    $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/forum/'.$path;
242
                    $this->addLegacyFileToResource($filePath, $forumPostRepo, $post, $id, $fileName);
243
                    $em->persist($resource);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $resource does not seem to be defined for all execution paths leading up to this point.
Loading history...
244
                    $em->flush();
245
                }
246
            }
247
            $em->flush();
248
            $em->clear();
249
        }
250
    }
251
}
252