UserRoles   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 91.89%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 89
ccs 34
cts 37
cp 0.9189
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addRole() 0 14 3
A removeRole() 0 13 2
A setRoles() 0 18 3
A getRoles() 0 14 3
A getDefaultRoles() 0 4 1
A getValidRoles() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Users\Entity;
6
7
use Doctrine\ORM\Mapping as ORM;
8
9
/**
10
 * @ORM\Embeddable
11
 */
12
class UserRoles
13
{
14
    public const ROLE_USER = 'ROLE_USER';
15
    public const ROLE_MODERATOR = 'ROLE_MODER';
16
    public const ROLE_ADMIN = 'ROLE_ADMIN';
17
18
    /**
19
     * @ORM\Column(type="string", length=255, nullable=true)
20
     */
21
    private $roles;
22
23 32
    public function __construct()
24
    {
25 32
        $this->addRole(self::ROLE_USER);
26 32
    }
27
28 32
    public function addRole(string $role): self
29
    {
30 32
        if (\in_array($role, $this->getValidRoles(), true) === false) {
31 1
            throw new \InvalidArgumentException(sprintf('Invalid role: %s', $role));
32
        }
33
34 32
        if (array_search($role, $this->getRoles(), true) === false) {
35 2
            return $this->setRoles(
36 2
                array_merge($this->getRoles(), [$role])
37
            );
38
        }
39
40 32
        return $this;
41
    }
42
43 1
    public function removeRole(string $role): self
44
    {
45 1
        $roles = $this->getRoles();
46 1
        $foundedRoleKey = array_search($role, $roles, true);
47
48 1
        if ($foundedRoleKey !== false) {
49 1
            unset($roles[$foundedRoleKey]);
50
51 1
            return $this->setRoles($roles);
52
        }
53
54
        return $this;
55
    }
56
57 2
    private function setRoles(array $roles): self
58
    {
59 2
        if (!\count($roles)) {
60 1
            $this->roles = null;
61
62 1
            return $this;
63
        }
64
65 2
        $roles = json_encode($roles);
66
67 2
        if (mb_strlen($roles) > 255) {
68
            throw new \InvalidArgumentException(sprintf('UserRoles $roles is too long. Max 255 characters.'));
69
        }
70
71 2
        $this->roles = $roles;
72
73 2
        return $this;
74
    }
75
76 68
    public function getRoles(): array
77
    {
78 68
        if (!$this->roles) {
79 61
            return $this->getDefaultRoles();
80
        }
81
82 14
        $roles = (array) json_decode($this->roles);
83
84 14
        if (!\count($roles)) {
85
            return $this->getDefaultRoles();
86
        }
87
88 14
        return array_values($roles);
89
    }
90
91 61
    public function getDefaultRoles(): array
92
    {
93 61
        return [self::ROLE_USER];
94
    }
95
96 32
    public function getValidRoles(): array
97
    {
98 32
        return [self::ROLE_ADMIN, self::ROLE_MODERATOR, self::ROLE_USER];
99
    }
100
}
101