| Conditions | 8 |
| Paths | 11 |
| Total Lines | 81 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 25 | public function post(Post $post) |
||
| 26 | { |
||
| 27 | $group_id = $post->options['group_id']; |
||
| 28 | |||
| 29 | try { |
||
| 30 | $token = $this->service->getStorage()->retrieveAccessToken('Linkedin')->getAccessToken(); |
||
| 31 | } catch (TokenNotFoundException $e) { |
||
| 32 | throw new AuthorizationException(); |
||
| 33 | } |
||
| 34 | |||
| 35 | $path = '/groups/'.$group_id.'/posts?format=json&oauth2_access_token='.$token; |
||
| 36 | $params = [ |
||
| 37 | 'title' => $post->title, |
||
| 38 | 'summary' => '', |
||
| 39 | 'content' => [ |
||
| 40 | 'title' => $post->title . ' @', |
||
| 41 | 'submitted-url' => $post->url, |
||
| 42 | 'description' => $post->body, |
||
| 43 | ], |
||
| 44 | ]; |
||
| 45 | |||
| 46 | // Add media files, if they were sent. |
||
| 47 | if (isset($post->media) && array_key_exists(0, $post->media)) { |
||
| 48 | $params['content']['submitted-image-url'] = $post->media[0]; |
||
| 49 | } |
||
| 50 | |||
| 51 | $params = json_encode($params); |
||
| 52 | |||
| 53 | $url = 'https://api.linkedin.com/v1'.$path; |
||
| 54 | // Linkedin API requires the Content-Type header set to application/json |
||
| 55 | $options = [ |
||
| 56 | 'headers' => ['Content-Type' => 'application/json'], |
||
| 57 | 'body' => $params |
||
| 58 | ]; |
||
| 59 | |||
| 60 | $client = new Guzzle(); |
||
| 61 | try { |
||
| 62 | $result = $client->post($url, $options); |
||
| 63 | } catch (ClientException $e) { |
||
| 64 | if ($e->getCode() >= 400) { |
||
| 65 | throw new LinkedinForbiddenException($e); |
||
| 66 | } else { |
||
| 67 | throw $e; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($result->getStatusCode() > 300) { |
||
| 72 | $msg = "Error posting to Linkedin group. Error code from Linkedin: %s. Error message from Linkedin: %s"; |
||
| 73 | $msg = sprintf($msg, $result->status_code, json_decode($result->body)->message); |
||
| 74 | throw new LinkedinPostingException($msg, $result->status_code); |
||
| 75 | } |
||
| 76 | |||
| 77 | $response = new Response; |
||
| 78 | $response->setRawResponse($result); // This is already JSON. |
||
| 79 | $response->setProvider(static::$provider); |
||
| 80 | //$response->setPostId($result->getHeader('x-li-uuid')); |
||
| 81 | |||
| 82 | // As amazing as it may sound, there's a three year old bug that LinkedIn |
||
| 83 | // knows of but doesn't fix, which is simply the group posts URL is not |
||
| 84 | // returned when we create the post, and when the post endpoint is queried |
||
| 85 | // it returns a URL containing an incorrect domain: api.linkedin.com |
||
| 86 | // instead of www.linkedin.com. They acknowledge this in the "Known Issues" |
||
| 87 | // section of the groups API documentation and say the workaround is simple: |
||
| 88 | // just swap the domains. Well, thanks for nothing. Would it be so hard for |
||
| 89 | // them to return a public URL along with the response of the creation?... |
||
| 90 | // So we need to make another API call to fetch the correct URL, because |
||
| 91 | // it's not even possible to generate it manually. |
||
| 92 | |||
| 93 | // Moderated groups don't return a 'location' header, so let's skip it if that's the case. |
||
| 94 | $location = $result->getHeader('Location'); |
||
| 95 | if (!empty($location)) { |
||
| 96 | $url = $location . ':(id,site-group-post-url)?format=json&oauth2_access_token=' . $token; |
||
| 97 | $result = $client->get($url); |
||
| 98 | $json = $result->json(); |
||
| 99 | |||
| 100 | $post_url = str_replace('api.linkedin.com/v1', 'www.linkedin.com', $json['siteGroupPostUrl']); |
||
| 101 | $response->setPostUrl($post_url); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $response; |
||
| 105 | } |
||
| 106 | |||
| 140 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: