Passed
Push — master ( 6cd33d...652a98 )
by Julito
10:13
created

Version20210923090920   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 28 4
A getDescription() 0 3 1
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\Course;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CoreBundle\Repository\Node\IllustrationRepository;
12
use Chamilo\Kernel;
13
use Doctrine\DBAL\Schema\Schema;
14
use Symfony\Component\HttpFoundation\File\UploadedFile;
15
16
final class Version20210923090920 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Course pictures';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $em = $this->getEntityManager();
26
        $container = $this->getContainer();
27
28
        /** @var Kernel $kernel */
29
        $kernel = $container->get('kernel');
30
        $rootPath = $kernel->getProjectDir();
31
        $illustrationRepo = $container->get(IllustrationRepository::class);
32
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
33
34
        /** @var Course $course */
35
        foreach ($q->toIterable() as $course) {
36
            $directory = $course->getDirectory();
37
            $picturePath = $rootPath.'/app/courses/'.$directory.'/course-pic.png';
38
            $admin = $this->getAdmin();
39
40
            if ($illustrationRepo->hasIllustration($course)) {
41
                continue;
42
            }
43
44
            if ($this->fileExists($picturePath)) {
45
                $mimeType = mime_content_type($picturePath);
46
                $uploadFile = new UploadedFile($picturePath, 'course-pic', $mimeType, null, true);
47
                $illustrationRepo->addIllustration(
48
                    $course,
49
                    $admin,
50
                    $uploadFile
51
                );
52
            }
53
        }
54
    }
55
}
56