Passed
Pull Request — master (#6835)
by Angel Fernando Quiroz
09:45
created

Version20250927180002::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
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\Portfolio;
10
use Chamilo\CoreBundle\Entity\PortfolioComment;
11
use Chamilo\CoreBundle\Entity\ResourceLink;
12
use Chamilo\CoreBundle\Entity\User;
13
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
14
use Chamilo\CoreBundle\Repository\Node\PortfolioCommentRepository;
15
use Chamilo\CoreBundle\Repository\Node\PortfolioRepository;
16
use Chamilo\CoreBundle\Repository\Node\UserRepository;
17
use Chamilo\CoreBundle\Repository\ResourceRepository;
18
use DateTime;
19
use DateTimeZone;
20
use Doctrine\DBAL\Exception;
21
use Doctrine\DBAL\Schema\Schema;
22
23
class Version20250927180002 extends AbstractMigrationChamilo
24
{
25
    public function getDescription(): string
26
    {
27
        return 'Migrate portfolio comments to resource nodes';
28
    }
29
30
    /**
31
     * @throws Exception
32
     * @throws \Exception
33
     */
34
    public function up(Schema $schema): void
35
    {
36
        /** @var ResourceRepository $portfolioRepo */
37
        $portfolioRepo = $this->container->get(PortfolioRepository::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

37
        /** @scrutinizer ignore-call */ 
38
        $portfolioRepo = $this->container->get(PortfolioRepository::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
        /** @var ResourceRepository $commentRepo */
39
        $commentRepo = $this->container->get(PortfolioCommentRepository::class);
40
        /** @var UserRepository $userRepo */
41
        $userRepo = $this->container->get(UserRepository::class);
42
43
        $commentRows = $this->connection
44
            ->executeQuery("SELECT id, author_id, item_id, visibility, date FROM portfolio_comment ORDER BY id ASC")
45
            ->fetchAllAssociative();
46
47
        foreach ($commentRows as $commentRow) {
48
            /** @var PortfolioComment $comment */
49
            $comment = $commentRepo->find($commentRow['id']);
50
            /** @var User $author */
51
            $author = $userRepo->find($commentRow['author_id']);
52
            /** @var Portfolio $item */
53
            $item = $portfolioRepo->find($commentRow['item_id']);
54
            $visibility = (int) $commentRow['visibility'];
55
            $creationDate = new DateTime($commentRow['date']);
56
57
            $comment->setParent($item);
58
59
            $resourceNode = $portfolioRepo->addResourceNode(
60
                $comment,
61
                $author,
62
                $item
63
            );
64
65
            $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

65
            $this->entityManager->/** @scrutinizer ignore-call */ 
66
                                  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...
66
            $this->entityManager->flush();
67
68
            $resourceNode
69
                ->setCreatedAt($creationDate)
70
                ->setUpdatedAt($creationDate);
71
72
            $this->entityManager->flush();
73
74
            $courseLinkVisibility = match ($visibility) {
75
                PortfolioComment::VISIBILITY_VISIBLE => ResourceLink::VISIBILITY_PUBLISHED,
76
                default => ResourceLink::VISIBILITY_PENDING,
77
            };
78
79
            $itemsProperty = $this->connection
80
                ->executeQuery(
81
                    "SELECT * FROM c_item_property
82
                        WHERE tool = 'portfolio_comment' AND ref = {$comment->getId()}"
83
                )
84
                ->fetchAllAssociative();
85
86
            if (empty($itemsProperty)) {
87
                $itemRow = $this->connection
88
                    ->executeQuery("SELECT * FROM portfolio WHERE id = {$commentRow['item_id']}")
89
                    ->fetchAssociative();
90
91
                if ($itemRow && !empty($itemRow['c_id'])) {
92
                    $course = $this->findCourse($itemRow['c_id']);
93
                    $session = $itemRow['session_id'] ? $this->findSession($itemRow['session_id']) : null;
94
                    $creationDate = new DateTime($itemRow['creation_date']);
95
                    $updateDate = new DateTime($itemRow['update_date']);
96
97
                    $courseLink = $comment->addCourseLink(
98
                        $course,
99
                        $session,
100
                        null,
101
                        $courseLinkVisibility,
102
                        $creationDate,
103
                        $updateDate
104
                    );
105
106
                    $this->entityManager->persist($courseLink);
107
                }
108
            } else {
109
                foreach ($itemsProperty as $itemProperty) {
110
                    $course = $itemProperty['c_id'] ? $this->findCourse($itemProperty['c_id']) : null;
111
                    $session = $itemProperty['session_id'] ? $this->findSession($itemProperty['session_id']) : null;
112
                    $toUser = $itemProperty['to_user_id'] ? $userRepo->find($itemProperty['to_user_id']) : null;
113
                    $insertDate = new DateTime($itemProperty['insert_date'], new DateTimeZone('UTC'));
114
                    $editDate = new DateTime($itemProperty['lastedit_date'], new DateTimeZone('UTC'));
115
116
                    $resourceNode->setUpdatedAt($editDate);
117
118
                    if ($toUser) {
119
                        $userLink = $comment->addUserLink(
120
                            $toUser,
121
                            $course,
122
                            $session,
123
                            null,
124
                            $insertDate,
125
                            $editDate,
126
                        );
127
128
                        $this->entityManager->persist($userLink);
129
                    } else {
130
                        $courseLink = $comment->addCourseLink(
131
                            $course,
132
                            $session,
133
                            null,
134
                            $courseLinkVisibility,
135
                            $insertDate,
136
                            $editDate
137
                        );
138
139
                        $this->entityManager->persist($courseLink);
140
                    }
141
                }
142
            }
143
144
            $this->entityManager->flush();
145
        }
146
    }
147
}
148