1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Gewaer\Models; |
||
5 | |||
6 | use Gewaer\Traits\PermissionsTrait; |
||
7 | use Gewaer\Traits\SubscriptionPlanLimitTrait; |
||
8 | use Phalcon\Cashier\Billable; |
||
9 | use Gewaer\Exception\UnprocessableEntityHttpException; |
||
10 | use Carbon\Carbon; |
||
11 | |||
12 | /** |
||
13 | * Class Users |
||
14 | * |
||
15 | * @package Gewaer\Models |
||
16 | * |
||
17 | * @property Users $user |
||
18 | * @property Config $config |
||
19 | * @property Apps $app |
||
20 | * @property Companies $defaultCompany |
||
21 | * @property \Phalcon\Di $di |
||
22 | */ |
||
23 | class Users extends \Baka\Auth\Models\Users |
||
24 | { |
||
25 | use PermissionsTrait; |
||
26 | use Billable; |
||
27 | use SubscriptionPlanLimitTrait; |
||
28 | |||
29 | public $default_company_branch; |
||
30 | public $roles_id; |
||
31 | public $stripe_id; |
||
32 | public $card_last_four; |
||
33 | public $card_brand; |
||
34 | public $trial_ends_at; |
||
35 | |||
36 | /** |
||
37 | * Provide the app plan id |
||
38 | * if the user is signing up a new company |
||
39 | * |
||
40 | * @var integer |
||
41 | */ |
||
42 | public $appPlanId = null; |
||
43 | |||
44 | /** |
||
45 | * Initialize method for model. |
||
46 | */ |
||
47 | 27 | public function initialize() |
|
48 | { |
||
49 | 27 | $this->setSource('users'); |
|
50 | |||
51 | //overwrite parent relationships |
||
52 | 27 | $this->hasOne('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'session']); |
|
53 | 27 | $this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
|
54 | 27 | $this->hasMany('id', 'Baka\Auth\Models\SessionKeys', 'users_id', ['alias' => 'sessionKeys']); |
|
55 | 27 | $this->hasMany('id', 'Baka\Auth\Models\Banlist', 'users_id', ['alias' => 'bans']); |
|
56 | 27 | $this->hasMany('id', 'Baka\Auth\Models\Sessions', 'users_id', ['alias' => 'sessions']); |
|
57 | 27 | $this->hasMany('id', 'Gewaer\Models\UserConfig', 'users_id', ['alias' => 'config']); |
|
58 | 27 | $this->hasMany('id', 'Gewaer\Models\UserLinkedSources', 'users_id', ['alias' => 'sources']); |
|
59 | 27 | $this->hasMany('id', 'Baka\Auth\Models\UsersAssociatedCompany', 'users_id', ['alias' => 'companies']); |
|
60 | 27 | $this->hasOne('default_company', 'Gewaer\Models\Companies', 'id', ['alias' => 'defaultCompany']); |
|
61 | |||
62 | 27 | $this->hasOne( |
|
63 | 27 | 'id', |
|
64 | 27 | 'Gewaer\Models\UserRoles', |
|
65 | 27 | 'users_id', |
|
66 | 27 | ['alias' => 'permission'] |
|
67 | ); |
||
68 | |||
69 | 27 | $this->hasMany( |
|
70 | 27 | 'id', |
|
71 | 27 | 'Gewaer\Models\UserRoles', |
|
72 | 27 | 'users_id', |
|
73 | 27 | ['alias' => 'permissions'] |
|
74 | ); |
||
75 | |||
76 | 27 | $this->hasManyToMany( |
|
77 | 27 | 'id', |
|
78 | 27 | 'Gewaer\Models\UserRoles', |
|
79 | 27 | 'users_id', |
|
80 | 27 | 'roles_id', |
|
81 | 27 | 'Gewaer\Models\Roles', |
|
82 | 27 | 'id', |
|
83 | [ |
||
84 | 27 | 'alias' => 'roles', |
|
85 | 'params' => [ |
||
86 | 27 | 'limit' => 1, |
|
87 | 27 | 'conditions' => 'Gewaer\Models\UserRoles.apps_id = ' . $this->di->getConfig()->app->id, |
|
88 | ] |
||
89 | ] |
||
90 | ); |
||
91 | 27 | } |
|
92 | |||
93 | /** |
||
94 | * Returns table name mapped in the model. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 22 | public function getSource() : string |
|
99 | { |
||
100 | 22 | return 'users'; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get the User key for redis |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getKey() : string |
||
109 | { |
||
110 | return $this->id; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get all of the subscriptions for the user. |
||
115 | */ |
||
116 | public function subscriptions() |
||
117 | { |
||
118 | $this->hasMany( |
||
119 | 'id', |
||
120 | Subscription::class, |
||
121 | 'user_id', |
||
122 | [ |
||
123 | 'alias' => 'subscriptions', |
||
124 | 'params' => [ |
||
125 | 'conditions' => 'apps_id = ?0 and company_id = ?1', |
||
126 | 'bind' => [$this->di->getApp()->getId(), $this->default_company], |
||
127 | 'order' => 'id DESC' |
||
128 | ] |
||
129 | ] |
||
130 | ); |
||
131 | return $this->getRelated('subscriptions'); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Strat a free trial |
||
136 | * |
||
137 | * @param Users $user |
||
138 | * @return Subscription |
||
139 | */ |
||
140 | 1 | public function startFreeTrial() : Subscription |
|
141 | { |
||
142 | 1 | $defaultPlan = AppsPlans::getDefaultPlan(); |
|
143 | |||
144 | 1 | $subscription = new Subscription(); |
|
145 | 1 | $subscription->user_id = $this->getId(); |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
146 | 1 | $subscription->company_id = $this->default_company; |
|
0 ignored issues
–
show
The property
company_id does not exist on Gewaer\Models\Subscription . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
147 | 1 | $subscription->apps_id = $this->di->getApp()->getId(); |
|
0 ignored issues
–
show
The property
apps_id does not exist on Gewaer\Models\Subscription . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
148 | 1 | $subscription->apps_plans_id = $this->di->getApp()->default_apps_plan_id; |
|
149 | 1 | $subscription->name = $defaultPlan->name; |
|
0 ignored issues
–
show
The property
name does not exist on Gewaer\Models\Subscription . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
150 | 1 | $subscription->stripe_id = $defaultPlan->stripe_id; |
|
0 ignored issues
–
show
The property
stripe_id does not exist on Gewaer\Models\Subscription . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
151 | 1 | $subscription->stripe_plan = $defaultPlan->stripe_plan; |
|
0 ignored issues
–
show
The property
stripe_plan does not exist on Gewaer\Models\Subscription . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
152 | 1 | $subscription->quantity = 1; |
|
0 ignored issues
–
show
|
|||
153 | 1 | $subscription->trial_ends_at = Carbon::now()->addDays($this->di->getApp()->plan->free_trial_dates)->toDateTimeString(); |
|
0 ignored issues
–
show
The property
trial_ends_at does not exist on Gewaer\Models\Subscription . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
154 | |||
155 | 1 | if (!$subscription->save()) { |
|
156 | throw new ServerErrorHttpException((string)current($this->getMessages())); |
||
0 ignored issues
–
show
The type
Gewaer\Models\ServerErrorHttpException was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
157 | } |
||
158 | |||
159 | 1 | $this->trial_ends_at = $subscription->trial_ends_at; |
|
0 ignored issues
–
show
The property
trial_ends_at does not exist on Gewaer\Models\Subscription . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
160 | 1 | $this->update(); |
|
161 | |||
162 | 1 | return $subscription; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Before create |
||
167 | * |
||
168 | * @return void |
||
169 | */ |
||
170 | 1 | public function beforeCreate() |
|
171 | { |
||
172 | 1 | parent::beforeCreate(); |
|
173 | |||
174 | //this is only empty when creating a new user |
||
175 | 1 | if (!empty($this->default_company)) { |
|
176 | //confirm if the app reach its limit |
||
177 | $this->isAtLimit(); |
||
178 | } |
||
179 | |||
180 | //Assign admin role to the system if we dont get a specify role |
||
181 | 1 | if (empty($this->roles_id)) { |
|
182 | 1 | $role = Roles::findFirstByName('Admins'); |
|
183 | 1 | $this->roles_id = $role->getId(); |
|
184 | } |
||
185 | 1 | } |
|
186 | |||
187 | /** |
||
188 | * What to do after the creation of a new users |
||
189 | * - Assign default role |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | 1 | public function afterCreate() |
|
194 | { |
||
195 | /** |
||
196 | * User signing up for a new app / plan |
||
197 | * How do we know? well he doesnt have a default_company |
||
198 | */ |
||
199 | 1 | if (empty($this->default_company)) { |
|
200 | 1 | $company = new Companies(); |
|
201 | 1 | $company->name = $this->defaultCompanyName; |
|
202 | 1 | $company->users_id = $this->getId(); |
|
203 | |||
204 | 1 | if (!$company->save()) { |
|
205 | throw new Exception(current($company->getMessages())); |
||
206 | } |
||
207 | |||
208 | 1 | $this->default_company = $company->getId(); |
|
209 | |||
210 | 1 | if (!$this->update()) { |
|
211 | throw new Exception(current($this->getMessages())); |
||
212 | } |
||
213 | |||
214 | 1 | $this->default_company_branch = $this->defaultCompany->branch->getId(); |
|
215 | 1 | $this->update(); |
|
216 | |||
217 | //update default subscription free trial |
||
218 | 1 | $company->app->subscriptions_id = $this->startFreeTrial()->getId(); |
|
0 ignored issues
–
show
The property
subscriptions_id does not exist on Gewaer\Models\Apps . Since you implemented __set , consider adding a @property annotation.
![]() |
|||
219 | 1 | $company->update(); |
|
220 | } else { |
||
221 | //we have the company id |
||
222 | if (empty($this->default_company_branch)) { |
||
223 | $this->default_company_branch = $this->defaultCompany->branch->getId(); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | //Create new company associated company |
||
228 | 1 | $newUserAssocCompany = new UsersAssociatedCompany(); |
|
229 | 1 | $newUserAssocCompany->users_id = $this->id; |
|
230 | 1 | $newUserAssocCompany->company_id = $this->default_company; |
|
231 | 1 | $newUserAssocCompany->identify_id = 1; |
|
232 | 1 | $newUserAssocCompany->user_active = 1; |
|
233 | 1 | $newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
|
234 | |||
235 | 1 | if (!$newUserAssocCompany->save()) { |
|
236 | throw new UnprocessableEntityHttpException((string)current($newUserAssocCompany->getMessages())); |
||
237 | } |
||
238 | |||
239 | //Insert record into user_roles |
||
240 | 1 | $userRole = new UserRoles(); |
|
241 | 1 | $userRole->users_id = $this->id; |
|
242 | 1 | $userRole->roles_id = $this->roles_id; |
|
243 | 1 | $userRole->apps_id = $this->di->getApp()->getId(); |
|
244 | 1 | $userRole->company_id = $this->default_company; |
|
245 | |||
246 | 1 | if (!$userRole->save()) { |
|
247 | throw new UnprocessableEntityHttpException((string)current($userRole->getMessages())); |
||
248 | } |
||
249 | |||
250 | //update model total activity |
||
251 | 1 | $this->updateAppActivityLimit(); |
|
252 | } |
||
253 | } |
||
254 |