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

Version20250927180002   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 79
c 3
b 0
f 0
dl 0
loc 125
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
C up() 0 114 13
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 *, 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
            /** @var PortfolioComment $parent */
55
            $parent = $commentRow['parent_id'] ? $commentRepo->find($commentRow['parent_id']) : null;
56
            $visibility = (int) $commentRow['visibility'];
57
            $creationDate = new DateTime($commentRow['date']);
58
            $resourceParent = $parent ?: $item;
59
60
            $comment->setParent($resourceParent);
61
62
            $resourceNode = $portfolioRepo->addResourceNode(
63
                $comment,
64
                $author,
65
                $resourceParent,
66
            );
67
68
            $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

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