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

Version20250927180001::up()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 94
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 63
c 1
b 0
f 0
nc 21
nop 1
dl 0
loc 94
rs 7.5628

How to fix   Long Method   

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\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 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
        foreach ($portfolioRows as $portfolioRow) {
41
            $portfolioId = $portfolioRow['id'];
42
            $creatorId = $portfolioRow['user_id'];
43
            $courseId = $portfolioRow['c_id'];
44
            $sessionId = $portfolioRow['session_id'];
45
            $creationDate = new DateTime($portfolioRow['creation_date']);
46
            $updateDate = new DateTime($portfolioRow['update_date']);
47
48
            /** @var Portfolio $portfolio */
49
            $portfolio = $portfolioRepo->find($portfolioId);
50
            $creator = $userRepo->find($creatorId);
51
            $course = $courseId ? $this->findCourse($courseId) : null;
52
            $session = $sessionId ? $this->findSession($sessionId) : null;
53
54
            $portfolio->setParent($creator);
55
56
            $resourceNode = $portfolioRepo->addResourceNode(
57
                $portfolio,
58
                $creator,
59
                $creator
60
            );
61
62
            $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

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