Test Failed
Pull Request — master (#12)
by Maximo
03:04
created

Users   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 73
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 18 1
A getKey() 0 3 1
A getSource() 0 3 1
A afterCreate() 0 17 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Traits\PermissionsTrait;
7
use Gewaer\Exception\ModelException;
8
9
class Users extends \Baka\Auth\Models\Users
10
{
11
    use PermissionsTrait;
0 ignored issues
show
introduced by
The trait Gewaer\Traits\PermissionsTrait requires some properties which are not provided by Gewaer\Models\Users: $defaultCompany, $di, $roles, $app
Loading history...
12
13
    public $roles_id;
14
15
    /**
16
     * Initialize method for model.
17
     */
18 15
    public function initialize()
19
    {
20 15
        parent::initialize();
21
22 15
        $this->setSource('users');
23
24 15
        $this->hasOne(
25 15
            'id',
26 15
            'Gewaer\Models\UserRoles',
27 15
            'users_id',
28 15
            ['alias' => 'permission']
29
        );
30
31 15
        $this->hasMany(
32 15
            'id',
33 15
            'Gewaer\Models\UserRoles',
34 15
            'users_id',
35 15
            ['alias' => 'permissions']
36
        );
37 15
    }
38
39
    /**
40
     * Returns table name mapped in the model.
41
     *
42
     * @return string
43
     */
44 5
    public function getSource(): string
45
    {
46 5
        return 'users';
47
    }
48
49
    /**
50
     * Get the User key for redis
51
     *
52
     * @return string
53
     */
54
    public function getKey(): string
55
    {
56
        return $this->id;
57
    }
58
59
    /**
60
     * What to do after the creation of a new users
61
     *  - Assign default role
62
     *
63
     * @return void
64
     */
65 1
    public function afterCreate()
66
    {
67 1
        parent::afterCreate();
68
69
        //Assign admin role to the system if we dont get a specify role
70 1
        if (empty($this->roles_id)) {
71 1
            $role = Roles::findFirstByName('Admins');
72 1
            $this->roles_id = $role->getId();
73 1
            $this->update();
74
75 1
            $userRoles = new UserRoles();
76 1
            $userRoles->users_id = $this->getId();
77 1
            $userRoles->roles_id = $role->getId();
78 1
            $userRoles->apps_id = $this->di->getConfig()->app->id;
79 1
            $userRoles->company_id = $this->default_company;
80 1
            if (!$userRoles->save()) {
81
                throw new ModelException((string) current($userRoles->getMessages()));
82
            }
83
        }
84 1
    }
85
}
86