Passed
Push — master ( 45f289...4cdab1 )
by
unknown
15:09 queued 07:46
created

Version20241025120000::up()   C

Complexity

Conditions 15
Paths 23

Size

Total Lines 69
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 45
c 1
b 0
f 0
nc 23
nop 1
dl 0
loc 69
rs 5.9166

How to fix   Long Method    Complexity   

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\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CourseBundle\Entity\CDocument;
11
use Chamilo\CourseBundle\Repository\CDocumentRepository;
12
use Chamilo\CourseBundle\Repository\CQuizQuestionRepository;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20241025120000 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Ensure resource node and ResourceFile creation for CQuizQuestion with picture associations.';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $quizQuestionRepo = $this->container->get(CQuizQuestionRepository::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

24
        /** @scrutinizer ignore-call */ 
25
        $quizQuestionRepo = $this->container->get(CQuizQuestionRepository::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...
25
        $documentRepo = $this->container->get(CDocumentRepository::class);
26
27
        $questions = $quizQuestionRepo->findAll();
28
29
        foreach ($questions as $question) {
30
            $courseAdmin = $this->getAdmin();
31
            $pictureId = $question->getPicture();
32
            $course = null;
33
34
            if (!$question->hasResourceNode()) {
35
                if ($pictureId) {
36
                    $document = $this->findDocumentByPictureId($pictureId, $documentRepo);
37
                    if ($document && $document->hasResourceNode() && $document->getResourceNode()->getResourceLinks()->first()) {
38
                        $course = $document->getResourceNode()->getResourceLinks()->first()->getCourse();
39
                        error_log('Creating resource node for question IID ' . $question->getIid());
40
41
                        $resourceNode = $quizQuestionRepo->addResourceNode($question, $courseAdmin, $course);
42
                        $this->entityManager->persist($resourceNode);
0 ignored issues
show
Bug introduced by
The method persist() 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

42
                        $this->entityManager->/** @scrutinizer ignore-call */ 
43
                                              persist($resourceNode);

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...
43
44
                        // Flush here to ensure the resource node is saved
45
                        $this->entityManager->flush();
46
                    } else {
47
                        error_log('No course association for question IID ' . $question->getIid() . ' with document ' . $pictureId);
48
                        continue;
49
                    }
50
                }
51
            }
52
53
            if ($question->hasResourceNode() && $pictureId && !$question->getResourceNode()->hasResourceFile()) {
54
                error_log('Existing resource node found for question IID ' . $question->getIid() . ' but no ResourceFile.');
55
56
                $document = $this->findDocumentByPictureId($pictureId, $documentRepo);
57
                if ($document) {
58
                    error_log('Document found for picture ID ' . $pictureId . ' and question IID ' . $question->getIid());
59
60
                    if ($document->hasResourceNode() && !$document->getResourceNode()->hasResourceFile()) {
61
                        $course = $document->getResourceNode()->getResourceLinks()->first()->getCourse();
62
                        $filePath = $this->getUpdateRootPath() . '/app/courses/' . $course->getDirectory() . '/document/images/' . $document->getTitle();
63
                        if (file_exists($filePath)) {
64
                            $this->addLegacyFileToResource($filePath, $documentRepo, $document, $document->getIid());
65
                            $this->entityManager->persist($document);
66
                            $this->entityManager->flush();
67
                            error_log('ResourceFile created and flushed for document with IID ' . $document->getIid());
68
                        } else {
69
                            continue;
70
                        }
71
                    }
72
73
                    if ($document->getResourceNode()->hasResourceFile()) {
74
                        $resourceFile = $document->getResourceNode()->getResourceFiles()->first();
75
                        error_log('Resource file ready for question IID ' . $question->getIid() . ': ' . $resourceFile->getOriginalName());
76
77
                        $contents = $documentRepo->getResourceFileContent($document);
78
                        $quizQuestionRepo->addFileFromString(
79
                            $question,
80
                            $resourceFile->getOriginalName(),
81
                            $resourceFile->getMimeType(),
82
                            $contents
83
                        );
84
                        $this->entityManager->persist($question);
85
                    }
86
                }
87
            }
88
        }
89
90
        $this->entityManager->flush();
91
    }
92
93
    private function findDocumentByPictureId(string $pictureId, $documentRepo): ?CDocument
94
    {
95
        if (str_starts_with($pictureId, 'quiz-')) {
96
            return $documentRepo->findOneBy(['title' => $pictureId]) ?: null;
97
        }
98
99
        return $documentRepo->find($pictureId);
100
    }
101
}
102