Passed
Push — master ( b58c60...cd85be )
by Julito
08:44
created

Version20201212114910::up()   B

Complexity

Conditions 10
Paths 120

Size

Total Lines 71
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 47
c 1
b 0
f 0
nc 120
nop 1
dl 0
loc 71
rs 7.1563

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