PermissionTrait::canByName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authorization;
13
14
/**
15
 * User PermissionTrait.
16
 */
17
trait PermissionTrait
18
{
19
    /**
20
     * @var array<mixed> User permissions
21
     */
22
    protected array $permission = [];
23
24
    /**
25
     * Check if a Permission is owned, use Permission instance.
26
     *
27
     * @param Permission $permission
28
     *
29
     * @return bool
30
     */
31 32
    public function can(Permission $permission): bool
32
    {
33 32
        return $this->canById($permission->getId());
34
    }
35
36
    /**
37
     * Check if a Permission is owned, use permission Id.
38
     *
39
     * @param int $permissionId
40
     *
41
     * @return bool
42
     */
43 62
    public function canById(int $permissionId): bool
44
    {
45 62
        if (isset($this->permission[$permissionId])) {
46 39
            return true;
47
        }
48
49 23
        return false;
50
    }
51
52
    /**
53
     * Check if a Permission is owned, use permission name.
54
     *
55
     * @param string $permissionName
56
     *
57
     * @return bool
58
     */
59 32
    public function canByName(string $permissionName): bool
60
    {
61 32
        if (\in_array($permissionName, \array_column($this->permission, 'name'), true)) {
62 20
            return true;
63
        }
64
65 12
        return false;
66
    }
67
}
68