Total Complexity | 47 |
Total Lines | 334 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like AbstractMigrationChamilo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractMigrationChamilo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | abstract class AbstractMigrationChamilo extends AbstractMigration |
||
32 | { |
||
33 | public const BATCH_SIZE = 20; |
||
34 | |||
35 | protected ?EntityManagerInterface $entityManager = null; |
||
36 | protected ?ContainerInterface $container = null; |
||
37 | |||
38 | public function setEntityManager(EntityManagerInterface $entityManager): void |
||
41 | } |
||
42 | |||
43 | public function setContainer(?ContainerInterface $container = null): void |
||
46 | } |
||
47 | |||
48 | public function adminExist(): bool |
||
49 | { |
||
50 | $sql = 'SELECT user_id FROM admin WHERE user_id IN (SELECT id FROM user) ORDER BY id LIMIT 1'; |
||
51 | $result = $this->connection->executeQuery($sql); |
||
52 | $adminRow = $result->fetchAssociative(); |
||
53 | |||
54 | if (empty($adminRow)) { |
||
55 | return false; |
||
56 | } |
||
57 | |||
58 | return true; |
||
59 | } |
||
60 | |||
61 | public function getAdmin(): User |
||
62 | { |
||
63 | $admin = $this->entityManager |
||
64 | ->getRepository(Admin::class) |
||
|
|||
65 | ->findOneBy([], ['id' => 'ASC']) |
||
66 | ; |
||
67 | |||
68 | return $admin->getUser(); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Speeds up SettingsCurrent creation. |
||
73 | * |
||
74 | * @param string $variable The variable itself |
||
75 | * @param string $subKey The subkey |
||
76 | * @param string $type The type of setting (text, radio, select, etc) |
||
77 | * @param string $category The category (Platform, User, etc) |
||
78 | * @param string $selectedValue The default value |
||
79 | * @param string $title The setting title string name |
||
80 | * @param string $comment The setting comment string name |
||
81 | * @param string $scope The scope |
||
82 | * @param string $subKeyText Text if there is a subKey |
||
83 | * @param int $accessUrl What URL it is for |
||
84 | * @param bool $accessUrlChangeable Whether it can be changed on each url |
||
85 | * @param bool $accessUrlLocked Whether the setting for the current URL is |
||
86 | * locked to the current value |
||
87 | * @param array $options Optional array in case of a radio-type field, |
||
88 | * to insert options |
||
89 | */ |
||
90 | public function addSettingCurrent( |
||
91 | $variable, |
||
92 | $subKey, |
||
93 | $type, |
||
94 | $category, |
||
95 | $selectedValue, |
||
96 | $title, |
||
97 | $comment, |
||
98 | $scope = '', |
||
99 | $subKeyText = '', |
||
100 | $accessUrl = 1, |
||
101 | $accessUrlChangeable = false, |
||
102 | $accessUrlLocked = true, |
||
103 | $options = [] |
||
104 | ): void { |
||
105 | $accessUrl = $this->entityManager->find(AccessUrl::class, $accessUrl); |
||
106 | |||
107 | $setting = new SettingsCurrent(); |
||
108 | $setting |
||
109 | ->setVariable($variable) |
||
110 | ->setSubkey($subKey) |
||
111 | ->setType($type) |
||
112 | ->setCategory($category) |
||
113 | ->setSelectedValue($selectedValue) |
||
114 | ->setTitle($title) |
||
115 | ->setComment($comment) |
||
116 | ->setScope($scope) |
||
117 | ->setSubkeytext($subKeyText) |
||
118 | ->setUrl($accessUrl) |
||
119 | ->setAccessUrlChangeable((int) $accessUrlChangeable) |
||
120 | ->setAccessUrlLocked((int) $accessUrlLocked) |
||
121 | ; |
||
122 | |||
123 | $this->entityManager->persist($setting); |
||
124 | |||
125 | if (\count($options) > 0) { |
||
126 | foreach ($options as $option) { |
||
127 | if (empty($option['text'])) { |
||
128 | if ('true' === $option['value']) { |
||
129 | $option['text'] = 'Yes'; |
||
130 | } else { |
||
131 | $option['text'] = 'No'; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | $settingOption = new SettingsOptions(); |
||
136 | $settingOption |
||
137 | ->setVariable($variable) |
||
138 | ->setValue($option['value']) |
||
139 | ->setDisplayText($option['text']) |
||
140 | ; |
||
141 | |||
142 | $this->entityManager->persist($settingOption); |
||
143 | } |
||
144 | } |
||
145 | $this->entityManager->flush(); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param string $variable |
||
150 | * @param null|mixed $configuration |
||
151 | */ |
||
152 | public function getConfigurationValue($variable, $configuration = null) |
||
153 | { |
||
154 | global $_configuration; |
||
155 | |||
156 | if (isset($configuration)) { |
||
157 | $_configuration = $configuration; |
||
158 | } |
||
159 | |||
160 | if (isset($_configuration[$variable])) { |
||
161 | return $_configuration[$variable]; |
||
162 | } |
||
163 | |||
164 | return false; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Remove a setting completely. |
||
169 | * |
||
170 | * @param string $variable The setting variable name |
||
171 | */ |
||
172 | public function removeSettingCurrent($variable): void |
||
173 | { |
||
174 | // to be implemented |
||
175 | } |
||
176 | |||
177 | public function addLegacyFileToResource( |
||
202 | } |
||
203 | |||
204 | public function fixItemProperty( |
||
205 | $tool, |
||
206 | ResourceRepository $repo, |
||
207 | $course, |
||
208 | $admin, |
||
209 | ResourceInterface $resource, |
||
210 | $parentResource, |
||
211 | array $items = [] |
||
212 | ) { |
||
213 | $courseId = $course->getId(); |
||
214 | $id = $resource->getResourceIdentifier(); |
||
215 | |||
216 | if (empty($items)) { |
||
217 | $sql = "SELECT * FROM c_item_property |
||
218 | WHERE tool = '{$tool}' AND c_id = {$courseId} AND ref = {$id}"; |
||
219 | $result = $this->connection->executeQuery($sql); |
||
220 | $items = $result->fetchAllAssociative(); |
||
221 | } |
||
222 | |||
223 | // For some reason the resource doesn't have a c_item_property value. |
||
224 | if (empty($items)) { |
||
225 | return false; |
||
226 | } |
||
227 | |||
228 | $sessionRepo = $this->container->get(SessionRepository::class); |
||
229 | $groupRepo = $this->container->get(CGroupRepository::class); |
||
230 | $userRepo = $this->container->get(UserRepository::class); |
||
231 | |||
232 | $resource->setParent($parentResource); |
||
233 | $resourceNode = null; |
||
234 | $userList = []; |
||
235 | $groupList = []; |
||
236 | $sessionList = []; |
||
237 | foreach ($items as $item) { |
||
238 | $visibility = (int) $item['visibility']; |
||
239 | $userId = (int) $item['insert_user_id']; |
||
240 | $sessionId = $item['session_id'] ?? 0; |
||
241 | $groupId = $item['to_group_id'] ?? 0; |
||
242 | if (empty($item['lastedit_date'])) { |
||
243 | $lastUpdatedAt = new DateTime('now', new DateTimeZone('UTC')); |
||
244 | } else { |
||
245 | $lastUpdatedAt = new DateTime($item['lastedit_date'], new DateTimeZone('UTC')); |
||
246 | } |
||
247 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
248 | |||
249 | // Old 1.11.x visibility (item property) is based in this switch: |
||
250 | switch ($visibility) { |
||
251 | case 0: |
||
252 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
253 | |||
254 | break; |
||
255 | |||
256 | case 1: |
||
257 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
258 | |||
259 | break; |
||
260 | } |
||
261 | |||
262 | // If c_item_property.insert_user_id doesn't exist we use the first admin id. |
||
263 | $user = null; |
||
264 | if (isset($userList[$userId])) { |
||
265 | $user = $userList[$userId]; |
||
266 | } else { |
||
267 | if (!empty($userId)) { |
||
268 | $userFound = $userRepo->find($userId); |
||
269 | if ($userFound) { |
||
270 | $user = $userList[$userId] = $userRepo->find($userId); |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | if (null === $user) { |
||
276 | $user = $admin; |
||
277 | } |
||
278 | |||
279 | $session = null; |
||
280 | if (!empty($sessionId)) { |
||
281 | if (isset($sessionList[$sessionId])) { |
||
282 | $session = $sessionList[$sessionId]; |
||
283 | } else { |
||
284 | $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | $group = null; |
||
289 | if (!empty($groupId)) { |
||
290 | if (isset($groupList[$groupId])) { |
||
291 | $group = $groupList[$groupId]; |
||
292 | } else { |
||
293 | $group = $groupList[$groupId] = $groupRepo->find($groupId); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | if (null === $resourceNode) { |
||
298 | $resourceNode = $repo->addResourceNode($resource, $user, $parentResource); |
||
299 | $this->entityManager->persist($resourceNode); |
||
300 | } |
||
301 | $resource->addCourseLink($course, $session, $group, $newVisibility); |
||
302 | |||
303 | if (2 === $visibility) { |
||
304 | $link = $resource->getResourceNode()->getResourceLinkByContext($course, $session, $group); |
||
305 | $link->setDeletedAt($lastUpdatedAt); |
||
306 | } |
||
307 | |||
308 | $this->entityManager->persist($resource); |
||
309 | } |
||
310 | |||
311 | return true; |
||
312 | } |
||
313 | |||
314 | public function fileExists($filePath): bool |
||
315 | { |
||
316 | return file_exists($filePath) && !is_dir($filePath) && is_readable($filePath); |
||
317 | } |
||
318 | |||
319 | public function findCourse(int $id): ?Course |
||
320 | { |
||
321 | if (0 === $id) { |
||
322 | return null; |
||
323 | } |
||
324 | |||
325 | return $this->entityManager->find(Course::class, $id); |
||
326 | } |
||
327 | |||
328 | public function findSession(int $id): ?Session |
||
329 | { |
||
330 | if (0 === $id) { |
||
331 | return null; |
||
332 | } |
||
333 | |||
334 | return $this->entityManager->find(Session::class, $id); |
||
335 | } |
||
336 | |||
337 | private function generateFilePath(string $filename): string |
||
338 | { |
||
339 | $cacheDir = $this->getContainer()->get('kernel')->getCacheDir(); |
||
340 | |||
341 | return $cacheDir.'/migration_'.$filename; |
||
342 | } |
||
343 | |||
344 | protected function writeFile(string $filename, string $content): void |
||
350 | } |
||
351 | |||
352 | protected function readFile(string $filename): string |
||
353 | { |
||
354 | $fullFilename = $this->generateFilePath($filename); |
||
355 | |||
356 | return file_get_contents($fullFilename); |
||
357 | } |
||
358 | |||
359 | protected function removeFile(string $filename): string |
||
365 | } |
||
366 | } |
||
367 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.