Passed
Push — master ( 068028...bea80d )
by Julito
08:43
created

Version20210813150011::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 (file_exists($rootPath) && !is_dir($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