Passed
Push — master ( ad5dd8...3078de )
by Julito
08:55
created

Version20210205082253   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
dl 0
loc 82
rs 10
c 1
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
C up() 0 75 13
A getDescription() 0 3 1
1
<?php
2
3
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
4
5
use Chamilo\CoreBundle\Entity\User;
6
use Chamilo\CoreBundle\Entity\Usergroup;
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
9
use Chamilo\CoreBundle\Repository\Node\UserRepository;
10
use Doctrine\DBAL\Connection;
11
use Doctrine\DBAL\Schema\Schema;
12
use Symfony\Component\HttpFoundation\File\UploadedFile;
13
14
final class Version20210205082253 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Migrate User/Usergroups images';
19
    }
20
21
    public function up(Schema $schema): void
22
    {
23
        $container = $this->getContainer();
24
        $doctrine = $container->get('doctrine');
25
        $em = $doctrine->getManager();
26
        /** @var Connection $connection */
27
        $connection = $em->getConnection();
28
29
        $kernel = $container->get('kernel');
30
        $rootPath = $kernel->getProjectDir();
31
32
        $userRepo = $container->get(UserRepository::class);
33
        $illustrationRepo = $container->get(IllustrationRepository::class);
34
35
        // Adding users to the resource node tree.
36
        $batchSize = self::BATCH_SIZE;
37
        $counter = 1;
38
        $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u');
39
40
        $sql = "SELECT * FROM settings_current WHERE variable = 'split_users_upload_directory' AND access_url = 1";
41
        $result = $connection->executeQuery($sql);
42
        $setting = $result->fetchAssociative();
43
44
        /** @var User $userEntity */
45
        foreach ($q->toIterable() as $userEntity) {
46
            if ($userEntity->hasResourceNode()) {
47
                continue;
48
            }
49
            $id = $userEntity->getId();
50
            $picture = $userEntity->getPictureUri();
51
            $path = "users/$id/";
52
            if (!empty($setting) && 'true' === $setting['selected_value']) {
53
                $path = 'users/'.substr((string) $id, 0, 1).'/'.$id.'/';
54
            }
55
            $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture;
56
            if (file_exists($picturePath)) {
57
                $file = new UploadedFile($picturePath, $picture, null, null, true);
58
                $illustrationRepo->addIllustration($userEntity, $userEntity, $file);
59
            }
60
61
            if (0 === $counter % $batchSize) {
62
                $em->flush();
63
                $em->clear(); // Detaches all objects from Doctrine!
64
            }
65
            $counter++;
66
        }
67
68
        // Migrate Usergroup images.
69
        $counter = 1;
70
        $q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\Usergroup u');
71
72
        $admin = $this->getAdmin();
73
74
        /** @var Usergroup $userGroup */
75
        foreach ($q->toIterable() as $userGroup) {
76
            if ($userGroup->hasResourceNode()) {
77
                continue;
78
            }
79
            $id = $userGroup->getId();
80
            $picture = $userGroup->getPicture();
81
            $path = "groups/$id/";
82
            if (!empty($setting) && 'true' === $setting['selected_value']) {
83
                $path = 'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
84
            }
85
            $picturePath = $rootPath.'/app/upload/'.$path.'/'.$picture;
86
            if (file_exists($picturePath)) {
87
                $file = new UploadedFile($picturePath, $picture, null, null, true);
88
                $illustrationRepo->addIllustration($userGroup, $admin, $file);
89
            }
90
91
            if (0 === $counter % $batchSize) {
92
                $em->flush();
93
                $em->clear(); // Detaches all objects from Doctrine!
94
            }
95
            $counter++;
96
        }
97
    }
98
}
99