Passed
Push — master ( bb15d1...ce9f14 )
by Julito
09:50
created

GroupRepository::createDefaultGroups()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 67
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 38
c 3
b 0
f 0
nc 6
nop 1
dl 0
loc 67
rs 9.0008

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
31
        /** @var Group $group */
32
        $group = $this->findOneBy($criteria);
33
34
        return $group->getUsers();
35
    }
36
37
    public function createDefaultGroups(AccessGroupFixtures $accessGroupFixtures = null): void
38
    {
39
        $groups = [
40
            [
41
                'code' => 'ADMIN',
42
                'title' => 'Administrators',
43
                'roles' => ['ROLE_ADMIN'],
44
            ],
45
            [
46
                'code' => 'STUDENT',
47
                'title' => 'Students',
48
                'roles' => ['ROLE_STUDENT'],
49
            ],
50
            [
51
                'code' => 'TEACHER',
52
                'title' => 'Teachers',
53
                'roles' => ['ROLE_TEACHER'],
54
            ],
55
            [
56
                'code' => 'RRHH',
57
                'title' => 'Human resources manager',
58
                'roles' => ['ROLE_RRHH'],
59
            ],
60
            [
61
                'code' => 'SESSION_MANAGER',
62
                'title' => 'Session',
63
                'roles' => ['ROLE_SESSION_MANAGER'],
64
            ],
65
            [
66
                'code' => 'QUESTION_MANAGER',
67
                'title' => 'Question manager',
68
                'roles' => ['ROLE_QUESTION_MANAGER'],
69
            ],
70
            [
71
                'code' => 'STUDENT_BOSS',
72
                'title' => 'Student boss',
73
                'roles' => ['ROLE_STUDENT_BOSS'],
74
            ],
75
            [
76
                'code' => 'INVITEE',
77
                'title' => 'Invitee',
78
                'roles' => ['ROLE_INVITEE'],
79
            ],
80
        ];
81
82
        $manager = $this->getEntityManager();
83
84
        foreach ($groups as $groupData) {
85
            $groupExists = $this->findOneBy(['code' => $groupData['code']]);
86
            if (null === $groupExists) {
87
                $group = new Group($groupData['title']);
88
                $group
89
                    ->setCode($groupData['code'])
90
                ;
91
92
                foreach ($groupData['roles'] as $role) {
93
                    $group->addRole($role);
94
                }
95
                $manager->persist($group);
96
97
                if (null !== $accessGroupFixtures) {
98
                    $accessGroupFixtures->addReference('GROUP_'.$groupData['code'], $group);
99
                }
100
            }
101
        }
102
103
        $manager->flush();
104
    }
105
}
106