Passed
Push — master ( 8fa3ca...0a8765 )
by
unknown
21:09 queued 12:30
created

Version20250923205900::up()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 75
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 47
c 2
b 0
f 0
nc 9
nop 1
dl 0
loc 75
rs 8.5341

How to fix   Long Method   

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:

1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\ResourceLink;
11
use Chamilo\CoreBundle\Entity\Tool;
12
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
13
use Chamilo\CoreBundle\Settings\SettingsManager;
14
use Chamilo\CoreBundle\Tool\ToolChain;
15
use Chamilo\CourseBundle\Entity\CTool;
16
use Doctrine\DBAL\Schema\Schema;
17
use Doctrine\ORM\Query;
18
19
final class Version20250923205900 extends AbstractMigrationChamilo
20
{
21
    public function getDescription(): string
22
    {
23
        return 'Seed missing course tools into existing courses (one tool).';
24
    }
25
26
    public function up(Schema $schema): void
27
    {
28
        /** @var ToolChain $toolChain */
29
        $toolChain = $this->container->get(ToolChain::class);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

29
        /** @scrutinizer ignore-call */ 
30
        $toolChain = $this->container->get(ToolChain::class);

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.

Loading history...
30
        /** @var SettingsManager $settings */
31
        $settings  = $this->container->get(SettingsManager::class);
32
33
        // Ensure global catalog is seeded (tool + resource_types)
34
        $toolChain->createTools();
35
36
        $targetTitle = 'dropbox';
37
38
        // Get Tool ID by title (fail fast if not found)
39
        $toolId = $this->entityManager
40
            ->createQuery('SELECT t.id FROM Chamilo\CoreBundle\Entity\Tool t WHERE t.title = :title')
0 ignored issues
show
Bug introduced by
The method createQuery() does not exist on null. ( Ignorable by Annotation )

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

40
            ->/** @scrutinizer ignore-call */ createQuery('SELECT t.id FROM Chamilo\CoreBundle\Entity\Tool t WHERE t.title = :title')

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.

Loading history...
41
            ->setParameter('title', $targetTitle)
42
            ->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR);
43
44
        if ($toolId === null) {
45
            $this->write('Tool "'.$targetTitle.'" does not exist; aborting migration.');
46
            return;
47
        }
48
        $toolId = (int) $toolId;
49
50
        $activeOnCreate = $settings->getSetting('course.active_tools_on_create') ?? [];
51
        $visible        = \in_array($targetTitle, $activeOnCreate, true);
52
        $linkVisibility = $visible ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT;
53
54
        $batchSize = self::BATCH_SIZE;
55
        $i = 0;
56
57
        // Iterate over course IDs only (no heavy hydration)
58
        $q = $this->entityManager->createQuery('SELECT c.id FROM Chamilo\CoreBundle\Entity\Course c');
59
60
        foreach ($q->toIterable([], Query::HYDRATE_SCALAR) as $row) {
61
            $courseId = (int) $row['id'];
62
63
            // Does the course already have this tool?
64
            $exists = (int) $this->entityManager
65
                ->createQuery(
66
                    'SELECT COUNT(ct.iid)
67
                       FROM Chamilo\CourseBundle\Entity\CTool ct
68
                      WHERE ct.title = :title
69
                        AND ct.course = :course'
70
                )
71
                ->setParameter('title', $targetTitle)
72
                ->setParameter('course', $this->entityManager->getReference(Course::class, $courseId))
73
                ->getSingleScalarResult();
74
75
            if ($exists > 0) {
76
                continue;
77
            }
78
79
            $toolRef   = $this->entityManager->getReference(Tool::class, $toolId);
80
            $courseRef = $this->entityManager->getReference(Course::class, $courseId);
81
82
            $ctool = (new CTool())
83
                ->setTool($toolRef)
84
                ->setTitle($targetTitle)
85
                ->setVisibility($visible)
86
                ->setCourse($courseRef)
87
                ->setParent($courseRef)
88
                ->setCreator($courseRef->getCreator())
89
                ->addCourseLink($courseRef, null, null, $linkVisibility);
90
91
            $this->entityManager->persist($ctool);
92
93
            if ((++$i % $batchSize) === 0) {
94
                $this->entityManager->flush();
95
                $this->entityManager->clear();
96
            }
97
        }
98
99
        $this->entityManager->flush();
100
        $this->entityManager->clear();
101
    }
102
103
    public function down(Schema $schema): void {}
104
}
105