| Conditions | 11 |
| Paths | 240 |
| Total Lines | 78 |
| Code Lines | 52 |
| 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 |
||
| 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 | } |
||
| 102 |