| Conditions | 13 |
| Paths | 276 |
| Total Lines | 101 |
| Code Lines | 55 |
| 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 |
||
| 247 | public function applyForJob($jobPostId, $test = false) |
||
| 248 | { |
||
| 249 | $request = Craft::$app->getRequest(); |
||
| 250 | $postUrl = sprintf('postings/%s/%s?key=%s', |
||
| 251 | $this->settings->site, |
||
| 252 | $jobPostId, |
||
| 253 | $this->settings->apiKey |
||
| 254 | ); |
||
| 255 | |||
| 256 | if ($test) |
||
| 257 | { |
||
| 258 | // reconfigure client for testing |
||
| 259 | $this->_client = new Client([ |
||
| 260 | 'base_uri' => UrlHelper::baseUrl(), |
||
| 261 | 'headers' => [ |
||
| 262 | 'Content-Type' => 'application/json; charset=utf-8', |
||
| 263 | 'Accept' => 'application/json' |
||
| 264 | ], |
||
| 265 | 'verify' => false, |
||
| 266 | 'debug' => false |
||
| 267 | ]); |
||
| 268 | |||
| 269 | // https://site.local/actions/lever/apply/test |
||
| 270 | $postUrl = UrlHelper::actionUrl('lever/apply/test'); |
||
| 271 | } |
||
| 272 | |||
| 273 | $resumeIncluded = ! empty($_FILES['resume']['tmp_name']) |
||
| 274 | && ! empty($_FILES['resume']['name']); |
||
| 275 | |||
| 276 | if ( ! $application = new LeverJobApplication([ |
||
| 277 | 'name' => $request->getParam('name'), |
||
| 278 | 'email' => $request->getParam('email'), |
||
| 279 | 'phone' => $request->getParam('phone'), |
||
| 280 | 'org' => $request->getParam('org'), |
||
| 281 | 'urls' => $request->getParam('urls'), |
||
| 282 | 'comments' => $request->getParam('comments'), |
||
| 283 | 'ip' => $request->getUserIP(), |
||
| 284 | 'silent' => $this->settings->applySilently, |
||
| 285 | 'source' => $this->settings->applicationSource, |
||
| 286 | ])) |
||
| 287 | { |
||
| 288 | array_merge($this->errors, $application->getErrors()); |
||
| 289 | return false; |
||
| 290 | } |
||
| 291 | |||
| 292 | if ($resumeIncluded) |
||
| 293 | { |
||
| 294 | $application->resume = UploadedFile::getInstanceByName('resume'); |
||
| 295 | } |
||
| 296 | |||
| 297 | $event = new ApplyEvent([ 'application' => $application ]); |
||
| 298 | |||
| 299 | if ($this->hasEventHandlers(self::EVENT_BEFORE_VALIDATE_APPLICATION)) |
||
| 300 | { |
||
| 301 | $this->trigger(self::EVENT_BEFORE_VALIDATE_APPLICATION, $event); |
||
| 302 | $application = $event->application; |
||
| 303 | } |
||
| 304 | |||
| 305 | if ( ! $application->validate()) |
||
| 306 | { |
||
| 307 | $this->errors = array_merge($this->errors, $application->getErrors()); |
||
| 308 | return false; |
||
| 309 | } |
||
| 310 | |||
| 311 | if ($this->hasEventHandlers(self::EVENT_BEFORE_SEND_APPLICATION)) |
||
| 312 | { |
||
| 313 | $this->trigger(self::EVENT_BEFORE_SEND_APPLICATION, $event); |
||
| 314 | } |
||
| 315 | |||
| 316 | if ($event->isSpam) |
||
| 317 | { |
||
| 318 | Craft::info('Spammy job application ignored.', 'lever'); |
||
| 319 | |||
| 320 | // pretend it's fine so they feel good about themselves |
||
| 321 | return true; |
||
| 322 | } |
||
| 323 | |||
| 324 | if ($response = $this->getClient()->post( |
||
| 325 | $postUrl, |
||
| 326 | [ 'multipart' => $application->toMultiPartPostData() ] |
||
| 327 | )) |
||
| 328 | { |
||
| 329 | $responseIsHealthy = $response->getStatusCode() === 200 && |
||
| 330 | isset($response->getBody()->applicationId); |
||
|
|
|||
| 331 | |||
| 332 | if ($responseIsHealthy) |
||
| 333 | { |
||
| 334 | if ($this->hasEventHandlers(self::EVENT_AFTER_SEND_APPLICATION)) |
||
| 335 | { |
||
| 336 | $this->trigger(self::EVENT_AFTER_SEND_APPLICATION, $event); |
||
| 337 | } |
||
| 338 | |||
| 339 | return true; |
||
| 340 | } |
||
| 341 | |||
| 342 | $this->errors[] = Craft::t('lever', 'Your application could not be submitted.'); |
||
| 343 | return false; |
||
| 344 | } |
||
| 345 | |||
| 346 | $this->errors[] = Craft::t('lever', 'There was a problem submitting your application.'); |
||
| 347 | return false; |
||
| 348 | } |
||
| 349 | } |