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

Version20250927180001   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

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

2 Methods

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

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