Passed
Push — master ( 5f975d...198b2c )
by Julito
10:29
created

Version20210813150011::up()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
nc 5
nop 1
dl 0
loc 40
rs 9.2248
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\Asset;
10
use Chamilo\CoreBundle\Entity\Skill;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Chamilo\CoreBundle\Repository\SkillRepository;
13
use Chamilo\Kernel;
14
use Doctrine\DBAL\Schema\Schema;
15
use Symfony\Component\HttpFoundation\File\UploadedFile;
16
17
class Version20210813150011 extends AbstractMigrationChamilo
18
{
19
    public function getDescription(): string
20
    {
21
        return 'Migrate skill badges';
22
    }
23
24
    public function up(Schema $schema): void
25
    {
26
        $container = $this->getContainer();
27
        /** @var Kernel $kernel */
28
        $kernel = $container->get('kernel');
29
        $rootPath = $kernel->getProjectDir();
30
        $doctrine = $container->get('doctrine');
31
32
        $em = $doctrine->getManager();
33
        //$connection = $em->getConnection();
34
        //$skillRepo = $container->get(SkillRepository::class);
35
36
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Skill c');
37
        /** @var Skill $skill */
38
        foreach ($q->toIterable() as $skill) {
39
            if ($skill->hasAsset()) {
40
                continue;
41
            }
42
43
            $icon = $skill->getIcon();
44
45
            if (empty($icon)) {
46
                continue;
47
            }
48
49
            $filePath = $rootPath.'/app/upload/badges/'.$icon;
50
            if ($this->fileExists($filePath)) {
51
                $mimeType = mime_content_type($filePath);
52
                $fileName = basename($filePath);
53
                $file = new UploadedFile($filePath, $fileName, $mimeType, null, true);
54
55
                $asset = (new Asset())
56
                    ->setCategory(Asset::SKILL)
57
                    ->setTitle($fileName)
58
                    ->setFile($file)
59
                ;
60
61
                $skill->setAsset($asset);
62
                $em->persist($skill);
63
                $em->flush();
64
            }
65
        }
66
    }
67
}
68