| Conditions | 10 |
| Paths | 120 |
| Total Lines | 71 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 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 | } |
||
| 97 |