Passed
Push — master ( 0a87d5...5ba6bb )
by Julito
18:12
created

GroupRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 90
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdmins() 0 9 1
A createDefaultGroups() 0 69 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\DataFixtures\AccessGroupFixtures;
10
use Chamilo\CoreBundle\Entity\Group;
11
use Chamilo\CoreBundle\Entity\User;
12
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
13
use Doctrine\Persistence\ManagerRegistry;
14
15
class GroupRepository extends ServiceEntityRepository
16
{
17
    public function __construct(ManagerRegistry $registry)
18
    {
19
        parent::__construct($registry, Group::class);
20
    }
21
22
    /**
23
     * @return User[]
24
     */
25
    public function getAdmins()
26
    {
27
        $criteria = [
28
            'name' => 'admins',
29
        ];
30
        /** @var Group $group */
31
        $group = $this->findOneBy($criteria);
32
33
        return $group->getUsers();
34
    }
35
36
    public function createDefaultGroups(AccessGroupFixtures $accessGroupFixtures = null): void
37
    {
38
        $groups = [
39
            [
40
                'code' => 'ADMIN',
41
                'title' => 'Administrators',
42
                'roles' => ['ROLE_ADMIN'],
43
            ],
44
            [
45
                'code' => 'STUDENT',
46
                'title' => 'Students',
47
                'roles' => ['ROLE_STUDENT'],
48
            ],
49
            [
50
                'code' => 'TEACHER',
51
                'title' => 'Teachers',
52
                'roles' => ['ROLE_TEACHER'],
53
            ],
54
            [
55
                'code' => 'RRHH',
56
                'title' => 'Human resources manager',
57
                'roles' => ['ROLE_RRHH'],
58
            ],
59
            [
60
                'code' => 'SESSION_MANAGER',
61
                'title' => 'Session',
62
                'roles' => ['ROLE_SESSION_MANAGER'],
63
            ],
64
            [
65
                'code' => 'QUESTION_MANAGER',
66
                'title' => 'Question manager',
67
                'roles' => ['ROLE_QUESTION_MANAGER'],
68
            ],
69
            [
70
                'code' => 'STUDENT_BOSS',
71
                'title' => 'Student boss',
72
                'roles' => ['ROLE_STUDENT_BOSS'],
73
            ],
74
            [
75
                'code' => 'INVITEE',
76
                'title' => 'Invitee',
77
                'roles' => ['ROLE_INVITEE'],
78
            ],
79
        ];
80
81
        $manager = $this->getEntityManager();
82
83
        $repo = $manager->getRepository(Group::class);
84
        foreach ($groups as $groupData) {
85
            $criteria = [
86
                'code' => $groupData['code'],
87
            ];
88
            $groupExists = $repo->findOneBy($criteria);
89
            if (!$groupExists) {
90
                $group = new Group($groupData['title']);
91
                $group
92
                    ->setCode($groupData['code'])
93
                ;
94
95
                foreach ($groupData['roles'] as $role) {
96
                    $group->addRole($role);
97
                }
98
                $manager->persist($group);
99
100
                $accessGroupFixtures->addReference('GROUP_'.$groupData['code'], $group);
0 ignored issues
show
Bug introduced by
The method addReference() 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

100
                $accessGroupFixtures->/** @scrutinizer ignore-call */ 
101
                                      addReference('GROUP_'.$groupData['code'], $group);

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...
101
            }
102
        }
103
104
        $manager->flush();
105
    }
106
}
107