Passed
Pull Request — master (#5143)
by Angel Fernando Quiroz
08:25
created

AbstractMigrationChamilo::addSettingCurrent()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 31
nc 2
nop 13
dl 0
loc 58
rs 9.1128
c 0
b 0
f 0

How to fix   Long Method    Many Parameters   

Long Method

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations;
8
9
use Chamilo\CoreBundle\Entity\AbstractResource;
10
use Chamilo\CoreBundle\Entity\AccessUrl;
11
use Chamilo\CoreBundle\Entity\Admin;
12
use Chamilo\CoreBundle\Entity\Course;
13
use Chamilo\CoreBundle\Entity\ResourceInterface;
14
use Chamilo\CoreBundle\Entity\ResourceLink;
15
use Chamilo\CoreBundle\Entity\Session;
16
use Chamilo\CoreBundle\Entity\SettingsCurrent;
17
use Chamilo\CoreBundle\Entity\SettingsOptions;
18
use Chamilo\CoreBundle\Entity\User;
19
use Chamilo\CoreBundle\Repository\Node\UserRepository;
20
use Chamilo\CoreBundle\Repository\ResourceRepository;
21
use Chamilo\CoreBundle\Repository\SessionRepository;
22
use Chamilo\CourseBundle\Repository\CGroupRepository;
23
use DateTime;
24
use DateTimeZone;
25
use Doctrine\Migrations\AbstractMigration;
26
use Doctrine\ORM\EntityManager;
27
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
28
use Symfony\Component\DependencyInjection\ContainerInterface;
29
use Symfony\Component\HttpFoundation\File\UploadedFile;
30
31
abstract class AbstractMigrationChamilo extends AbstractMigration implements ContainerAwareInterface
32
{
33
    public const BATCH_SIZE = 20;
34
35
    private ?EntityManager $manager = null;
36
    private ?ContainerInterface $container = null;
37
38
    public function setEntityManager(EntityManager $manager): void
39
    {
40
        $this->manager = $manager;
41
    }
42
43
    public function setContainer(?ContainerInterface $container = null): void
44
    {
45
        $this->container = $container;
46
    }
47
48
    /**
49
     * @return ContainerInterface
50
     */
51
    public function getContainer()
52
    {
53
        return $this->container;
54
    }
55
56
    public function adminExist(): bool
57
    {
58
        $em = $this->getEntityManager();
59
        $connection = $em->getConnection();
60
61
        $sql = 'SELECT user_id FROM admin WHERE user_id IN (SELECT id FROM user) ORDER BY id LIMIT 1';
62
        $result = $connection->executeQuery($sql);
63
        $adminRow = $result->fetchAssociative();
64
65
        if (empty($adminRow)) {
66
            return false;
67
        }
68
69
        return true;
70
    }
71
72
    public function getAdmin(): User
73
    {
74
        $admin = $this->getEntityManager()
75
            ->getRepository(Admin::class)
76
            ->findOneBy([], ['id' => 'ASC'])
77
        ;
78
79
        return $admin->getUser();
80
    }
81
82
    /**
83
     * @return EntityManager
84
     */
85
    public function getEntityManager()
86
    {
87
        return $this->getContainer()->get('doctrine')->getManager();
88
    }
89
90
    /**
91
     * Speeds up SettingsCurrent creation.
92
     *
93
     * @param string $variable            The variable itself
94
     * @param string $subKey              The subkey
95
     * @param string $type                The type of setting (text, radio, select, etc)
96
     * @param string $category            The category (Platform, User, etc)
97
     * @param string $selectedValue       The default value
98
     * @param string $title               The setting title string name
99
     * @param string $comment             The setting comment string name
100
     * @param string $scope               The scope
101
     * @param string $subKeyText          Text if there is a subKey
102
     * @param int    $accessUrl           What URL it is for
103
     * @param bool   $accessUrlChangeable Whether it can be changed on each url
104
     * @param bool   $accessUrlLocked     Whether the setting for the current URL is
105
     *                                    locked to the current value
106
     * @param array  $options             Optional array in case of a radio-type field,
107
     *                                    to insert options
108
     */
109
    public function addSettingCurrent(
110
        $variable,
111
        $subKey,
112
        $type,
113
        $category,
114
        $selectedValue,
115
        $title,
116
        $comment,
117
        $scope = '',
118
        $subKeyText = '',
119
        $accessUrl = 1,
120
        $accessUrlChangeable = false,
121
        $accessUrlLocked = true,
122
        $options = []
123
    ): void {
124
        $em = $this->getEntityManager();
125
126
        $accessUrl = $em->find(AccessUrl::class, $accessUrl);
127
128
        $setting = new SettingsCurrent();
129
        $setting
130
            ->setVariable($variable)
131
            ->setSubkey($subKey)
132
            ->setType($type)
133
            ->setCategory($category)
134
            ->setSelectedValue($selectedValue)
135
            ->setTitle($title)
136
            ->setComment($comment)
137
            ->setScope($scope)
138
            ->setSubkeytext($subKeyText)
139
            ->setUrl($accessUrl)
140
            ->setAccessUrlChangeable((int) $accessUrlChangeable)
141
            ->setAccessUrlLocked((int) $accessUrlLocked)
142
        ;
143
144
        $em->persist($setting);
145
146
        if (\count($options) > 0) {
147
            foreach ($options as $option) {
148
                if (empty($option['text'])) {
149
                    if ('true' === $option['value']) {
150
                        $option['text'] = 'Yes';
151
                    } else {
152
                        $option['text'] = 'No';
153
                    }
154
                }
155
156
                $settingOption = new SettingsOptions();
157
                $settingOption
158
                    ->setVariable($variable)
159
                    ->setValue($option['value'])
160
                    ->setDisplayText($option['text'])
161
                ;
162
163
                $em->persist($settingOption);
164
            }
165
        }
166
        $em->flush();
167
    }
168
169
    /**
170
     * @param string     $variable
171
     * @param null|mixed $configuration
172
     */
173
    public function getConfigurationValue($variable, $configuration = null)
174
    {
175
        global $_configuration;
176
177
        if (isset($configuration)) {
178
            $_configuration = $configuration;
179
        }
180
181
        if (isset($_configuration[$variable])) {
182
            return $_configuration[$variable];
183
        }
184
185
        return false;
186
    }
187
188
    /**
189
     * Remove a setting completely.
190
     *
191
     * @param string $variable The setting variable name
192
     */
193
    public function removeSettingCurrent($variable): void
194
    {
195
        // to be implemented
196
    }
197
198
    public function addLegacyFileToResource(
199
        string $filePath,
200
        ResourceRepository $repo,
201
        AbstractResource $resource,
202
        $id,
203
        $fileName = '',
204
        $description = ''
205
    ): bool {
206
        $class = $resource::class;
207
        $documentPath = basename($filePath);
208
209
        if (is_dir($filePath) || (!is_dir($filePath) && !file_exists($filePath))) {
210
            $this->warnIf(true, "Cannot migrate {$class} #'.{$id}.' file not found: {$documentPath}");
211
212
            return false;
213
        }
214
215
        $mimeType = mime_content_type($filePath);
216
        if (empty($fileName)) {
217
            $fileName = basename($documentPath);
218
        }
219
        $file = new UploadedFile($filePath, $fileName, $mimeType, null, true);
220
        $repo->addFile($resource, $file);
221
222
        return true;
223
    }
224
225
    public function fixItemProperty(
226
        $tool,
227
        ResourceRepository $repo,
228
        $course,
229
        $admin,
230
        ResourceInterface $resource,
231
        $parentResource,
232
        array $items = []
233
    ) {
234
        $container = $this->getContainer();
235
        $em = $this->getEntityManager();
236
        $connection = $em->getConnection();
237
238
        $courseId = $course->getId();
239
        $id = $resource->getResourceIdentifier();
240
241
        if (empty($items)) {
242
            $sql = "SELECT * FROM c_item_property
243
                    WHERE tool = '{$tool}' AND c_id = {$courseId} AND ref = {$id}";
244
            $result = $connection->executeQuery($sql);
245
            $items = $result->fetchAllAssociative();
246
        }
247
248
        // For some reason the resource doesn't have a c_item_property value.
249
        if (empty($items)) {
250
            return false;
251
        }
252
253
        $sessionRepo = $container->get(SessionRepository::class);
254
        $groupRepo = $container->get(CGroupRepository::class);
255
        $userRepo = $container->get(UserRepository::class);
256
257
        $resource->setParent($parentResource);
0 ignored issues
show
Bug introduced by
The method setParent() does not exist on Chamilo\CoreBundle\Entity\ResourceInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Chamilo\CoreBundle\Entity\ResourceInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

257
        $resource->/** @scrutinizer ignore-call */ 
258
                   setParent($parentResource);
Loading history...
258
        $resourceNode = null;
259
        $userList = [];
260
        $groupList = [];
261
        $sessionList = [];
262
        foreach ($items as $item) {
263
            $visibility = (int) $item['visibility'];
264
            $userId = (int) $item['insert_user_id'];
265
            $sessionId = $item['session_id'] ?? 0;
266
            $groupId = $item['to_group_id'] ?? 0;
267
            $lastUpdatedAt = new DateTime($item['lastedit_date'], new DateTimeZone('UTC'));
268
269
            $newVisibility = ResourceLink::VISIBILITY_DRAFT;
270
271
            // Old 1.11.x visibility (item property) is based in this switch:
272
            switch ($visibility) {
273
                case 0:
274
                    $newVisibility = ResourceLink::VISIBILITY_DRAFT;
275
276
                    break;
277
278
                case 1:
279
                    $newVisibility = ResourceLink::VISIBILITY_PUBLISHED;
280
281
                    break;
282
            }
283
284
            // If c_item_property.insert_user_id doesn't exist we use the first admin id.
285
            $user = null;
286
            if (isset($userList[$userId])) {
287
                $user = $userList[$userId];
288
            } else {
289
                if (!empty($userId)) {
290
                    $userFound = $userRepo->find($userId);
291
                    if ($userFound) {
292
                        $user = $userList[$userId] = $userRepo->find($userId);
293
                    }
294
                }
295
            }
296
297
            if (null === $user) {
298
                $user = $admin;
299
            }
300
301
            $session = null;
302
            if (!empty($sessionId)) {
303
                if (isset($sessionList[$sessionId])) {
304
                    $session = $sessionList[$sessionId];
305
                } else {
306
                    $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId);
307
                }
308
            }
309
310
            $group = null;
311
            if (!empty($groupId)) {
312
                if (isset($groupList[$groupId])) {
313
                    $group = $groupList[$groupId];
314
                } else {
315
                    $group = $groupList[$groupId] = $groupRepo->find($groupId);
316
                }
317
            }
318
319
            if (null === $resourceNode) {
320
                $resourceNode = $repo->addResourceNode($resource, $user, $parentResource);
321
                $em->persist($resourceNode);
322
            }
323
            $resource->addCourseLink($course, $session, $group, $newVisibility);
0 ignored issues
show
Bug introduced by
The method addCourseLink() does not exist on Chamilo\CoreBundle\Entity\ResourceInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Chamilo\CoreBundle\Entity\User. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

323
            $resource->/** @scrutinizer ignore-call */ 
324
                       addCourseLink($course, $session, $group, $newVisibility);
Loading history...
324
325
            if (2 === $visibility) {
326
                $link = $resource->getResourceNode()->getResourceLinkByContext($course, $session, $group);
327
                $link->setDeletedAt($lastUpdatedAt);
328
            }
329
330
            $em->persist($resource);
331
        }
332
333
        return true;
334
    }
335
336
    public function fileExists($filePath): bool
337
    {
338
        return file_exists($filePath) && !is_dir($filePath) && is_readable($filePath);
339
    }
340
341
    public function findCourse(int $id): ?Course
342
    {
343
        if (0 === $id) {
344
            return null;
345
        }
346
347
        return $this->getEntityManager()->find(Course::class, $id);
348
    }
349
350
    public function findSession(int $id): ?Session
351
    {
352
        if (0 === $id) {
353
            return null;
354
        }
355
356
        return $this->getEntityManager()->find(Session::class, $id);
357
    }
358
}
359