Conditions | 8 |
Paths | 20 |
Total Lines | 59 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Tests | 26 |
CRAP Score | 8.4218 |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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(); |
|
1 ignored issue
–
show
|
|||
215 | 1 | $this->update(); |
|
216 | |||
217 | //update default subscription free trial |
||
218 | 1 | $company->app->subscriptions_id = $this->startFreeTrial()->getId(); |
|
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 | } |
||
254 |