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

Version20201212114910::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
4
5
use Chamilo\CoreBundle\Entity\AccessUrl;
6
use Chamilo\CoreBundle\Entity\User;
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
9
use Chamilo\CoreBundle\Repository\Node\UserRepository;
10
use Doctrine\DBAL\Connection;
11
use Doctrine\DBAL\Schema\Schema;
12
use Symfony\Component\Uid\Uuid;
13
14
final class Version20201212114910 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Migrate access_url, users';
19
    }
20
21
    public function up(Schema $schema): void
22
    {
23
        $table = $schema->getTable('user');
24
        if (false === $table->hasColumn('uuid')) {
25
            $this->addSql("ALTER TABLE user ADD uuid BINARY(16) NOT NULL COMMENT '(DC2Type:uuid)'");
26
        }
27
28
        $container = $this->getContainer();
29
        $doctrine = $container->get('doctrine');
30
        $em = $doctrine->getManager();
31
        /** @var Connection $connection */
32
        $connection = $em->getConnection();
33
34
        $urlRepo = $container->get(AccessUrlRepository::class);
35
        $userRepo = $container->get(UserRepository::class);
36
37
        $userList = [];
38
        // Adding first admin as main creator also adding to the resource node tree.
39
        $admin = $this->getAdmin();
40
41
        $this->abortIf(null === $admin, 'Admin not found in the system');
42
43
        $adminId = $admin->getId();
44
        $userList[$adminId] = $admin;
45
        if (false === $admin->hasResourceNode()) {
46
            $resourceNode = $userRepo->addUserToResourceNode($adminId, $adminId);
47
            $em->persist($resourceNode);
48
        }
49
50
        // Adding portals (AccessUrl) to the resource node tree.
51
        $urls = $urlRepo->findAll();
52
        /** @var AccessUrl $url */
53
        foreach ($urls as $url) {
54
            if (false === $url->hasResourceNode()) {
55
                $urlRepo->addResourceNode($url, $admin);
56
                $em->persist($url);
57
            }
58
        }
59
        $em->flush();
60
61
        // Adding users to the resource node tree.
62
        $sql = 'SELECT * FROM user';
63
        $result = $connection->executeQuery($sql);
64
        $users = $result->fetchAllAssociative();
65
        $batchSize = self::BATCH_SIZE;
66
        $counter = 1;
67
        foreach ($users as $user) {
68
            /** @var User $userEntity */
69
            $userEntity = $userRepo->find($user['id']);
70
            if ($userEntity->hasResourceNode()) {
71
                continue;
72
            }
73
            $userEntity->setUuid(Uuid::v4());
74
            $creatorId = $user['creator_id'];
75
            $creator = null;
76
            if (isset($userList[$adminId])) {
77
                $creator = $userList[$adminId];
78
            } else {
79
                $creator = $userRepo->find($creatorId);
80
                $userList[$adminId] = $creator;
81
            }
82
            if (null === $creator) {
83
                $creator = $admin;
84
            }
85
86
            $resourceNode = $userRepo->addUserToResourceNode($adminId, $creator->getId());
87
            $em->persist($resourceNode);
88
            if (0 === $counter % $batchSize) {
89
                $em->flush();
90
                $em->clear(); // Detaches all objects from Doctrine!
91
            }
92
            $counter++;
93
        }
94
        $em->flush();
95
        $em->clear();
96
97
        if (false === $table->hasIndex('UNIQ_8D93D649D17F50A6')) {
98
            $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649D17F50A6 ON user (uuid);');
99
        }
100
    }
101
}
102