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

Version20250927180003   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 98
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 87 7
A getDescription() 0 3 1
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 Version20250927180003 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
            foreach ($itemsProperty as $itemProperty) {
87
                $course = $itemProperty['c_id'] ? $this->findCourse($itemProperty['c_id']) : null;
88
                $session = $itemProperty['session_id'] ? $this->findSession($itemProperty['session_id']) : null;
89
                $toUser = $itemProperty['to_user_id'] ? $userRepo->find($itemProperty['to_user_id']) : null;
90
                $insertDate = new DateTime($itemProperty['insert_date'], new DateTimeZone('UTC'));
91
                $editDate = new DateTime($itemProperty['lastedit_date'], new DateTimeZone('UTC'));
92
93
                $resourceNode->setUpdatedAt($editDate);
94
95
                if ($toUser) {
96
                    $userLink = $comment->addUserLink(
97
                        $toUser,
98
                        $course,
99
                        $session,
100
                        null,
101
                        $insertDate,
102
                        $editDate,
103
                    );
104
105
                    $this->entityManager->persist($userLink);
106
                } else {
107
                    $courseLink = $comment->addCourseLink(
108
                        $course,
109
                        $session,
110
                        null,
111
                        $courseLinkVisibility,
112
                        $insertDate,
113
                        $editDate
114
                    );
115
116
                    $this->entityManager->persist($courseLink);
117
                }
118
            }
119
120
            $this->entityManager->flush();
121
        }
122
    }
123
}
124