Completed
Push — master ( 9c95f5...e428dc )
by Paweł
06:06
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Model;
18
19
use SWP\Bundle\UserBundle\Model\User as BaseUser;
20
use SWP\Component\MultiTenancy\Model\TenantAwareTrait;
21
22
class User extends BaseUser implements UserInterface
23
{
24
    use TenantAwareTrait;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function addRole($role)
30 100
    {
31
        $role = strtoupper($role);
32 100
        if ($role === static::ROLE_READER) {
33
            return $this;
34 100
        }
35 100
36
        if (!in_array($role, $this->roles, true)) {
37
            $this->roles[] = $role;
38
        }
39
40
        return $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getRoles()
47
    {
48
        $roles = $this->roles;
49
        foreach ($this->getGroups() as $group) {
50
            $roles = array_merge($roles, $group->getRoles());
51
        }
52
        // we need to make sure to have at least one role
53
        $roles[] = static::ROLE_READER;
54
55
        return array_unique($roles);
56
    }
57
}
58