Passed
Push — master ( db12d9...b58c60 )
by Julito
12:19
created

Version20201212203625::up()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 101
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 59
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 101
rs 7.6501

How to fix   Long Method   

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
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Entity\AccessUrl;
8
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse;
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
11
use Chamilo\CoreBundle\Repository\Node\UserRepository;
12
use Chamilo\CoreBundle\Repository\SessionRepository;
13
use Chamilo\CourseBundle\Entity\CDocument;
14
use Chamilo\CourseBundle\Repository\CDocumentRepository;
15
use Chamilo\CourseBundle\Repository\CGroupRepository;
16
use Chamilo\Kernel;
17
use Doctrine\DBAL\Connection;
18
use Doctrine\DBAL\Schema\Schema;
19
20
/**
21
 * Auto-generated Migration: Please modify to your needs!
22
 */
23
final class Version20201212203625 extends AbstractMigrationChamilo
24
{
25
    public function getDescription(): string
26
    {
27
        return 'Migrate documents';
28
    }
29
30
    public function up(Schema $schema): void
31
    {
32
        $container = $this->getContainer();
33
        $doctrine = $container->get('doctrine');
34
        $em = $doctrine->getManager();
35
        /** @var Connection $connection */
36
        $connection = $em->getConnection();
37
38
        $documentRepo = $container->get(CDocumentRepository::class);
39
        $courseRepo = $container->get(CourseRepository::class);
40
        $sessionRepo = $container->get(SessionRepository::class);
41
        $groupRepo = $container->get(CGroupRepository::class);
42
        $userRepo = $container->get(UserRepository::class);
43
44
        /** @var Kernel $kernel */
45
        $kernel = $container->get('kernel');
46
        $rootPath = $kernel->getProjectDir();
47
48
        $userList = [];
49
        $groupList = [];
50
        $sessionList = [];
51
        $batchSize = self::BATCH_SIZE;
52
53
        $admin = $this->getAdmin();
54
        $urlRepo = $em->getRepository(AccessUrl::class);
55
        $urls = $urlRepo->findAll();
56
57
        /** @var AccessUrl $url */
58
        foreach ($urls as $url) {
59
            $accessUrlRelCourses = $url->getCourses();
60
            /** @var AccessUrlRelCourse $accessUrlRelCourse */
61
            foreach ($accessUrlRelCourses as $accessUrlRelCourse) {
62
                $counter = 1;
63
                $course = $accessUrlRelCourse->getCourse();
64
                $courseId = $course->getId();
65
                $courseCode = $course->getCode();
66
                $course = $courseRepo->find($courseId);
67
68
                $sql = "SELECT * FROM c_document WHERE c_id = $courseId
69
                        ORDER BY filetype DESC";
70
                $result = $connection->executeQuery($sql);
71
                $documents = $result->fetchAllAssociative();
72
                foreach ($documents as $documentData) {
73
                    $documentId = $documentData['iid'];
74
                    $documentPath = $documentData['path'];
75
76
                    /** @var CDocument $document */
77
                    $document = $documentRepo->find($documentId);
78
                    if ($document->hasResourceNode()) {
79
                        continue;
80
                    }
81
82
                    $sql = "SELECT * FROM c_item_property
83
                            WHERE tool = 'document' AND c_id = $courseId AND ref = $documentId";
84
                    $result = $connection->executeQuery($sql);
85
                    $items = $result->fetchAllAssociative();
86
87
                    // For some reason this document doesnt have a c_item_property value.
88
                    if (empty($items)) {
89
                        continue;
90
                    }
91
92
                    $createNode = false;
93
                    $resourceNode = null;
94
                    $parent = null;
95
                    if ('.' !== dirname($documentPath)) {
96
                        $parentId = \DocumentManager::get_document_id(
97
                            ['real_id' => $courseId],
98
                            dirname($documentPath)
99
                        );
100
                        $parent = $documentRepo->find($parentId);
101
                    }
102
103
                    if (null === $parent) {
104
                        $parent = $course;
105
                    }
106
107
                    $this->fixItemProperty($documentRepo, $course, $admin, $document, $parent, $items);
108
                    $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/document/'.$documentPath;
109
                    $this->addLegacyFileToResource($filePath, $documentRepo, $document, $documentId);
110
111
                    $em->persist($document);
112
                    $em->flush();
113
                    //$em->clear();
114
                    /*if ($createNode) {
115
                        $em->persist($document);
116
                        if (0 === $counter % $batchSize) {
117
                            $em->flush();
118
                            $em->clear();
119
                        }
120
121
                        $counter++;
122
                    } else {
123
                        $em->clear();
124
                    }*/
125
                }
126
                $em->clear();
127
            }
128
129
            $em->flush();
130
            $em->clear();
131
        }
132
    }
133
134
    public function down(Schema $schema): void
135
    {
136
        // this down() migration is auto-generated, please modify it to your needs
137
    }
138
}
139