Passed
Push — master ( 619086...47480f )
by Julito
10:49
created

GroupRepository::createDefaultGroups()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 71
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 41
c 2
b 0
f 0
nc 6
nop 1
dl 0
loc 71
rs 8.9528

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
        /** @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
                if (null !== $accessGroupFixtures) {
101
                    $accessGroupFixtures->addReference('GROUP_'.$groupData['code'], $group);
102
                }
103
            }
104
        }
105
106
        $manager->flush();
107
    }
108
}
109