Passed
Pull Request — master (#5651)
by Yannick
07:40
created

Version20240709222700   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 4 1
A getDescription() 0 3 1
B up() 0 42 6
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\DataFixtures\PermissionFixtures;
10
use Chamilo\CoreBundle\Entity\Permission;
11
use Chamilo\CoreBundle\Entity\PermissionRelRole;
12
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20240709222700 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Insert default data into permissions and permission_rel_roles tables';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $permissions = PermissionFixtures::getPermissions();
25
        $roles = PermissionFixtures::getRoles();
26
        $permissionsMapping = PermissionFixtures::getPermissionsMapping();
27
28
        foreach ($permissions as $permData) {
29
            $permissionRepository = $this->entityManager->getRepository(Permission::class);
0 ignored issues
show
Bug introduced by
The method getRepository() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            /** @scrutinizer ignore-call */ 
30
            $permissionRepository = $this->entityManager->getRepository(Permission::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
            $existingPermission = $permissionRepository->findOneBy(['slug' => $permData['slug']]);
31
32
            if ($existingPermission) {
33
                $permission = $existingPermission;
34
            } else {
35
                $permission = new Permission();
36
                $permission->setTitle($permData['title']);
37
                $permission->setSlug($permData['slug']);
38
                $permission->setDescription($permData['description']);
39
40
                $this->entityManager->persist($permission);
41
                $this->entityManager->flush();
42
            }
43
44
            foreach ($roles as $roleName => $roleCode) {
45
                if (in_array($roleCode, $permissionsMapping[$permData['slug']])) {
46
                    $permissionRelRoleRepository = $this->entityManager->getRepository(PermissionRelRole::class);
47
                    $existingRelation = $permissionRelRoleRepository->findOneBy([
48
                        'permission' => $permission,
49
                        'roleCode' => $roleName
50
                    ]);
51
52
                    if ($existingRelation) {
53
                        continue;
54
                    }
55
56
                    $permissionRelRole = new PermissionRelRole();
57
                    $permissionRelRole->setPermission($permission);
58
                    $permissionRelRole->setRoleCode($roleName);
59
                    $permissionRelRole->setChangeable(true);
60
                    $permissionRelRole->setUpdatedAt(new \DateTime());
61
62
                    $this->entityManager->persist($permissionRelRole);
63
                    $this->entityManager->flush();
64
                }
65
            }
66
        }
67
    }
68
69
    public function down(Schema $schema): void
70
    {
71
        $this->addSql('DELETE FROM permission_rel_roles');
72
        $this->addSql('DELETE FROM permissions');
73
    }
74
}
75