Passed
Pull Request — master (#6835)
by Angel Fernando Quiroz
08:13
created

Version20250927180001   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 71
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
C up() 0 99 11
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\ResourceLink;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
13
use Chamilo\CoreBundle\Repository\Node\PortfolioRepository;
14
use Chamilo\CoreBundle\Repository\Node\UserRepository;
15
use Chamilo\CoreBundle\Repository\ResourceRepository;
16
use DateTime;
17
use DateTimeZone;
18
use Doctrine\DBAL\Schema\Schema;
19
use Exception;
20
21
class Version20250927180001 extends AbstractMigrationChamilo
22
{
23
    public function getDescription(): string
24
    {
25
        return 'Migrate portfolio items to resource nodes';
26
    }
27
28
    /**
29
     * @throws Exception
30
     */
31
    public function up(Schema $schema): void
32
    {
33
        /** @var ResourceRepository $portfolioRepo */
34
        $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

34
        /** @scrutinizer ignore-call */ 
35
        $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...
35
        $userRepo = $this->container->get(UserRepository::class);
36
37
        $portfolioRows = $this->connection
38
            ->executeQuery("SELECT * FROM portfolio ORDER BY id ASC")
39
            ->fetchAllAssociative();
40
41
        foreach ($portfolioRows as $portfolioRow) {
42
            $portfolioId = $portfolioRow['id'];
43
            $creationDate = new DateTime($portfolioRow['creation_date']);
44
            $updateDate = new DateTime($portfolioRow['update_date']);
45
46
            /** @var Portfolio $portfolio */
47
            $portfolio = $portfolioRepo->find($portfolioId);
48
            $creator = $userRepo->find($portfolioRow['user_id']);
49
            $course = $portfolioRow['c_id'] ? $this->findCourse($portfolioRow['c_id']) : null;
50
            $session = $portfolioRow['session_id'] ? $this->findSession($portfolioRow['session_id']) : null;
51
52
            $portfolio->setParent($creator);
53
54
            $resourceNode = $portfolioRepo->addResourceNode(
55
                $portfolio,
56
                $creator,
57
                $creator
58
            );
59
60
            $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

60
            $this->entityManager->/** @scrutinizer ignore-call */ 
61
                                  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...
61
            $this->entityManager->flush();
62
63
            $resourceNode
64
                ->setCreatedAt($creationDate)
65
                ->setUpdatedAt($updateDate);
66
67
            $this->entityManager->flush();
68
69
            $resourceVisibility = match ($portfolio->getVisibility()) {
70
                Portfolio::VISIBILITY_HIDDEN => ResourceLink::VISIBILITY_DRAFT,
71
                Portfolio::VISIBILITY_VISIBLE => ResourceLink::VISIBILITY_PUBLISHED,
72
                default => ResourceLink::VISIBILITY_PENDING,
73
            };
74
75
            $itemsProperty = $this->connection
76
                ->executeQuery(
77
                    "SELECT * FROM c_item_property
78
                        WHERE tool = 'portfolio' AND ref = $portfolioId"
79
                )
80
                ->fetchAllAssociative();
81
82
            if (empty($itemsProperty) && $course) {
83
                $courseLink = $portfolio->addCourseLink(
84
                    $course,
85
                    $session,
86
                    null,
87
                    $resourceVisibility,
88
                    $creationDate,
89
                    $updateDate
90
                );
91
92
                $this->entityManager->persist($courseLink);
93
            } else {
94
                foreach ($itemsProperty as $itemProperty) {
95
                    $course = $itemProperty['c_id'] ? $this->findCourse($itemProperty['c_id']) : null;
96
                    $session = $itemProperty['session_id'] ? $this->findSession($itemProperty['session_id']) : null;
97
                    $toUser = $itemProperty['to_user_id'] ? $userRepo->find($itemProperty['to_user_id']) : null;
98
                    $insertDate = new DateTime($itemProperty['insert_date'], new DateTimeZone('UTC'));
99
                    $editDate = new DateTime($itemProperty['lastedit_date'], new DateTimeZone('UTC'));
100
101
                    $resourceNode->setUpdatedAt($editDate);
102
103
                    if ($toUser) {
104
                        $userLink = $portfolio->addUserLink(
105
                            $toUser,
106
                            $course,
107
                            $session,
108
                            null,
109
                            $insertDate,
110
                            $editDate
111
                        );
112
113
                        $this->entityManager->persist($userLink);
114
                    } else {
115
                        $courseLink = $portfolio->addCourseLink(
116
                            $course,
117
                            $session,
118
                            null,
119
                            $resourceVisibility,
120
                            $creationDate,
121
                            $updateDate
122
                        );
123
124
                        $this->entityManager->persist($courseLink);
125
                    }
126
                }
127
            }
128
129
            $this->entityManager->flush();
130
        }
131
    }
132
}
133