Version20201215160445::up()   F
last analyzed

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 256
Code Lines 153

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 30
eloc 153
nc 37441
nop 1
dl 0
loc 256
rs 0
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\Course;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
12
use Chamilo\CourseBundle\Entity\CForum;
13
use Chamilo\CourseBundle\Entity\CForumCategory;
14
use Chamilo\CourseBundle\Entity\CForumPost;
15
use Chamilo\CourseBundle\Entity\CForumThread;
16
use Chamilo\CourseBundle\Repository\CForumCategoryRepository;
17
use Chamilo\CourseBundle\Repository\CForumPostRepository;
18
use Chamilo\CourseBundle\Repository\CForumRepository;
19
use Chamilo\CourseBundle\Repository\CForumThreadRepository;
20
use Chamilo\Kernel;
21
use Doctrine\DBAL\Schema\Schema;
22
23
final class Version20201215160445 extends AbstractMigrationChamilo
24
{
25
    public function getDescription(): string
26
    {
27
        return 'Migrate c_forum tables';
28
    }
29
30
    public function up(Schema $schema): void
31
    {
32
        $forumCategoryRepo = $this->container->get(CForumCategoryRepository::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
        $forumCategoryRepo = $this->container->get(CForumCategoryRepository::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
        $forumRepo = $this->container->get(CForumRepository::class);
34
        $forumThreadRepo = $this->container->get(CForumThreadRepository::class);
35
        $forumPostRepo = $this->container->get(CForumPostRepository::class);
36
        $courseRepo = $this->container->get(CourseRepository::class);
37
38
        /** @var Kernel $kernel */
39
        $kernel = $this->container->get('kernel');
40
        $rootPath = $kernel->getProjectDir();
41
42
        $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

42
        /** @scrutinizer ignore-call */ 
43
        $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...
43
44
        /** @var Course $course */
45
        foreach ($q->toIterable() as $course) {
46
            $courseId = $course->getId();
47
            $course = $courseRepo->find($courseId);
48
49
            $admin = $this->getAdmin();
50
51
            // Categories.
52
            $sql = "SELECT * FROM c_forum_category WHERE c_id = {$courseId}
53
                    ORDER BY iid";
54
            $result = $this->connection->executeQuery($sql);
55
            $items = $result->fetchAllAssociative();
56
            foreach ($items as $itemData) {
57
                $id = $itemData['iid'];
58
59
                /** @var CForumCategory $resource */
60
                $resource = $forumCategoryRepo->find($id);
61
                if ($resource->hasResourceNode()) {
62
                    continue;
63
                }
64
                $result = $this->fixItemProperty(
65
                    'forum_category',
66
                    $forumCategoryRepo,
67
                    $course,
68
                    $admin,
69
                    $resource,
70
                    $course
71
                );
72
73
                if (false === $result) {
74
                    continue;
75
                }
76
77
                $this->entityManager->persist($resource);
78
                $this->entityManager->flush();
79
            }
80
81
            $this->entityManager->flush();
82
            $this->entityManager->clear();
83
84
            // Forums.
85
            $sql = "SELECT * FROM c_forum_forum WHERE c_id = {$courseId}
86
                    ORDER BY iid";
87
            $result = $this->connection->executeQuery($sql);
88
            $items = $result->fetchAllAssociative();
89
90
            $admin = $this->getAdmin();
91
            foreach ($items as $itemData) {
92
                $id = $itemData['iid'];
93
94
                /** @var CForum $resource */
95
                $resource = $forumRepo->find($id);
96
                if ($resource->hasResourceNode()) {
97
                    continue;
98
                }
99
100
                $course = $courseRepo->find($courseId);
101
102
                $parent = null;
103
                $categoryId = $itemData['forum_category'];
104
                if (!empty($categoryId)) {
105
                    $parent = $forumCategoryRepo->find($categoryId);
106
                }
107
108
                // Parent should not be null, because every forum must have a category, in this case use the course
109
                // as parent.
110
                if (null === $parent) {
111
                    $parent = $course;
112
                }
113
114
                $result = $this->fixItemProperty(
115
                    'forum',
116
                    $forumRepo,
117
                    $course,
118
                    $admin,
119
                    $resource,
120
                    $parent
121
                );
122
123
                $this->entityManager->persist($resource);
124
                $this->entityManager->flush();
125
126
                $forumImage = $itemData['forum_image'];
127
                if (!empty($forumImage)) {
128
                    $filePath = $this->getUpdateRootPath().'/app/courses/'.$course->getDirectory().'/upload/forum/images/'.$forumImage;
129
                    error_log('MIGRATIONS :: $filePath -- '.$filePath.' ...');
130
                    if ($this->fileExists($filePath)) {
131
                        $this->addLegacyFileToResource($filePath, $forumRepo, $resource, $id, $forumImage);
132
                    }
133
                }
134
135
                if (false === $result) {
136
                    continue;
137
                }
138
                $this->entityManager->persist($resource);
139
                $this->entityManager->flush();
140
            }
141
            $this->entityManager->flush();
142
            $this->entityManager->clear();
143
144
            // Threads.
145
            $sql = "SELECT * FROM c_forum_thread WHERE c_id = {$courseId}
146
                    ORDER BY iid";
147
            $result = $this->connection->executeQuery($sql);
148
            $items = $result->fetchAllAssociative();
149
            $admin = $this->getAdmin();
150
151
            foreach ($items as $itemData) {
152
                $id = (int) $itemData['iid'];
153
154
                /** @var CForumThread $resource */
155
                $resource = $forumThreadRepo->find($id);
156
                if ($resource->hasResourceNode()) {
157
                    continue;
158
                }
159
160
                $forumId = (int) $itemData['forum_id'];
161
                if (empty($forumId)) {
162
                    continue;
163
                }
164
165
                /** @var CForum|null $forum */
166
                $forum = $forumRepo->find($forumId);
167
                if (null === $forum) {
168
                    continue;
169
                }
170
171
                $course = $courseRepo->find($courseId);
172
173
                $result = $this->fixItemProperty(
174
                    'forum_thread',
175
                    $forumThreadRepo,
176
                    $course,
177
                    $admin,
178
                    $resource,
179
                    $forum
180
                );
181
182
                if (false === $result) {
183
                    continue;
184
                }
185
186
                $this->entityManager->persist($resource);
187
                $this->entityManager->flush();
188
            }
189
190
            $this->entityManager->flush();
191
            $this->entityManager->clear();
192
193
            // Posts.
194
            $sql = "SELECT * FROM c_forum_post WHERE c_id = {$courseId}
195
                    ORDER BY iid";
196
            $result = $this->connection->executeQuery($sql);
197
            $items = $result->fetchAllAssociative();
198
            $admin = $this->getAdmin();
199
            foreach ($items as $itemData) {
200
                $id = (int) $itemData['iid'];
201
202
                /** @var CForumPost $resource */
203
                $resource = $forumPostRepo->find($id);
204
205
                if ($resource->hasResourceNode()) {
206
                    continue;
207
                }
208
209
                if (empty(trim($resource->getTitle()))) {
210
                    $resource->setTitle(\sprintf('Post #%s', $resource->getIid()));
211
                }
212
213
                $threadId = (int) $itemData['thread_id'];
214
215
                if (empty($threadId)) {
216
                    continue;
217
                }
218
219
                /** @var CForumThread|null $thread */
220
                $thread = $forumThreadRepo->find($threadId);
221
222
                if (null === $thread) {
223
                    continue;
224
                }
225
226
                $forum = $thread->getForum();
227
228
                // For some reason the thread doesn't have a forum, so we ignore the thread posts.
229
                if (null === $forum) {
230
                    continue;
231
                }
232
233
                $course = $courseRepo->find($courseId);
234
235
                $result = $this->fixItemProperty(
236
                    'forum_post',
237
                    $forumPostRepo,
238
                    $course,
239
                    $admin,
240
                    $resource,
241
                    $thread
242
                );
243
244
                if (false === $result) {
245
                    continue;
246
                }
247
248
                $this->entityManager->persist($resource);
249
                $this->entityManager->flush();
250
            }
251
252
            $this->entityManager->flush();
253
            $this->entityManager->clear();
254
255
            // Post attachments
256
            $sql = "SELECT * FROM c_forum_attachment WHERE c_id = {$courseId}
257
                    ORDER BY iid";
258
            $result = $this->connection->executeQuery($sql);
259
            $items = $result->fetchAllAssociative();
260
261
            foreach ($items as $itemData) {
262
                $id = $itemData['iid'];
263
                $postId = (int) $itemData['post_id'];
264
                $path = $itemData['path'];
265
                $fileName = $itemData['filename'];
266
267
                /** @var CForumPost|null $post */
268
                $post = $forumPostRepo->find($postId);
269
270
                if (null === $post || !$post->hasResourceNode()) {
271
                    continue;
272
                }
273
274
                if (!empty($fileName) && !empty($path)) {
275
                    $filePath = $this->getUpdateRootPath().'/app/courses/'.$course->getDirectory().'/upload/forum/'.$path;
276
                    error_log('MIGRATIONS :: $filePath -- '.$filePath.' ...');
277
                    if ($this->fileExists($filePath)) {
278
                        $this->addLegacyFileToResource($filePath, $forumPostRepo, $post, $id, $fileName);
279
                        $this->entityManager->persist($post);
280
                        $this->entityManager->flush();
281
                    }
282
                }
283
            }
284
            $this->entityManager->flush();
285
            $this->entityManager->clear();
286
        }
287
    }
288
}
289