Passed
Push — master ( e9f4b2...682802 )
by Maximo
02:58
created

Users::afterCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 17
ccs 12
cts 13
cp 0.9231
crap 3.004
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Traits\PermissionsTrait;
7
use Gewaer\Exception\ModelException;
8
use Phalcon\Cashier\Billable;
9
10
class Users extends \Baka\Auth\Models\Users
11
{
12
    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...
13
    use Billable;
1 ignored issue
show
introduced by
The trait Phalcon\Cashier\Billable requires some properties which are not provided by Gewaer\Models\Users: $stripe, $data, $brand, $stripe_plan, $last4, $paid, $customer, $secretKey, $card, $sources, $subscriptions
Loading history...
14
15
    public $roles_id;
16
    public $stripe_id;
17
    public $card_last_four;
18
    public $card_brand;
19
    public $trial_ends_at;
20
21
    /**
22
     * Initialize method for model.
23
     */
24 15
    public function initialize()
25
    {
26 15
        parent::initialize();
27
28 15
        $this->setSource('users');
29
30 15
        $this->hasOne(
31 15
            'id',
32 15
            'Gewaer\Models\UserRoles',
33 15
            'users_id',
34 15
            ['alias' => 'permission']
35
        );
36
37 15
        $this->hasMany(
38 15
            'id',
39 15
            'Gewaer\Models\UserRoles',
40 15
            'users_id',
41 15
            ['alias' => 'permissions']
42
        );
43
44 15
        $this->hasManyToMany(
45 15
            'id',
46 15
            'Gewaer\Models\UserRoles',
47 15
            'users_id',
48 15
            'roles_id',
49 15
            'Gewaer\Models\Roles',
50 15
            'id',
51
            [
52 15
                'alias' => 'roles',
53
                'params' => [
54 15
                    'limit' => 1,
55 15
                    'conditions' => 'Gewaer\Models\UserRoles.apps_id = ' . $this->di->getConfig()->app->id,
56
                ]
57
            ]
58
        );
59 15
    }
60
61
    /**
62
     * Returns table name mapped in the model.
63
     *
64
     * @return string
65
     */
66 11
    public function getSource(): string
67
    {
68 11
        return 'users';
69
    }
70
71
    /**
72
     * Get the User key for redis
73
     *
74
     * @return string
75
     */
76
    public function getKey(): string
77
    {
78
        return $this->id;
79
    }
80
81
    /**
82
     * What to do after the creation of a new users
83
     *  - Assign default role
84
     *
85
     * @return void
86
     */
87 1
    public function afterCreate()
88
    {
89 1
        parent::afterCreate();
90
91
        //Assign admin role to the system if we dont get a specify role
92 1
        if (empty($this->roles_id)) {
93 1
            $role = Roles::findFirstByName('Admins');
94 1
            $this->roles_id = $role->getId();
95 1
            $this->update();
96
97 1
            $userRoles = new UserRoles();
98 1
            $userRoles->users_id = $this->getId();
99 1
            $userRoles->roles_id = $role->getId();
100 1
            $userRoles->apps_id = $this->di->getConfig()->app->id;
101 1
            $userRoles->company_id = $this->default_company;
102 1
            if (!$userRoles->save()) {
103
                throw new ModelException((string) current($userRoles->getMessages()));
104
            }
105
        }
106 1
    }
107
}
108