Completed
Push — b0.25.0 ( 25876b...875a21 )
by Sebastian
05:28
created

PermissionTrait::canByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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 User permissions
21
     */
22
    protected $permission = [];
23
24
    /**
25
     * Check if a Permission is owned, use Permission instance.
26
     *
27
     * @param Permission $permission
28
     *
29
     * @return bool
30
     */
31
    public function can(Permission $permission): bool
32
    {
33
        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
    public function canById(int $permissionId): bool
44
    {
45
        if (isset($this->permission[$permissionId])) {
46
            return true;
47
        }
48
49
        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
    public function canByName(string $permissionName): bool
60
    {
61
        if (in_array($permissionName, array_column($this->permission, 'name'), true)) {
62
            return true;
63
        }
64
65
        return false;
66
    }
67
}
68