Test Failed
Pull Request — master (#20)
by
unknown
03:57
created

Roles::getByAppName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 19
ccs 0
cts 11
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\ServerErrorHttpException;
7
use Gewaer\Models\Companies;
8
use Baka\Auth\Models\Companies as BakaCompanies;
9
use Phalcon\Di;
10
use Phalcon\Validation;
11
use Phalcon\Validation\Validator\PresenceOf;
12
use Phalcon\Validation\Validator\StringLength;
13
14
15
16
17
class Roles extends AbstractModel
18
{
19
    /**
20
     *
21
     * @var integer
22
     */
23
    public $id;
24
25
    /**
26
     *
27
     * @var string
28
     */
29
    public $name;
30
31
    /**
32
     *
33
     * @var string
34
     */
35
    public $description;
36
37
    /**
38
     *
39
     * @var integer
40
     */
41
    public $scope;
42
43
    /**
44
     *
45
     * @var integer
46
     */
47
    public $company_id;
48
49
    /**
50
     *
51
     * @var int
52
     */
53
    public $apps_id;
54
55
    /**
56
     *
57
     * @var string
58
     */
59
    public $created_at;
60
61
    /**
62
     *
63
     * @var string
64
     */
65
    public $updated_at;
66
67
    /**
68
     *
69
     * @var integer
70
     */
71
    public $is_deleted;
72
73
    /**
74
     * Default ACL company
75
     *
76
     */
77
    const DEFAULT_ACL_COMPANY_ID = 0;
78
    const DEFAULT_ACL_APP_ID = 0;
79
80
    /**
81
     * Initialize method for model.
82
     */
83 8
    public function initialize()
84
    {
85 8
        $this->setSource('roles');
86
87 8
        $this->hasMany(
88 8
            'id',
89 8
            'Gewaer\Models\AccessList',
90 8
            'roles_id',
91 8
            ['alias' => 'accesList']
92
        );
93 8
    }
94
95
    /**
96
     * Validations and business logic
97
     */
98
    public function validation()
99
    {
100
        $validator = new Validation();
101
102
        $validator->add(
103
            'name',
104
            new PresenceOf([
105
                'field' => 'name',
106
                'required' => true,
107
            ])
108
        );
109
110
        $validator->add(
111
            'description',
112
            new PresenceOf([
113
                'field' => 'description',
114
                'required' => true,
115
            ])
116
        );
117
118
        $validator->add(
119
            'name',
120
            new StringLength([
121
                'max' => 32,
122
                'messageMinimum' => _('Role Name. Maxium 32 characters.'),
123
            ])
124
        );
125
126
        return $this->validate($validator);
127
    }
128
129
    /**
130
     * Returns table name mapped in the model.
131
     *
132
     * @return string
133
     */
134 8
    public function getSource(): string
135
    {
136 8
        return 'roles';
137
    }
138
139
    /**
140
     * Get the entity by its name
141
     *
142
     * @param string $name
143
     * @return void
144
     */
145
    public static function getByName(string $name)
146
    {
147
        return self::findFirst([
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::findFirst(a...)->getApp()->getId()))) returns the type Phalcon\Mvc\Model which is incompatible with the documented return type void.
Loading history...
148
            'conditions' => 'name = ?0 AND company_id = ?1 AND apps_id = ?2 AND is_deleted = 0',
149
            'bind' => [$name, Di::getDefault()->getUserData()->default_company, Di::getDefault()->getApp()->getId()]
150
        ]);
151
    }
152
153
    /**
154
     * Get the entity by its name
155
     *
156
     * @param string $name
157
     * @return void
158
     */
159
    public static function getById(int $id)
160
    {
161
        return self::findFirst([
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::findFirst(a...EWAER_DEFAULT_APP_ID))) returns the type Phalcon\Mvc\Model which is incompatible with the documented return type void.
Loading history...
162
            'conditions' => 'id = ?0 AND company_id in (?1, ?2) AND apps_id in (?3, ?4) AND is_deleted = 0',
163
            'bind' => [$id, Di::getDefault()->getUserData()->default_company, Apps::GEWAER_DEFAULT_APP_ID, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID]
164
        ]);
165
    }
166
167
    /**
168
     * Get the Role by it app name
169
     *
170
     * @param string $role
171
     * @return Roles
172
     */
173
    public static function getByAppName(string $role, Companies $company): Roles
174
    {
175
        //echeck if we have a dot , taht means we are sending the specific app to use
176
        if (strpos($role, '.') === false) {
177
            throw new ServerErrorHttpException('ACL - We are expecting the app for this role');
178
        }
179
180
        $appRole = explode('.', $role);
181
        $role = $appRole[1];
1 ignored issue
show
Unused Code introduced by
The assignment to $role is dead and can be removed.
Loading history...
182
        $appName = $appRole[0];
183
184
        //look for the app and set it
185
        if (!$app = Apps::getACLApp($appName)) {
186
            throw new ServerErrorHttpException('ACL - No app found for this role');
187
        }
188
189
        return self::findFirst([
190
            'conditions' => 'apps_id in (?0, ?1) AND company_id in (?2 , ?3)',
191
            'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID]
192
        ]);
193
    }
194
195
    /**
196
     * Duplicate a role with it access list
197
     *
198
     * @return bool
199
     */
200
    public function copy(): Roles
201
    {
202
        $accesList = $this->accesList;
0 ignored issues
show
Bug Best Practice introduced by
The property accesList does not exist on Gewaer\Models\Roles. Since you implemented __get, consider adding a @property annotation.
Loading history...
203
204
        //remove id to create new record
205
        $this->name .= 'Copie';
206
        $this->scope = 1;
207
        $this->id = null;
208
        $this->company_id = $this->di->getUserData()->default_company;
0 ignored issues
show
Bug Best Practice introduced by
The property di does not exist on Gewaer\Models\Roles. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getUserData() does not exist on Phalcon\Mvc\Model\Resultset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
        $this->company_id = $this->di->/** @scrutinizer ignore-call */ getUserData()->default_company;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
209
        $this->apps_id = $this->di->getApp()->getId();
0 ignored issues
show
Bug introduced by
The method getApp() does not exist on Phalcon\Mvc\Model\Resultset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

209
        $this->apps_id = $this->di->/** @scrutinizer ignore-call */ getApp()->getId();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
210
        $this->save();
211
212
        foreach ($accesList as $access) {
213
            $copyAccessList = new AccessList();
214
            $copyAccessList->apps_id = $this->apps_id;
215
            $copyAccessList->roles_id = $this->getId();
216
            $copyAccessList->roles_name = $this->name;
217
            $copyAccessList->resources_name = $access->resources_name;
218
            $copyAccessList->access_name = $access->access_name;
219
            $copyAccessList->allowed = $access->allowed;
220
            $copyAccessList->create();
221
        }
222
223
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Gewaer\Models\Roles which is incompatible with the documented return type boolean.
Loading history...
224
    }
225
}
226