Failed Conditions
Push — master ( eb5d54...ba9646 )
by Maximo
02:36
created

AccessList::deleteAllByRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 8
    public function initialize()
70
    {
71 8
        $this->setSource('access_list');
72
73 8
        $this->belongsTo(
74 8
            'roles_name',
75 8
            'Gewaer\Models\Roles',
76 8
            'name',
77 8
            ['alias' => 'role']
78
        );
79 8
    }
80
81
82
83
    /**
84
     * Returns table name mapped in the model.
85
     *
86
     * @return string
87
     */
88 8
    public function getSource(): string
89
    {
90 8
        return 'access_list';
91
    }
92
93
    /**
94
     * Given the resource and access check if exist
95
     *
96
     * @param Roles $role
97
     * @param string $resourceName
98
     * @param string $accessName
99
     * @return integer
100
     */
101 4
    public static function exist(Roles $role, string $resourceName, string $accessName): int
102
    {
103 4
        return self::count([
104 4
            'conditions' => 'roles_id = ?0 and resources_name = ?1 AND access_name = ?2 AND apps_id = ?3',
105 4
            'bind' => [$role->getId(), $resourceName, $accessName, Di::getDefault()->getAcl()->getApp()->getId()]
106
        ]);
107
    }
108
109
    /**
110
     * Given the resource and access check if exist
111
     *
112
     * @param Roles $role
113
     * @param string $resourceName
114
     * @param string $accessName
115
     * @return integer
116
     */
117 3
    public static function getBy(Roles $role, string $resourceName, string $accessName) : AccessList
118
    {
119 3
        $access = self::findFirst([
120 3
            'conditions' => 'roles_id = ?0 and resources_name = ?1 AND access_name = ?2 AND apps_id = ?3',
121 3
            'bind' => [$role->getId(), $resourceName, $accessName, Di::getDefault()->getAcl()->getApp()->getId()]
122
        ]);
123
124 3
        if (!is_object($access)) {
125
            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()));
126
        }
127
128 3
        return $access;
129
    }
130
}
131