Conditions | 17 |
Paths | 112 |
Total Lines | 91 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
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 |
||
102 | public function update($id, array $data) |
||
103 | { |
||
104 | $node = Models\Node::findOrFail($id); |
||
105 | |||
106 | // Validate Fields |
||
107 | $validator = $validator = Validator::make($data, [ |
||
108 | 'name' => 'regex:/^([\w .-]{1,100})$/', |
||
109 | 'location_id' => 'numeric|min:1|exists:locations,id', |
||
110 | 'public' => 'numeric|between:0,1', |
||
111 | 'fqdn' => 'string|unique:nodes,fqdn,' . $id, |
||
112 | 'scheme' => 'regex:/^(http(s)?)$/', |
||
113 | 'behind_proxy' => 'boolean', |
||
114 | 'memory' => 'numeric|min:1', |
||
115 | 'memory_overallocate' => 'numeric|min:-1', |
||
116 | 'disk' => 'numeric|min:1', |
||
117 | 'disk_overallocate' => 'numeric|min:-1', |
||
118 | 'upload_size' => 'numeric|min:0', |
||
119 | 'daemonBase' => 'sometimes|regex:/^([\/][\d\w.\-\/]+)$/', |
||
120 | 'daemonSFTP' => 'numeric|between:1,65535', |
||
121 | 'daemonListen' => 'numeric|between:1,65535', |
||
122 | 'reset_secret' => 'sometimes|nullable|accepted', |
||
123 | ]); |
||
124 | |||
125 | // Run validator, throw catchable and displayable exception if it fails. |
||
126 | // Exception includes a JSON result of failed validation rules. |
||
127 | if ($validator->fails()) { |
||
128 | throw new DisplayValidationException(json_encode($validator->errors())); |
||
129 | } |
||
130 | |||
131 | // Verify the FQDN |
||
132 | if (isset($data['fqdn'])) { |
||
133 | |||
134 | // Verify the FQDN if using SSL |
||
135 | if ((isset($data['scheme']) && $data['scheme'] === 'https') || (! isset($data['scheme']) && $node->scheme === 'https')) { |
||
136 | if (filter_var($data['fqdn'], FILTER_VALIDATE_IP)) { |
||
137 | throw new DisplayException('A fully qualified domain name is required to use secure comunication on this node.'); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // Verify FQDN is resolvable, or if not using SSL that the IP is valid. |
||
142 | if (! filter_var(gethostbyname($data['fqdn']), FILTER_VALIDATE_IP)) { |
||
143 | throw new DisplayException('The FQDN (or IP Address) provided does not resolve to a valid IP address.'); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | // Should we be nulling the overallocations? |
||
148 | if (isset($data['memory_overallocate'])) { |
||
149 | $data['memory_overallocate'] = ($data['memory_overallocate'] < 0) ? null : $data['memory_overallocate']; |
||
150 | } |
||
151 | |||
152 | if (isset($data['disk_overallocate'])) { |
||
153 | $data['disk_overallocate'] = ($data['disk_overallocate'] < 0) ? null : $data['disk_overallocate']; |
||
154 | } |
||
155 | |||
156 | // Set the Secret |
||
157 | if (isset($data['reset_secret']) && ! is_null($data['reset_secret'])) { |
||
158 | $uuid = new UuidService; |
||
159 | $data['daemonSecret'] = (string) $uuid->generate('nodes', 'daemonSecret'); |
||
160 | unset($data['reset_secret']); |
||
161 | } |
||
162 | |||
163 | $oldDaemonKey = $node->daemonSecret; |
||
164 | $node->update($data); |
||
165 | try { |
||
166 | $node->guzzleClient(['X-Access-Token' => $oldDaemonKey])->request('PATCH', '/config', [ |
||
167 | 'json' => [ |
||
168 | 'web' => [ |
||
169 | 'listen' => $node->daemonListen, |
||
170 | 'ssl' => [ |
||
171 | 'enabled' => (! $node->behind_proxy && $node->scheme === 'https'), |
||
172 | ], |
||
173 | ], |
||
174 | 'sftp' => [ |
||
175 | 'path' => $node->daemonBase, |
||
176 | 'port' => $node->daemonSFTP, |
||
177 | ], |
||
178 | 'remote' => [ |
||
179 | 'base' => config('app.url'), |
||
180 | ], |
||
181 | 'uploads' => [ |
||
182 | 'size_limit' => $node->upload_size, |
||
183 | ], |
||
184 | 'keys' => [ |
||
185 | $node->daemonSecret, |
||
186 | ], |
||
187 | ], |
||
188 | ]); |
||
189 | } catch (\Exception $ex) { |
||
190 | throw new DisplayException('Failed to update the node configuration, however your changes have been saved to the database. You will need to manually update the configuration file for the node to apply these changes.'); |
||
191 | } |
||
192 | } |
||
193 | |||
292 |