Passed
Pull Request — master (#21)
by Maximo
04:23
created

Roles   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 113
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getByAppName() 0 19 3
A getSource() 0 3 1
A initialize() 0 9 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\ServerErrorHttpException;
7
use Baka\Auth\Models\Companies as BakaCompanies;
8
9
class Roles extends AbstractModel
10
{
11
    /**
12
     *
13
     * @var integer
14
     */
15
    public $id;
16
17
    /**
18
     *
19
     * @var integer
20
     */
21
    public $name;
22
23
    /**
24
     *
25
     * @var integer
26
     */
27
    public $description;
28
29
    /**
30
     *
31
     * @var integer
32
     */
33
    public $scope;
34
35
    /**
36
     *
37
     * @var integer
38
     */
39
    public $company_id;
40
41
    /**
42
     *
43
     * @var int
44
     */
45
    public $apps_id;
46
47
    /**
48
     *
49
     * @var string
50
     */
51
    public $created_at;
52
53
    /**
54
     *
55
     * @var string
56
     */
57
    public $updated_at;
58
59
    /**
60
     *
61
     * @var integer
62
     */
63
    public $is_deleted;
64
65
    /**
66
     * Default ACL company
67
     *
68
     */
69
    const DEFAULT_ACL_COMPANY_ID = 0;
70
    const DEFAULT_ACL_APP_ID = 0;
71
72
    /**
73
     * Initialize method for model.
74
     */
75 10
    public function initialize()
76
    {
77 10
        $this->setSource('roles');
78
79 10
        $this->hasMany(
80 10
            'roles_name',
81 10
            'Gewaer\Models\Roles',
82 10
            'name',
83 10
            ['alias' => 'role']
84
        );
85 10
    }
86
87
    /**
88
     * Returns table name mapped in the model.
89
     *
90
     * @return string
91
     */
92 10
    public function getSource(): string
93
    {
94 10
        return 'roles';
95
    }
96
97
    /**
98
     * Get the Role by it app name
99
     *
100
     * @param string $role
101
     * @return Roles
102
     */
103 6
    public static function getByAppName(string $role, BakaCompanies $company): Roles
104
    {
105
        //echeck if we have a dot , taht means we are sending the specific app to use
106 6
        if (strpos($role, '.') === false) {
107
            throw new ServerErrorHttpException('ACL - We are expecting the app for this role');
108
        }
109
110 6
        $appRole = explode('.', $role);
111 6
        $role = $appRole[1];
1 ignored issue
show
Unused Code introduced by
The assignment to $role is dead and can be removed.
Loading history...
112 6
        $appName = $appRole[0];
113
114
        //look for the app and set it
115 6
        if (!$app = Apps::getACLApp($appName)) {
116
            throw new ServerErrorHttpException('ACL - No app found for this role');
117
        }
118
119 6
        return self::findFirst([
120 6
            'conditions' => 'apps_id in (?0, ?1) AND company_id in (?2 , ?3)',
121 6
            'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID]
122
        ]);
123
    }
124
}
125