Passed
Push — master ( 044f0c...7b524f )
by Angel Fernando Quiroz
08:11
created

Version20250927180004::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\PortfolioAttachment;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CoreBundle\Repository\Node\PortfolioCommentRepository;
12
use Chamilo\CoreBundle\Repository\Node\PortfolioRepository;
13
use Chamilo\CoreBundle\Repository\PortfolioAttachmentRepository;
14
use Doctrine\DBAL\Schema\Schema;
15
16
class Version20250927180004 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Migrate portfolio attachments';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        /** @var PortfolioAttachmentRepository $attachmentRepo */
26
        $attachmentRepo = $this->container->get(PortfolioAttachmentRepository::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

26
        /** @scrutinizer ignore-call */ 
27
        $attachmentRepo = $this->container->get(PortfolioAttachmentRepository::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...
27
28
        /** @var PortfolioRepository $itemRepo */
29
        $itemRepo = $this->container->get(PortfolioRepository::class);
30
31
        /** @var PortfolioCommentRepository $itemRepo */
32
        $commentRepo = $this->container->get(PortfolioCommentRepository::class);
33
34
        $attachmentRows = $this->connection
35
            ->executeQuery('SELECT * FROM portfolio_attachment')
36
            ->fetchAllAssociative()
37
        ;
38
39
        foreach ($attachmentRows as $attachmentRow) {
40
            $userId = 0;
41
            $resource = null;
42
            $resourceRepo = null;
43
44
            if (PortfolioAttachment::TYPE_ITEM === (int) $attachmentRow['origin_type']) {
45
                $resourceRepo = $itemRepo;
46
                $resource = $itemRepo->find($attachmentRow['origin_id']);
47
48
                $itemRow = $this->connection
49
                    ->executeQuery("SELECT user_id FROM portfolio WHERE id = {$attachmentRow['origin_id']}")
50
                    ->fetchAssociative()
51
                ;
52
53
                $userId = $itemRow['user_id'] ?? 0;
54
            } elseif (PortfolioAttachment::TYPE_COMMENT === (int) $attachmentRow['origin_type']) {
55
                $resourceRepo = $commentRepo;
56
                $resource = $commentRepo->find($attachmentRow['origin_id']);
57
58
                $commentRow = $this->connection
59
                    ->executeQuery("SELECT author_id FROM portfolio_comment WHERE id = {$attachmentRow['origin_id']}")
60
                    ->fetchAssociative()
61
                ;
62
63
                $userId = $commentRow['author_id'] ?? 0;
64
            }
65
66
            if (!$resource) {
67
                continue;
68
            }
69
70
            $folderId = ((string) $userId)[0];
71
            $path = $attachmentRow['path'];
72
73
            $filePath = $this->getUpdateRootPath()."/app/upload/users/$folderId/$userId/portfolio_attachments/$path";
74
75
            $this->write('MIGRATIONS :: $filePath -- '.$filePath.' ...');
76
77
            if (!file_exists($filePath)) {
78
                continue;
79
            }
80
81
            $this->addLegacyFileToResource(
82
                $filePath,
83
                $resourceRepo,
84
                $resource,
85
                $attachmentRow['origin_id'],
86
                $attachmentRow['filename'],
87
                $attachmentRow['comment']
88
            );
89
        }
90
91
        $this->entityManager->flush();
0 ignored issues
show
Bug introduced by
The method flush() 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

91
        $this->entityManager->/** @scrutinizer ignore-call */ 
92
                              flush();

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...
92
    }
93
}
94