Skip to content

Commit

Permalink
Merge pull request #47 from Nyholm/cs-fixes
Browse files Browse the repository at this point in the history
scrutinizer fixes
  • Loading branch information
Nyholm committed May 6, 2015
2 parents 7dc9df0 + e708b72 commit 93049c4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Exceptions/LinkedInApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LinkedInApiException extends \Exception
/**
* Make a new API Exception with the given result.
*
* @param array $result The result from the API server
* @param mixed $result The result from the API server
*/
public function __construct($result)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Http/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class CurlRequest implements RequestInterface
{
/**
* @var array lastHeaders
* @var mixed lastHeaders
*/
protected $lastHeaders;

Expand Down
2 changes: 1 addition & 1 deletion src/Http/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getUrl($name, $path = '', $params = array())
$url .= $path;
}

if ($params) {
if (!empty($params)) {
//it needs to be PHP_QUERY_RFC3986. We want to have %20 between scopes
// we cant run http_build_query($params, null, '&', PHP_QUERY_RFC3986); because it is not supported in php 5.3 or hhvm
$url .= '?';
Expand Down
77 changes: 44 additions & 33 deletions src/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class LinkedIn
/**
* An array with default user stuff.
*
* @var array
* @var mixed
*/
protected $user;

Expand Down Expand Up @@ -128,38 +128,17 @@ public function isAuthenticated()
*/
public function api($method, $resource, array $options = array())
{
/*
* Add token and format
*/
// Add access token to the headers
$options['headers']['Authorization'] = sprintf('Bearer %s', (string) $this->getAccessToken());
if (isset($options['json'])) {
$options['format'] = 'json';
} elseif (!isset($options['format'])) {
$options['format'] = $this->getFormat();
}

// Set correct headers for this format
switch ($options['format']) {
case 'simple_xml':
$options['simple_xml'] = true;
case 'xml':
$options['headers']['Content-Type'] = 'text/xml';
break;
case 'json':
$options['headers']['Content-Type'] = 'application/json';
$options['headers']['x-li-format'] = 'json';
$options['query']['format'] = 'json';
break;
default:
// Do nothing
}
unset($options['format']);
// Do logic and adjustments to the options
$this->filterRequestOption($options);

//generate an url
$url = $this->getUrlGenerator()->getUrl('api', $resource, isset($options['query'])?$options['query']:array());
// Generate an url
$url = $this->getUrlGenerator()->getUrl('api', $resource, isset($options['query']) ? $options['query'] : array());
unset($options['query']);

//$method that url
// $method that url
$result = $this->getRequest()->send($method, $url, $options);

return $result;
Expand Down Expand Up @@ -272,9 +251,9 @@ protected function getUserFromAvailableData()
* 1: We got an access token
* 2: The access token has changed or if we don't got a user.
*/
if ($accessToken && !($user && $persistedAccessToken == $accessToken)) {
if ($accessToken && !($user !== null && $persistedAccessToken == $accessToken)) {
$user = $this->getUserFromAccessToken();
if ($user) {
if ($user !== null) {
$storage->set('user', $user);
} else {
$storage->clearAll();
Expand Down Expand Up @@ -360,7 +339,7 @@ public function getAccessToken()
}

$newAccessToken = $this->fetchNewAccessToken();
if ($newAccessToken) {
if ($newAccessToken !== null) {
$this->setAccessToken($newAccessToken);
}

Expand All @@ -372,7 +351,7 @@ public function getAccessToken()
* Determines and returns the user access token using the authorization code. The intent is to
* return a valid user access token, or null if one is determined to not be available.
*
* @return string|null A valid user access token, or null if one could not be determined.
* @return string|AccessToken|null A valid user access token, or null if one could not be determined.
*
* @throws LinkedInApiException
*/
Expand All @@ -381,7 +360,7 @@ protected function fetchNewAccessToken()
$storage = $this->getStorage();
$code = $this->getCode();

if ($code) {
if ($code !== null) {
$accessToken = $this->getAccessTokenFromCode($code);
if ($accessToken) {
$storage->set('code', $code);
Expand Down Expand Up @@ -680,4 +659,36 @@ public function getLastHeaders()
{
return $this->getRequest()->getHeadersFromLastResponse();
}

/**
* Modify and filter the request options. Make sure we use the correct query parameters and headers.
*
* @param array $options
*/
protected function filterRequestOption(array &$options)
{
if (isset($options['json'])) {
$options['format'] = 'json';
} elseif (!isset($options['format'])) {
$options['format'] = $this->getFormat();
}

// Set correct headers for this format
switch ($options['format']) {
case 'simple_xml':
$options['simple_xml'] = true;
// simple_xml is still xml. This should fall through
case 'xml':
$options['headers']['Content-Type'] = 'text/xml';
break;
case 'json':
$options['headers']['Content-Type'] = 'application/json';
$options['headers']['x-li-format'] = 'json';
$options['query']['format'] = 'json';
break;
default:
// Do nothing
}
unset($options['format']);
}
}
4 changes: 2 additions & 2 deletions src/Storage/DataStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ interface DataStorageInterface
* getPersistentData($key) return $value. This call may be in another request.
*
* @param string $key
* @param array $value
* @param mixed $value
*/
public function set($key, $value);

/**
* Get the data for $key, persisted by BaseFacebook::setPersistentData().
*
* @param string $key The key of the data to retrieve
* @param boolean $default The default value to return if $key is not found
* @param mixed $default The default value to return if $key is not found
*
* @return mixed
*/
Expand Down

0 comments on commit 93049c4

Please sign in to comment.