User::hasRole()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eloyekunle\PermissionsBundle\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
17
/**
18
 * Storage agnostic user object.
19
 */
20
abstract class User implements UserInterface
21
{
22
    /**
23
     * @var RoleInterface[]|Collection
24
     */
25
    protected $userRoles;
26
27
    /**
28
     * User constructor.
29
     */
30 5
    public function __construct()
31
    {
32 5
        $this->userRoles = new ArrayCollection();
33 5
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function getRoles()
39
    {
40 1
        $roleNames = [];
41 1
        $roles = $this->getUserRoles();
42
43 1
        foreach ($roles as $role) {
44 1
            $roleNames[] = $role->getRole();
45
        }
46
47 1
        return array_unique($roleNames);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 3
    public function hasRole(RoleInterface $role)
54
    {
55 3
        return $this->userRoles->contains($role);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function isSuperAdmin()
62
    {
63 1
        $isSuperAdmin = false;
64 1
        foreach ($this->getUserRoles() as $role) {
65 1
            if ($role->isSuperAdmin()) {
66 1
                return true;
67
            }
68
        }
69
70 1
        return $isSuperAdmin;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function removeRole(RoleInterface $role)
77
    {
78 1
        if (!$this->userRoles->contains($role)) {
79 1
            return;
80
        }
81
82 1
        $this->userRoles->removeElement($role);
83 1
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 5
    public function addRole(RoleInterface $role)
89
    {
90 5
        if ($this->userRoles->contains($role)) {
91 1
            return;
92
        }
93
94 5
        $this->userRoles->add($role);
95 5
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1
    public function setUserRoles(array $userRoles)
101
    {
102 1
        foreach ($userRoles as $role) {
103 1
            $this->addRole($role);
104
        }
105 1
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 2
    public function hasPermission($permission)
111
    {
112 2
        $hasPermission = false;
113
114 2
        foreach ($this->getUserRoles() as $role) {
115 2
            if ($role->isSuperAdmin() || $role->hasPermission($permission)) {
116 2
                return true;
117
            }
118
        }
119
120 2
        return $hasPermission;
121
    }
122
123
    /**
124
     * @return RoleInterface[]
125
     */
126 3
    public function getUserRoles()
127
    {
128 3
        return $this->userRoles;
129
    }
130
}
131