Passed
Pull Request — master (#6795)
by
unknown
09:03
created

Version20250923205900   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 78
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 1 1
A getDescription() 0 3 1
B up() 0 68 8
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
18
final class Version20250923205900 extends AbstractMigrationChamilo
19
{
20
    public function getDescription(): string
21
    {
22
        return 'Seed missing course tools into existing courses (one tool).';
23
    }
24
25
    public function up(Schema $schema): void
26
    {
27
        /** @var ToolChain $toolChain */
28
        $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

28
        /** @scrutinizer ignore-call */ 
29
        $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...
29
        /** @var SettingsManager $settings */
30
        $settings  = $this->container->get(SettingsManager::class);
31
32
        // Ensure global catalog is seeded (tool + resource_types)
33
        $toolChain->createTools();
34
35
        $targetTitle = 'dropbox';
36
37
        $toolRepo   = $this->entityManager->getRepository(Tool::class);
0 ignored issues
show
Bug introduced by
The method getRepository() 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

37
        /** @scrutinizer ignore-call */ 
38
        $toolRepo   = $this->entityManager->getRepository(Tool::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...
38
        $courseRepo = $this->entityManager->getRepository(Course::class);
39
40
        /** @var Tool|null $tool */
41
        $tool = $toolRepo->findOneBy(['title' => $targetTitle]);
42
        if (!$tool) {
43
            $this->write('Tool "'.$targetTitle.'" does not exist; aborting migration.');
44
            return;
45
        }
46
47
        $activeOnCreate = $settings->getSetting('course.active_tools_on_create') ?? [];
48
        $batchSize = self::BATCH_SIZE;
49
        $i = 0;
50
51
        // Iterate over all courses using a memory-efficient cursor
52
        $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
53
54
        /** @var Course $course */
55
        foreach ($q->toIterable() as $course) {
56
            // Skip if the course already has a CTool with the same title
57
            $already = false;
58
            foreach ($course->getTools() as $ct) {
59
                if (0 === strcasecmp($ct->getTitle(), $targetTitle)) {
60
                    $already = true;
61
                    break;
62
                }
63
            }
64
            if ($already) {
65
                continue;
66
            }
67
68
            // Initial visibility and link visibility (same rule as addToolsInCourse)
69
            $visible = \in_array($targetTitle, $activeOnCreate, true);
70
            $linkVisibility = $visible ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT;
71
72
            // Create CTool and its ResourceLink
73
            $ctool = (new CTool())
74
                ->setTool($tool)
75
                ->setTitle($targetTitle)
76
                ->setVisibility($visible)
77
                ->setParent($course)
78
                ->setCreator($course->getCreator())
79
                ->addCourseLink($course, null, null, $linkVisibility);
80
81
            $course->addTool($ctool);
82
            $this->entityManager->persist($ctool);
83
84
            // Batch flush
85
            if ((++$i % $batchSize) === 0) {
86
                $this->entityManager->flush();
87
                $this->entityManager->clear();
88
            }
89
        }
90
91
        $this->entityManager->flush();
92
        $this->entityManager->clear();
93
    }
94
95
    public function down(Schema $schema): void {}
96
}
97