Conditions | 18 |
Paths | 484 |
Total Lines | 117 |
Code Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
26 | public function up(Schema $schema): void |
||
27 | { |
||
28 | $container = $this->getContainer(); |
||
29 | $em = $this->getEntityManager(); |
||
30 | /** @var Connection $connection */ |
||
31 | $connection = $em->getConnection(); |
||
32 | |||
33 | $kernel = $container->get('kernel'); |
||
34 | $rootPath = $kernel->getProjectDir(); |
||
35 | |||
36 | $illustrationRepo = $container->get(IllustrationRepository::class); |
||
37 | |||
38 | // Adding users to the resource node tree. |
||
39 | $batchSize = self::BATCH_SIZE; |
||
40 | $counter = 1; |
||
41 | $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u'); |
||
42 | |||
43 | $sql = "SELECT * FROM settings_current WHERE variable = 'split_users_upload_directory' AND access_url = 1"; |
||
44 | $result = $connection->executeQuery($sql); |
||
45 | $setting = $result->fetchAssociative(); |
||
46 | |||
47 | /** @var User $userEntity */ |
||
48 | foreach ($q->toIterable() as $userEntity) { |
||
49 | if ($userEntity->hasResourceNode()) { |
||
50 | continue; |
||
51 | } |
||
52 | $id = $userEntity->getId(); |
||
53 | $picture = $userEntity->getPictureUri(); |
||
54 | if (empty($picture)) { |
||
55 | continue; |
||
56 | } |
||
57 | $path = "users/{$id}/"; |
||
58 | if (!empty($setting) && 'true' === $setting['selected_value']) { |
||
59 | $path = 'users/'.substr((string) $id, 0, 1).'/'.$id.'/'; |
||
60 | } |
||
61 | $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture; |
||
62 | if ($this->fileExists($picturePath)) { |
||
63 | $mimeType = mime_content_type($picturePath); |
||
64 | $file = new UploadedFile($picturePath, $picture, $mimeType, null, true); |
||
65 | $illustrationRepo->addIllustration($userEntity, $userEntity, $file); |
||
66 | } |
||
67 | |||
68 | if (($counter % $batchSize) === 0) { |
||
69 | $em->flush(); |
||
70 | $em->clear(); // Detaches all objects from Doctrine! |
||
71 | } |
||
72 | $counter++; |
||
73 | } |
||
74 | |||
75 | $em->flush(); |
||
76 | $em->clear(); |
||
77 | |||
78 | // Migrate Usergroup. |
||
79 | $counter = 1; |
||
80 | $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u'); |
||
81 | $admin = $this->getAdmin(); |
||
82 | |||
83 | $userGroupRepo = $container->get(UsergroupRepository::class); |
||
84 | $urlRepo = $container->get(AccessUrlRepository::class); |
||
85 | $urlList = $urlRepo->findAll(); |
||
86 | /** @var AccessUrl $url */ |
||
87 | $url = $urlList[0]; |
||
88 | |||
89 | /** @var Usergroup $userGroup */ |
||
90 | foreach ($q->toIterable() as $userGroup) { |
||
91 | if ($userGroup->hasResourceNode()) { |
||
92 | continue; |
||
93 | } |
||
94 | |||
95 | $userGroup->setCreator($admin); |
||
96 | |||
97 | if (0 === $userGroup->getUrls()->count()) { |
||
98 | $accessUrlRelUserGroup = (new AccessUrlRelUserGroup()) |
||
99 | ->setUserGroup($userGroup) |
||
100 | ->setUrl($url) |
||
101 | ; |
||
102 | $userGroup->getUrls()->add($accessUrlRelUserGroup); |
||
103 | } |
||
104 | $userGroupRepo->addResourceNode($userGroup, $admin, $url); |
||
105 | $em->persist($userGroup); |
||
106 | $em->flush(); |
||
107 | } |
||
108 | $em->clear(); |
||
109 | |||
110 | // Migrate Usergroup images. |
||
111 | $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u'); |
||
112 | /** @var Usergroup $userGroup */ |
||
113 | foreach ($q->toIterable() as $userGroup) { |
||
114 | if (!$userGroup->hasResourceNode()) { |
||
115 | continue; |
||
116 | } |
||
117 | |||
118 | $picture = $userGroup->getPicture(); |
||
119 | if (empty($picture)) { |
||
120 | continue; |
||
121 | } |
||
122 | $id = $userGroup->getId(); |
||
123 | $path = "groups/{$id}/"; |
||
124 | if (!empty($setting) && 'true' === $setting['selected_value']) { |
||
125 | $path = 'groups/'.substr((string) $id, 0, 1).'/'.$id.'/'; |
||
126 | } |
||
127 | $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture; |
||
128 | if ($this->fileExists($picturePath)) { |
||
129 | $mimeType = mime_content_type($picturePath); |
||
130 | $file = new UploadedFile($picturePath, $picture, $mimeType, null, true); |
||
131 | $illustrationRepo->addIllustration($userGroup, $admin, $file); |
||
132 | } |
||
133 | |||
134 | if (($counter % $batchSize) === 0) { |
||
135 | $em->flush(); |
||
136 | $em->clear(); // Detaches all objects from Doctrine! |
||
137 | } |
||
138 | $counter++; |
||
139 | } |
||
140 | |||
141 | $em->flush(); |
||
142 | $em->clear(); |
||
143 | } |
||
145 |