Passed
Pull Request — master (#6835)
by Angel Fernando Quiroz
17:36 queued 08:49
created

Version20250927180002::up()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 123
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 77
c 3
b 0
f 0
nc 9
nop 1
dl 0
loc 123
rs 5.7951

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

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