Passed
Push — master ( fd60f2...93675b )
by Julito
16:32
created

Version20210205082253   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 124
rs 10
c 0
b 0
f 0
wmc 19

2 Methods

Rating   Name   Duplication   Size   Complexity  
F up() 0 117 18
A getDescription() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Entity\AccessUrl;
8
use Chamilo\CoreBundle\Entity\AccessUrlRelUserGroup;
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Entity\Usergroup;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
13
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
14
use Chamilo\CoreBundle\Repository\Node\UsergroupRepository;
15
use Doctrine\DBAL\Connection;
16
use Doctrine\DBAL\Schema\Schema;
17
use Symfony\Component\HttpFoundation\File\UploadedFile;
18
19
final class Version20210205082253 extends AbstractMigrationChamilo
20
{
21
    public function getDescription(): string
22
    {
23
        return 'Migrate User/Usergroups images';
24
    }
25
26
    public function up(Schema $schema): void
27
    {
28
        $container = $this->getContainer();
29
        $em = $this->getEntityManager();
30
        /** @var Connection $connection */
31
        $connection = $em->getConnection();
32
33
        $kernel = $container->get('kernel');
34
        $rootPath = $kernel->getProjectDir();
35
36
        $illustrationRepo = $container->get(IllustrationRepository::class);
37
38
        // Adding users to the resource node tree.
39
        $batchSize = self::BATCH_SIZE;
40
        $counter = 1;
41
        $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u');
42
43
        $sql = "SELECT * FROM settings_current WHERE variable = 'split_users_upload_directory' AND access_url = 1";
44
        $result = $connection->executeQuery($sql);
45
        $setting = $result->fetchAssociative();
46
47
        /** @var User $userEntity */
48
        foreach ($q->toIterable() as $userEntity) {
49
            if ($userEntity->hasResourceNode()) {
50
                continue;
51
            }
52
            $id = $userEntity->getId();
53
            $picture = $userEntity->getPictureUri();
54
            if (empty($picture)) {
55
                continue;
56
            }
57
            $path = "users/{$id}/";
58
            if (!empty($setting) && 'true' === $setting['selected_value']) {
59
                $path = 'users/'.substr((string) $id, 0, 1).'/'.$id.'/';
60
            }
61
            $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture;
62
            if ($this->fileExists($picturePath)) {
63
                $mimeType = mime_content_type($picturePath);
64
                $file = new UploadedFile($picturePath, $picture, $mimeType, null, true);
65
                $illustrationRepo->addIllustration($userEntity, $userEntity, $file);
66
            }
67
68
            if (($counter % $batchSize) === 0) {
69
                $em->flush();
70
                $em->clear(); // Detaches all objects from Doctrine!
71
            }
72
            $counter++;
73
        }
74
75
        $em->flush();
76
        $em->clear();
77
78
        // Migrate Usergroup.
79
        $counter = 1;
80
        $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u');
81
        $admin = $this->getAdmin();
82
83
        $userGroupRepo = $container->get(UsergroupRepository::class);
84
        $urlRepo = $container->get(AccessUrlRepository::class);
85
        $urlList = $urlRepo->findAll();
86
        /** @var AccessUrl $url */
87
        $url = $urlList[0];
88
89
        /** @var Usergroup $userGroup */
90
        foreach ($q->toIterable() as $userGroup) {
91
            if ($userGroup->hasResourceNode()) {
92
                continue;
93
            }
94
95
            $userGroup->setCreator($admin);
96
97
            if (0 === $userGroup->getUrls()->count()) {
98
                $accessUrlRelUserGroup = (new AccessUrlRelUserGroup())
99
                    ->setUserGroup($userGroup)
100
                    ->setUrl($url)
101
                ;
102
                $userGroup->getUrls()->add($accessUrlRelUserGroup);
103
            }
104
            $userGroupRepo->addResourceNode($userGroup, $admin, $url);
105
            $em->persist($userGroup);
106
            $em->flush();
107
        }
108
        $em->clear();
109
110
        // Migrate Usergroup images.
111
        $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u');
112
        /** @var Usergroup $userGroup */
113
        foreach ($q->toIterable() as $userGroup) {
114
            if (!$userGroup->hasResourceNode()) {
115
                continue;
116
            }
117
118
            $picture = $userGroup->getPicture();
119
            if (empty($picture)) {
120
                continue;
121
            }
122
            $id = $userGroup->getId();
123
            $path = "groups/{$id}/";
124
            if (!empty($setting) && 'true' === $setting['selected_value']) {
125
                $path = 'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
126
            }
127
            $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture;
128
            if ($this->fileExists($picturePath)) {
129
                $mimeType = mime_content_type($picturePath);
130
                $file = new UploadedFile($picturePath, $picture, $mimeType, null, true);
131
                $illustrationRepo->addIllustration($userGroup, $admin, $file);
132
            }
133
134
            if (($counter % $batchSize) === 0) {
135
                $em->flush();
136
                $em->clear(); // Detaches all objects from Doctrine!
137
            }
138
            $counter++;
139
        }
140
141
        $em->flush();
142
        $em->clear();
143
    }
144
}
145