| Conditions | 13 |
| Paths | 100 |
| Total Lines | 75 |
| Code Lines | 49 |
| 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 | $container = $this->getContainer(); |
||
| 24 | $doctrine = $container->get('doctrine'); |
||
| 25 | $em = $doctrine->getManager(); |
||
| 26 | /** @var Connection $connection */ |
||
| 27 | $connection = $em->getConnection(); |
||
| 28 | |||
| 29 | $kernel = $container->get('kernel'); |
||
| 30 | $rootPath = $kernel->getProjectDir(); |
||
| 31 | |||
| 32 | $userRepo = $container->get(UserRepository::class); |
||
| 33 | $illustrationRepo = $container->get(IllustrationRepository::class); |
||
| 34 | |||
| 35 | // Adding users to the resource node tree. |
||
| 36 | $batchSize = self::BATCH_SIZE; |
||
| 37 | $counter = 1; |
||
| 38 | $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u'); |
||
| 39 | |||
| 40 | $sql = "SELECT * FROM settings_current WHERE variable = 'split_users_upload_directory' AND access_url = 1"; |
||
| 41 | $result = $connection->executeQuery($sql); |
||
| 42 | $setting = $result->fetchAssociative(); |
||
| 43 | |||
| 44 | /** @var User $userEntity */ |
||
| 45 | foreach ($q->toIterable() as $userEntity) { |
||
| 46 | if ($userEntity->hasResourceNode()) { |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | $id = $userEntity->getId(); |
||
| 50 | $picture = $userEntity->getPictureUri(); |
||
| 51 | $path = "users/$id/"; |
||
| 52 | if (!empty($setting) && 'true' === $setting['selected_value']) { |
||
| 53 | $path = 'users/'.substr((string) $id, 0, 1).'/'.$id.'/'; |
||
| 54 | } |
||
| 55 | $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture; |
||
| 56 | if (file_exists($picturePath)) { |
||
| 57 | $file = new UploadedFile($picturePath, $picture, null, null, true); |
||
| 58 | $illustrationRepo->addIllustration($userEntity, $userEntity, $file); |
||
| 59 | } |
||
| 60 | |||
| 61 | if (0 === $counter % $batchSize) { |
||
| 62 | $em->flush(); |
||
| 63 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 64 | } |
||
| 65 | $counter++; |
||
| 66 | } |
||
| 67 | |||
| 68 | // Migrate Usergroup images. |
||
| 69 | $counter = 1; |
||
| 70 | $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u'); |
||
| 71 | |||
| 72 | $admin = $this->getAdmin(); |
||
| 73 | |||
| 74 | /** @var Usergroup $userGroup */ |
||
| 75 | foreach ($q->toIterable() as $userGroup) { |
||
| 76 | if ($userGroup->hasResourceNode()) { |
||
| 77 | continue; |
||
| 78 | } |
||
| 79 | $id = $userGroup->getId(); |
||
| 80 | $picture = $userGroup->getPicture(); |
||
| 81 | $path = "groups/$id/"; |
||
| 82 | if (!empty($setting) && 'true' === $setting['selected_value']) { |
||
| 83 | $path = 'groups/'.substr((string) $id, 0, 1).'/'.$id.'/'; |
||
| 84 | } |
||
| 85 | $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture; |
||
| 86 | if (file_exists($picturePath)) { |
||
| 87 | $file = new UploadedFile($picturePath, $picture, null, null, true); |
||
| 88 | $illustrationRepo->addIllustration($userGroup, $admin, $file); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (0 === $counter % $batchSize) { |
||
| 92 | $em->flush(); |
||
| 93 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 94 | } |
||
| 95 | $counter++; |
||
| 96 | } |
||
| 99 |