Conditions | 4 |
Paths | 38 |
Total Lines | 56 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
46 | protected function create(array $data) |
||
47 | { |
||
48 | try { |
||
49 | // DB::beginTransaction(); |
||
50 | // create person |
||
51 | $person = new Person(); |
||
52 | $person->name = $data['name']; |
||
|
|||
53 | $person->email = $data['email']; |
||
54 | $person->save(); |
||
55 | |||
56 | // get user_group_id |
||
57 | $user_group = UserGroup::where('name', 'Administrators')->first(); |
||
58 | if ($user_group == null) { |
||
59 | // create user_group |
||
60 | $user_group = UserGroup::create(['name'=>'Administrators', 'description'=>'Administrator users group']); |
||
61 | } |
||
62 | |||
63 | // get role_id |
||
64 | $role = Role::where('name', 'supervisor')->first(); |
||
65 | if ($role == null) { |
||
66 | $role = Role::create(['menu_id'=>1, 'name'=>'supervisor', 'display_name'=>'Supervisor', 'description'=>'Supervisor role.']); |
||
67 | } |
||
68 | $user = User::create([ |
||
69 | 'email' => $data['email'], |
||
70 | 'password' => Hash::make($data['password']), |
||
71 | 'person_id' => $person->id, |
||
72 | 'group_id' => $user_group->id, |
||
73 | 'role_id' => $role->id, |
||
74 | 'is_active' => 1, |
||
75 | ]); |
||
76 | // send verification email; |
||
77 | |||
78 | $this->initiateEmailActivation($user); |
||
79 | |||
80 | $company = Company::create([ |
||
81 | 'name' => $data['name'], |
||
82 | 'email' => $data['email'], |
||
83 | // 'is_active' => 1, |
||
84 | 'is_tenant' => 1, |
||
85 | 'status' => 1, |
||
86 | ]); |
||
87 | |||
88 | // $company->attachPerson($person->id, 'Owner'); |
||
89 | // DB::commit(); |
||
90 | |||
91 | $person->companies()->attach($company->id, ['person_id' => $person->id, 'is_main' => 1, 'is_mandatary' => 1, 'company_id' => $company->id]); |
||
92 | |||
93 | // Dispatch Tenancy Jobs |
||
94 | |||
95 | CreateDB::dispatch($company); |
||
96 | Migration::dispatch($company, $data['name'], $data['email'], $data['password']); |
||
97 | |||
98 | return $user; |
||
99 | } catch (\Exception $e) { |
||
100 | // DB::rollBack(); |
||
101 | throw $e; |
||
102 | } |
||
105 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.