Test Failed
Pull Request — master (#160)
by Maximo
07:08
created

UsersInvite::afterCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Canvas\Traits\SubscriptionPlanLimitTrait;
7
use Canvas\Exception\ModelException;
8
use Phalcon\Di;
9
10
class UsersInvite extends AbstractModel
11
{
12
    use SubscriptionPlanLimitTrait;
13
14
    /**
15
     *
16
     * @var integer
17
     */
18
    public $id;
19
20
    /**
21
     *
22
     * @var string
23
     */
24
    public $invite_hash;
25
26
    /**
27
     *
28
     * @var integer
29
     */
30
    public $users_id;
31
32
    /**
33
     *
34
     * @var integer
35
     */
36
    public $companies_id;
37
38
    /**
39
     *
40
     * @var integer
41
     */
42
    public $role_id;
43
44
    /**
45
     *
46
     * @var integer
47
     */
48
    public $app_id;
49
50
    /**
51
     *
52
     * @var string
53
     */
54
    public $email;
55
56
    /**
57
     *
58
     * @var integer
59
     */
60
    public $is_deleted;
61
62
    /**
63
     *
64
     * @var string
65
     */
66
    public $created_at;
67
68
    /**
69
     *
70
     * @var string
71
     */
72
    public $updated_at;
73
74
    /**
75
     * Subscription plan key.
76
     */
77
    protected $subscriptionPlanLimitKey = 'users';
78
79
    /**
80
     * Initialize method for model.
81
     */
82
    public function initialize()
83
    {
84
        $this->setSource('users_invite');
85
86
        $this->belongsTo(
87
            'companies_id',
88
            'Canvas\Models\Companies',
89
            'id',
90
            ['alias' => 'company']
91
        );
92
93
        $this->belongsTo(
94
            'apps_id',
95
            'Canvas\Models\Apps',
96
            'id',
97
            ['alias' => 'app']
98
        );
99
    }
100
101
    /**
102
     * Returns table name mapped in the model.
103
     *
104
     * @return string
105
     */
106
    public function getSource(): string
107
    {
108
        return 'users_invite';
109
    }
110
111
    /**
112
     * What to do after the creation of a new users
113
     *  - Assign default role.
114
     *
115
     * @return void
116
     */
117
    public function afterCreate()
118
    {
119
        $this->updateAppActivityLimit();
120
    }
121
122
    /**
123
     * Get the invite by hash
124
     *
125
     * @param string $hash
126
     * @return UsersInvite
127
     */
128
    public static function getByHash(string $hash): UsersInvite
129
    {
130
        return self::findFirstOrFail([
131
            'conditions' => 'invite_hash = ?0 and is_deleted = 0',
132
            'bind' => [$hash]
133
        ]);
134
    }
135
136
    /**
137
     * Validate if the current email is valid to invite.
138
     *
139
     * @throws Exception
140
     * @param string $email
141
     * @return bool
142
     */
143
    public static function isValid(string $email, int $roleId = 1): bool
144
    {
145
        $userData = Di::getDefault()->getUserData();
146
        //check inviste
147
        $invitedUser = self::findFirst([
148
            'conditions' => 'email = ?0 and companies_id = ?1 and role_id = ?2 and is_deleted = 0',
149
            'bind' => [$email, $userData->currentCompanyId(), $roleId]
150
        ]);
151
152
        if (is_object($invitedUser)) {
153
            throw new ModelException('User already invited to this company app');
154
        }
155
156
        //check for user if they already are in this company app
157
        $userExists = Users::findFirst([
158
            'conditions' => 'email = ?0 and is_deleted = 0',
159
            'bind' => [$email]
160
        ]);
161
162
        if (is_object($userExists)) {
163
            if ($userData->defaultCompany->userAssociatedToCompany($userExists)) {
164
                throw new ModelException('User already is associated with this company app');
165
            }
166
        }
167
168
        return true;
169
    }
170
171
    /**
172
     * Given the form request return a new user invite.
173
     *
174
     * @param array $requets
175
     * @return void
176
     */
177
    public function newUser(array $request): Users
178
    {
179
        $user = new Users();
180
        $user->firstname = $request['firstname'];
181
        $user->lastname = $request['lastname'];
182
        $user->displayname = $request['displayname'];
183
        $user->password = $request['password'];
184
        $user->email = $this->email;
185
        $user->user_active = 1;
186
        $user->roles_id = $this->role_id;
187
        $user->created_at = date('Y-m-d H:m:s');
188
        $user->default_company = $this->companies_id;
189
        $user->default_company_branch = $this->company->branch->getId();
190
191
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user returns the type Canvas\Models\Users which is incompatible with the documented return type void.
Loading history...
192
    }
193
}
194