Conditions | 11 |
Paths | 109 |
Total Lines | 89 |
Code Lines | 55 |
Lines | 13 |
Ratio | 14.61 % |
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 |
||
62 | public function create($sid, array $data) |
||
63 | { |
||
64 | $server = Server::with('node')->findOrFail($sid); |
||
|
|||
65 | |||
66 | $validator = Validator::make($data, [ |
||
67 | 'permissions' => 'required|array', |
||
68 | 'email' => 'required|email', |
||
69 | ]); |
||
70 | |||
71 | if ($validator->fails()) { |
||
72 | throw new DisplayValidationException(json_encode($validator->errors())); |
||
73 | } |
||
74 | |||
75 | DB::beginTransaction(); |
||
76 | |||
77 | try { |
||
78 | // Determine if this user exists or if we need to make them an account. |
||
79 | $user = User::where('email', $data['email'])->first(); |
||
80 | if (! $user) { |
||
81 | try { |
||
82 | $repo = new UserRepository; |
||
83 | $user = $repo->create([ |
||
84 | 'email' => $data['email'], |
||
85 | 'username' => str_random(8), |
||
86 | 'name_first' => 'Unassigned', |
||
87 | 'name_last' => 'Name', |
||
88 | 'root_admin' => false, |
||
89 | ]); |
||
90 | } catch (\Exception $ex) { |
||
91 | throw $ex; |
||
92 | } |
||
93 | } elseif ($server->owner_id === $user->id) { |
||
94 | throw new DisplayException('You cannot add the owner of a server as a subuser.'); |
||
95 | } elseif (Subuser::select('id')->where('user_id', $user->id)->where('server_id', $server->id)->first()) { |
||
96 | throw new DisplayException('A subuser with that email already exists for this server.'); |
||
97 | } |
||
98 | |||
99 | $uuid = new UuidService; |
||
100 | $subuser = Subuser::create([ |
||
101 | 'user_id' => $user->id, |
||
102 | 'server_id' => $server->id, |
||
103 | 'daemonSecret' => (string) $uuid->generate('servers', 'uuid'), |
||
104 | ]); |
||
105 | |||
106 | $perms = Permission::listPermissions(true); |
||
107 | $daemonPermissions = $this->coreDaemonPermissions; |
||
108 | |||
109 | View Code Duplication | foreach ($data['permissions'] as $permission) { |
|
110 | if (array_key_exists($permission, $perms)) { |
||
111 | // Build the daemon permissions array for sending. |
||
112 | if (! is_null($perms[$permission])) { |
||
113 | array_push($daemonPermissions, $perms[$permission]); |
||
114 | } |
||
115 | |||
116 | Permission::create([ |
||
117 | 'subuser_id' => $subuser->id, |
||
118 | 'permission' => $permission, |
||
119 | ]); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | // Contact Daemon |
||
124 | // We contact even if they don't have any daemon permissions to overwrite |
||
125 | // if they did have them previously. |
||
126 | |||
127 | $server->node->guzzleClient([ |
||
128 | 'X-Access-Server' => $server->uuid, |
||
129 | 'X-Access-Token' => $server->node->daemonSecret, |
||
130 | ])->request('PATCH', '/server', [ |
||
131 | 'json' => [ |
||
132 | 'keys' => [ |
||
133 | $subuser->daemonSecret => $daemonPermissions, |
||
134 | ], |
||
135 | ], |
||
136 | ]); |
||
137 | |||
138 | DB::commit(); |
||
139 | |||
140 | return $subuser; |
||
141 | } catch (TransferException $ex) { |
||
142 | DB::rollBack(); |
||
143 | throw new DisplayException('There was an error attempting to connect to the daemon to add this user.', $ex); |
||
144 | } catch (\Exception $ex) { |
||
145 | DB::rollBack(); |
||
146 | throw $ex; |
||
147 | } |
||
148 | |||
149 | return false; |
||
150 | } |
||
151 | |||
265 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: