Failed Conditions
Pull Request — master (#30)
by Maximo
03:40
created

AccessList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 128
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAllByRole() 0 3 1
A getBy() 0 12 2
A initialize() 0 9 1
A exist() 0 5 1
A getSource() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Models;
6
7
use Phalcon\Di;
8
use Gewaer\Exception\ModelException;
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 7
    public function initialize()
70
    {
71 7
        $this->setSource('access_list');
72
73 7
        $this->belongsTo(
74 7
            'roles_name',
75 7
            'Gewaer\Models\Roles',
76 7
            'name',
77 7
            ['alias' => 'role']
78
        );
79 7
    }
80
81
    /**
82
     * Delete all the accest list records for this given role
83
     *
84
     * @param Roles $role
85
     * @return void
86
     */
87 1
    public static function deleteAllByRole(Roles $role): bool
88
    {
89 1
        return (bool) self::find('roles_id = ' . $role->getId())->delete();
90
    }
91
92
    /**
93
     * Returns table name mapped in the model.
94
     *
95
     * @return string
96
     */
97 7
    public function getSource(): string
98
    {
99 7
        return 'access_list';
100
    }
101
102
    /**
103
     * Given the resource and access check if exist
104
     *
105
     * @param Roles $role
106
     * @param string $resourceName
107
     * @param string $accessName
108
     * @return integer
109
     */
110 4
    public static function exist(Roles $role, string $resourceName, string $accessName): int
111
    {
112 4
        return self::count([
113 4
            'conditions' => 'roles_id = ?0 and resources_name = ?1 AND access_name = ?2 AND apps_id = ?3',
114 4
            'bind' => [$role->getId(), $resourceName, $accessName, Di::getDefault()->getAcl()->getApp()->getId()]
115
        ]);
116
    }
117
118
    /**
119
     * Given the resource and access check if exist
120
     *
121
     * @param Roles $role
122
     * @param string $resourceName
123
     * @param string $accessName
124
     * @return integer
125
     */
126 2
    public static function getBy(Roles $role, string $resourceName, string $accessName) : AccessList
127
    {
128 2
        $access = self::findFirst([
129 2
            'conditions' => 'roles_id = ?0 and resources_name = ?1 AND access_name = ?2 AND apps_id = ?3',
130 2
            'bind' => [$role->getId(), $resourceName, $accessName, Di::getDefault()->getAcl()->getApp()->getId()]
131
        ]);
132
133 2
        if (!is_object($access)) {
134
            throw new ModelException(_('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()));
135
        }
136
137 2
        return $access;
138
    }
139
}
140