| Conditions | 9 |
| Paths | 26 |
| Total Lines | 60 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 9.4867 |
| 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 | 4 | 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 | 4 | 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 | 3 | 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 | 4 | $newUserAssocCompany = new UsersAssociatedCompany(); |
|
| 229 | 4 | $newUserAssocCompany->users_id = $this->id; |
|
| 230 | 4 | $newUserAssocCompany->company_id = $this->default_company; |
|
| 231 | 4 | $newUserAssocCompany->identify_id = 1; |
|
| 232 | 4 | $newUserAssocCompany->user_active = 1; |
|
| 233 | 4 | $newUserAssocCompany->user_role = $this->roles_id == 1 ? 'admins' : 'users'; |
|
| 234 | |||
| 235 | 4 | if (!$newUserAssocCompany->save()) { |
|
| 236 | throw new UnprocessableEntityHttpException((string)current($newUserAssocCompany->getMessages())); |
||
| 237 | } |
||
| 238 | |||
| 239 | //Insert record into user_roles |
||
| 240 | 4 | $userRole = new UserRoles(); |
|
| 241 | 4 | $userRole->users_id = $this->id; |
|
| 242 | 4 | $userRole->roles_id = $this->roles_id; |
|
| 243 | 4 | $userRole->apps_id = $this->di->getApp()->getId(); |
|
| 244 | 4 | $userRole->company_id = $this->default_company; |
|
| 245 | |||
| 246 | 4 | if (!$userRole->save()) { |
|
| 247 | throw new UnprocessableEntityHttpException((string)current($userRole->getMessages())); |
||
| 248 | } |
||
| 249 | |||
| 250 | //update user activity when its not a empty user |
||
| 251 | 4 | if (empty($this->default_company)) { |
|
| 252 | $this->updateAppActivityLimit(); |
||
| 253 | } |
||
| 256 |