Passed
Push — master ( 03a127...9b09a6 )
by Julito
09:17
created

Version20201010224040::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
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\SysAnnouncement;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CoreBundle\Repository\SysAnnouncementRepository;
12
use Doctrine\DBAL\Connection;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20201010224040 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'sys_announcement DB';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $container = $this->getContainer();
25
        $doctrine = $container->get('doctrine');
26
        $em = $doctrine->getManager();
27
        /** @var Connection $connection */
28
        $connection = $em->getConnection();
29
30
        $sql = "SELECT * FROM sys_announcement";
31
        $result = $connection->executeQuery($sql);
32
        $items = $result->fetchAllAssociative();
33
34
        $repo = $container->get(SysAnnouncementRepository::class);
35
36
        foreach ($items as $itemData) {
37
            $id = $itemData['id'];
38
            /** @var SysAnnouncement $announcement */
39
            $announcement = $repo->find($id);
40
41
            $legacyRoles = [
42
                'visible_teacher' => 'ROLE_TEACHER',
43
                'visible_student' => 'ROLE_STUDENT',
44
                'visible_guest' => 'ROLE_ANONYMOUS',
45
                'visible_drh' => 'ROLE_RRHH',
46
                'visible_session_admin' => 'ROLE_SESSION_MANAGER',
47
                'visible_boss' => 'ROLE_STUDENT_BOSS',
48
            ];
49
50
            foreach ($legacyRoles as $old => $new) {
51
                $visible = $itemData[$old] ?? '';
52
                if (1 === (int) $visible) {
53
                    $announcement->addRole($new);
54
                }
55
            }
56
            $repo->update($announcement);
57
        }
58
    }
59
60
    public function down(Schema $schema): void
61
    {
62
    }
63
}
64