Conditions | 10 |
Paths | 68 |
Total Lines | 83 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
227 | public function applyForJob($jobPostId, $jobApplication, $test = false): bool |
||
228 | { |
||
229 | $postUrl = sprintf('postings/%s/%s?key=%s', |
||
230 | $this->settings->site, |
||
231 | $jobPostId, |
||
232 | $this->settings->apiKey |
||
233 | ); |
||
234 | |||
235 | if ($test) |
||
236 | { |
||
237 | // reconfigure client for testing |
||
238 | $this->_client = new Client([ |
||
239 | 'base_uri' => UrlHelper::baseUrl(), |
||
240 | 'headers' => [ |
||
241 | 'Content-Type' => 'application/json; charset=utf-8', |
||
242 | 'Accept' => 'application/json' |
||
243 | ], |
||
244 | 'verify' => false, |
||
245 | 'debug' => false |
||
246 | ]); |
||
247 | |||
248 | // https://site.local/actions/lever/apply/test |
||
249 | $postUrl = UrlHelper::actionUrl('lever/apply/test'); |
||
250 | } |
||
251 | |||
252 | $event = new ApplyEvent([ 'application' => $jobApplication ]); |
||
253 | |||
254 | if ($this->hasEventHandlers(self::EVENT_BEFORE_VALIDATE_APPLICATION)) |
||
255 | { |
||
256 | $this->trigger(self::EVENT_BEFORE_VALIDATE_APPLICATION, $event); |
||
257 | $jobApplication = $event->application; |
||
258 | } |
||
259 | |||
260 | if ( ! $jobApplication->validate()) |
||
261 | { |
||
262 | Craft::info('Invalid job application.', 'lever'); |
||
263 | return false; |
||
264 | } |
||
265 | |||
266 | if ($this->hasEventHandlers(self::EVENT_BEFORE_SEND_APPLICATION)) |
||
267 | { |
||
268 | $this->trigger(self::EVENT_BEFORE_SEND_APPLICATION, $event); |
||
269 | } |
||
270 | |||
271 | if ($event->isSpam) |
||
272 | { |
||
273 | Craft::info('Spammy job application ignored.', 'lever'); |
||
274 | |||
275 | // pretend it's fine so they feel good about themselves |
||
276 | return true; |
||
277 | } |
||
278 | |||
279 | if ($response = $this->getClient()->post( |
||
280 | $postUrl, |
||
281 | [ 'multipart' => $jobApplication->toMultiPartPostData() ] |
||
282 | )) |
||
283 | { |
||
284 | /** |
||
285 | * Happy submission responses include an application ID. |
||
286 | * |
||
287 | * Here, we make sure it's a 200 response *and* that Lever |
||
288 | * was able to make a proper application out of it. |
||
289 | */ |
||
290 | $responseData = json_decode($response->getBody()); |
||
291 | $responseIsHealthy = $response->getStatusCode() === 200 && |
||
292 | isset($responseData->applicationId); |
||
293 | |||
294 | if ($responseIsHealthy) |
||
295 | { |
||
296 | if ($this->hasEventHandlers(self::EVENT_AFTER_SEND_APPLICATION)) |
||
297 | { |
||
298 | $this->trigger(self::EVENT_AFTER_SEND_APPLICATION, $event); |
||
299 | } |
||
300 | |||
301 | return true; |
||
302 | } |
||
303 | |||
304 | Craft::info('Application may not have been submitted.', 'lever'); |
||
305 | return false; |
||
306 | } |
||
307 | |||
308 | Craft::info('Application could not be sent.', 'lever'); |
||
309 | return false; |
||
310 | } |
||
311 | } |