AccessList   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 117
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 9 1
A exist() 0 5 1
A getBy() 0 12 2
A getSource() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Models;
6
7
use Phalcon\Di;
8
use Canvas\Http\Exception\NotFoundException;
9
10
class AccessList extends AbstractModel
11
{
12
    /**
13
     *
14
     * @var string
15
     */
16
    public $roles_name;
17
18
    /**
19
     *
20
     * @var string
21
     */
22
    public $resources_name;
23
24
    /**
25
     *
26
     * @var string
27
     */
28
    public $access_name;
29
30
    /**
31
     *
32
     * @var boolean
33
     */
34
    public $allowed;
35
36
    /**
37
     *
38
     * @var integer
39
     */
40
    public $apps_id;
41
42
    /**
43
     *
44
     * @var integer
45
     */
46
    public $roles_id;
47
48
    /**
49
     *
50
     * @var string
51
     */
52
    public $created_at;
53
54
    /**
55
     *
56
     * @var string
57
     */
58
    public $updated_at;
59
60
    /**
61
     *
62
     * @var integer
63
     */
64
    public $is_deleted;
65
66
    /**
67
     * Initialize method for model.
68
     */
69
    public function initialize()
70
    {
71
        $this->setSource('access_list');
72
73
        $this->belongsTo(
74
            'roles_name',
75
            'Canvas\Models\Roles',
76
            'name',
77
            ['alias' => 'role']
78
        );
79
    }
80
81
    /**
82
     * Returns table name mapped in the model.
83
     *
84
     * @return string
85
     */
86
    public function getSource(): string
87
    {
88
        return 'access_list';
89
    }
90
91
    /**
92
     * Given the resource and access check if exist.
93
     *
94
     * @param Roles $role
95
     * @param string $resourceName
96
     * @param string $accessName
97
     * @return integer
98
     */
99
    public static function exist(Roles $role, string $resourceName, string $accessName): int
100
    {
101
        return self::count([
102
            'conditions' => 'roles_id = ?0 and resources_name = ?1 AND access_name = ?2 AND apps_id = ?3',
103
            'bind' => [$role->getId(), $resourceName, $accessName, Di::getDefault()->getAcl()->getApp()->getId()]
104
        ]);
105
    }
106
107
    /**
108
     * Given the resource and access check if exist.
109
     *
110
     * @param Roles $role
111
     * @param string $resourceName
112
     * @param string $accessName
113
     * @return integer
114
     */
115
    public static function getBy(Roles $role, string $resourceName, string $accessName) : AccessList
116
    {
117
        $access = self::findFirst([
118
            'conditions' => 'roles_id = ?0 and resources_name = ?1 AND access_name = ?2 AND apps_id = ?3',
119
            'bind' => [$role->getId(), $resourceName, $accessName, Di::getDefault()->getAcl()->getApp()->getId()]
120
        ]);
121
122
        if (!is_object($access)) {
123
            throw new NotFoundException(_('Access for role ' . $role->name . ' with resource ' . $resourceName . '-' . $accessName . ' not found on this app ' . Di::getDefault()->getAcl()->getApp()->getId() . ' AND Company' . Di::getDefault()->getAcl()->getCompany()->getId()));
124
        }
125
126
        return $access;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $access returns the type object which is incompatible with the documented return type integer.
Loading history...
127
    }
128
}
129