Version20201212114910::up()   F
last analyzed

Complexity

Conditions 13
Paths 816

Size

Total Lines 96
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 60
nc 816
nop 1
dl 0
loc 96
rs 2.7054
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\AccessUrl;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
13
use Chamilo\CoreBundle\Repository\Node\UserRepository;
14
use Doctrine\DBAL\Schema\Schema;
15
use Symfony\Component\Uid\Uuid;
16
17
final class Version20201212114910 extends AbstractMigrationChamilo
18
{
19
    public function getDescription(): string
20
    {
21
        return 'Migrate access_url, users';
22
    }
23
24
    public function up(Schema $schema): void
25
    {
26
        $urlRepo = $this->container->get(AccessUrlRepository::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

26
        /** @scrutinizer ignore-call */ 
27
        $urlRepo = $this->container->get(AccessUrlRepository::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...
27
        $userRepo = $this->container->get(UserRepository::class);
28
29
        $userList = [];
30
        // Adding first admin as main creator also adding to the resource node tree.
31
        $admin = $this->getAdmin();
32
        $admin->addRole('ROLE_ADMIN');
33
34
        $adminId = $admin->getId();
35
        $userList[$adminId] = $admin;
36
37
        $this->write('Adding admin user');
38
        if (false === $admin->hasResourceNode()) {
39
            $resourceNode = $userRepo->addUserToResourceNode($adminId, $adminId);
40
            $this->entityManager->persist($resourceNode);
0 ignored issues
show
Bug introduced by
The method persist() 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

40
            $this->entityManager->/** @scrutinizer ignore-call */ 
41
                                  persist($resourceNode);

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...
41
        }
42
43
        // Adding portals (AccessUrl) to the resource node tree.
44
        $urls = $urlRepo->findAll();
45
46
        /** @var AccessUrl $url */
47
        foreach ($urls as $url) {
48
            if (false === $url->hasResourceNode()) {
49
                $urlRepo->createNodeForResourceWithNoParent($url, $admin);
50
                $this->entityManager->persist($url);
51
            }
52
        }
53
        $this->entityManager->flush();
54
55
        $sql = 'SELECT DISTINCT(user_id) FROM admin';
56
        $result = $this->entityManager->getConnection()->executeQuery($sql);
57
        $results = $result->fetchAllAssociative();
58
        $adminList = [];
59
        if (!empty($results)) {
60
            $adminList = array_map('intval', array_column($results, 'user_id'));
61
        }
62
63
        // Adding users to the resource node tree.
64
        $batchSize = self::BATCH_SIZE;
65
        $counter = 1;
66
        $q = $this->entityManager->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u');
67
68
        $this->write('Migrating users');
69
70
        /** @var User $userEntity */
71
        foreach ($q->toIterable() as $userEntity) {
72
            if ($userEntity->hasResourceNode()) {
73
                continue;
74
            }
75
76
            $userId = $userEntity->getId();
77
            $this->write("Migrating user: #$userId");
78
79
            $userEntity
80
                ->setUuid(Uuid::v4())
81
                ->setRoles([])
82
                ->setRoleFromStatus($userEntity->getStatus())
83
            ;
84
85
            if (\in_array($userId, $adminList, true)) {
86
                $userEntity->addRole('ROLE_ADMIN');
87
            }
88
89
            if ($userEntity::ANONYMOUS === $userEntity->getStatus()) {
90
                $userEntity->addRole('ROLE_ANONYMOUS');
91
            }
92
93
            $creatorId = $userEntity->getCreatorId();
94
            $creator = null;
95
            if (isset($userList[$adminId])) {
96
                $creator = $userList[$adminId];
97
            } else {
98
                $creator = $userRepo->find($creatorId);
99
                $userList[$adminId] = $creator;
100
            }
101
            if (null === $creator) {
102
                $creator = $admin;
103
            }
104
105
            $resourceNode = $userRepo->addUserToResourceNode($userId, $creator->getId());
106
            $this->entityManager->persist($resourceNode);
107
108
            if (($counter % $batchSize) === 0) {
109
                $this->entityManager->flush();
110
                $this->entityManager->clear(); // Detaches all objects from Doctrine!
111
            }
112
            $counter++;
113
        }
114
        $this->entityManager->flush();
115
        $this->entityManager->clear();
116
117
        $table = $schema->getTable('user');
118
        if (false === $table->hasIndex('UNIQ_8D93D649D17F50A6')) {
119
            $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649D17F50A6 ON user (uuid);');
120
        }
121
    }
122
}
123