Conditions | 12 |
Paths | 432 |
Total Lines | 93 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
24 | public function up(Schema $schema): void |
||
25 | { |
||
26 | $container = $this->getContainer(); |
||
27 | $doctrine = $container->get('doctrine'); |
||
28 | $em = $doctrine->getManager(); |
||
29 | |||
30 | $urlRepo = $container->get(AccessUrlRepository::class); |
||
31 | $userRepo = $container->get(UserRepository::class); |
||
32 | |||
33 | $userList = []; |
||
34 | // Adding first admin as main creator also adding to the resource node tree. |
||
35 | $admin = $this->getAdmin(); |
||
36 | |||
37 | $adminId = $admin->getId(); |
||
38 | $userList[$adminId] = $admin; |
||
39 | |||
40 | $this->write('Adding admin user'); |
||
41 | if (false === $admin->hasResourceNode()) { |
||
42 | $resourceNode = $userRepo->addUserToResourceNode($adminId, $adminId); |
||
43 | $em->persist($resourceNode); |
||
44 | } |
||
45 | |||
46 | // Adding portals (AccessUrl) to the resource node tree. |
||
47 | $urls = $urlRepo->findAll(); |
||
48 | /** @var AccessUrl $url */ |
||
49 | foreach ($urls as $url) { |
||
50 | if (false === $url->hasResourceNode()) { |
||
51 | $urlRepo->createNodeForResourceWithNoParent($url, $admin); |
||
52 | $em->persist($url); |
||
53 | } |
||
54 | } |
||
55 | $em->flush(); |
||
56 | |||
57 | $sql = 'SELECT DISTINCT(user_id) FROM admin'; |
||
58 | $result = $em->getConnection()->executeQuery($sql); |
||
59 | $results = $result->fetchAllAssociative(); |
||
60 | $adminList = []; |
||
61 | if (!empty($results)) { |
||
62 | $adminList = array_map('intval', array_column($results, 'user_id')); |
||
63 | } |
||
64 | |||
65 | // Adding users to the resource node tree. |
||
66 | $batchSize = self::BATCH_SIZE; |
||
67 | $counter = 1; |
||
68 | $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u'); |
||
69 | |||
70 | $this->write('Migrating users'); |
||
71 | /** @var User $userEntity */ |
||
72 | foreach ($q->toIterable() as $userEntity) { |
||
73 | if ($userEntity->hasResourceNode()) { |
||
74 | continue; |
||
75 | } |
||
76 | |||
77 | $userId = $userEntity->getId(); |
||
78 | $this->write("Migrating user: #$userId"); |
||
79 | |||
80 | $userEntity |
||
81 | ->setUuid(Uuid::v4()) |
||
82 | ->setRoles([]) |
||
83 | ->setRoleFromStatus($userEntity->getStatus()) |
||
84 | ; |
||
85 | |||
86 | if (\in_array($userId, $adminList, true)) { |
||
87 | $userEntity->addRole('ROLE_ADMIN'); |
||
88 | } |
||
89 | |||
90 | $creatorId = $userEntity->getCreatorId(); |
||
91 | $creator = null; |
||
92 | if (isset($userList[$adminId])) { |
||
93 | $creator = $userList[$adminId]; |
||
94 | } else { |
||
95 | $creator = $userRepo->find($creatorId); |
||
96 | $userList[$adminId] = $creator; |
||
97 | } |
||
98 | if (null === $creator) { |
||
99 | $creator = $admin; |
||
100 | } |
||
101 | |||
102 | $resourceNode = $userRepo->addUserToResourceNode($userId, $creator->getId()); |
||
103 | $em->persist($resourceNode); |
||
104 | |||
105 | if (($counter % $batchSize) === 0) { |
||
106 | $em->flush(); |
||
107 | $em->clear(); // Detaches all objects from Doctrine! |
||
108 | } |
||
109 | $counter++; |
||
110 | } |
||
111 | $em->flush(); |
||
112 | $em->clear(); |
||
113 | |||
114 | $table = $schema->getTable('user'); |
||
115 | if (false === $table->hasIndex('UNIQ_8D93D649D17F50A6')) { |
||
116 | $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649D17F50A6 ON user (uuid);'); |
||
117 | } |
||
120 |