| Conditions | 10 |
| Paths | 68 |
| Total Lines | 76 |
| Code Lines | 38 |
| 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 |
||
| 242 | public function applyForJob($jobPostId, $jobApplication, $test = false) |
||
| 243 | { |
||
| 244 | $postUrl = sprintf('postings/%s/%s?key=%s', |
||
| 245 | $this->settings->site, |
||
| 246 | $jobPostId, |
||
| 247 | $this->settings->apiKey |
||
| 248 | ); |
||
| 249 | |||
| 250 | if ($test) |
||
| 251 | { |
||
| 252 | // reconfigure client for testing |
||
| 253 | $this->_client = new Client([ |
||
| 254 | 'base_uri' => UrlHelper::baseUrl(), |
||
| 255 | 'headers' => [ |
||
| 256 | 'Content-Type' => 'application/json; charset=utf-8', |
||
| 257 | 'Accept' => 'application/json' |
||
| 258 | ], |
||
| 259 | 'verify' => false, |
||
| 260 | 'debug' => false |
||
| 261 | ]); |
||
| 262 | |||
| 263 | // https://site.local/actions/lever/apply/test |
||
| 264 | $postUrl = UrlHelper::actionUrl('lever/apply/test'); |
||
| 265 | } |
||
| 266 | |||
| 267 | $event = new ApplyEvent([ 'application' => $jobApplication ]); |
||
| 268 | |||
| 269 | if ($this->hasEventHandlers(self::EVENT_BEFORE_VALIDATE_APPLICATION)) |
||
| 270 | { |
||
| 271 | $this->trigger(self::EVENT_BEFORE_VALIDATE_APPLICATION, $event); |
||
| 272 | $jobApplication = $event->application; |
||
| 273 | } |
||
| 274 | |||
| 275 | if ( ! $jobApplication->validate()) |
||
| 276 | { |
||
| 277 | Craft::info('Invalid job application.', 'lever'); |
||
| 278 | return false; |
||
| 279 | } |
||
| 280 | |||
| 281 | if ($this->hasEventHandlers(self::EVENT_BEFORE_SEND_APPLICATION)) |
||
| 282 | { |
||
| 283 | $this->trigger(self::EVENT_BEFORE_SEND_APPLICATION, $event); |
||
| 284 | } |
||
| 285 | |||
| 286 | if ($event->isSpam) |
||
| 287 | { |
||
| 288 | Craft::info('Spammy job application ignored.', 'lever'); |
||
| 289 | |||
| 290 | // pretend it's fine so they feel good about themselves |
||
| 291 | return true; |
||
| 292 | } |
||
| 293 | |||
| 294 | if ($response = $this->getClient()->post( |
||
| 295 | $postUrl, |
||
| 296 | [ 'multipart' => $jobApplication->toMultiPartPostData() ] |
||
| 297 | )) |
||
| 298 | { |
||
| 299 | $responseIsHealthy = $response->getStatusCode() === 200 && |
||
| 300 | isset($response->getBody()->applicationId); |
||
|
|
|||
| 301 | |||
| 302 | if ($responseIsHealthy) |
||
| 303 | { |
||
| 304 | if ($this->hasEventHandlers(self::EVENT_AFTER_SEND_APPLICATION)) |
||
| 305 | { |
||
| 306 | $this->trigger(self::EVENT_AFTER_SEND_APPLICATION, $event); |
||
| 307 | } |
||
| 308 | |||
| 309 | return true; |
||
| 310 | } |
||
| 311 | |||
| 312 | Craft::info('Application may not have been submitted.', 'lever'); |
||
| 313 | return false; |
||
| 314 | } |
||
| 315 | |||
| 316 | Craft::info('Application could not be sent.', 'lever'); |
||
| 317 | return false; |
||
| 318 | } |
||
| 319 | } |