Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Feb 7, 2024
1 parent dda628b commit a1a134c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<?php

/*
* This file is part of fof/oauth.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\OAuth\Providers\Custom\LinkedIn\Provider\Exception;

class LinkedInAccessDeniedException extends \League\OAuth2\Client\Provider\Exception\IdentityProviderException
{

}
56 changes: 36 additions & 20 deletions src/Providers/Custom/LinkedIn/Provider/LinkedIn.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<?php

/*
* This file is part of fof/oauth.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\OAuth\Providers\Custom\LinkedIn\Provider;

use Exception;
use FoF\OAuth\Providers\Custom\LinkedIn\Provider\Exception\LinkedInAccessDeniedException;
use FoF\OAuth\Providers\Custom\LinkedIn\Token\LinkedInAccessToken;
use InvalidArgumentException;
Expand All @@ -17,32 +25,33 @@ class LinkedIn extends \League\OAuth2\Client\Provider\AbstractProvider
use BearerAuthorizationTrait;

/**
* Default scopes
* Default scopes.
*
* @var array
*/
public $defaultScopes = ['openid', 'profile', 'email'];

/**
* Requested fields in scope, seeded with default values
* Requested fields in scope, seeded with default values.
*
* @var array
*
* @see https://developer.linkedin.com/docs/fields/basic-profile
*/
protected $fields = [
'sub', 'name', 'given_name', 'family_name', 'picture', 'locale', 'email', 'email_verified'
'sub', 'name', 'given_name', 'family_name', 'picture', 'locale', 'email', 'email_verified',
];

/**
* Constructs an OAuth 2.0 service provider.
*
* @param array $options An array of options to set on this provider.
* Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
* Individual providers may introduce more options, as needed.
* @param array $options An array of options to set on this provider.
* Options include `clientId`, `clientSecret`, `redirectUri`, and `state`.
* Individual providers may introduce more options, as needed.
* @param array $collaborators An array of collaborators that may be used to
* override this provider's default behavior. Collaborators include
* `grantFactory`, `requestFactory`, and `httpClient`.
* Individual providers may introduce more collaborators, as needed.
* override this provider's default behavior. Collaborators include
* `grantFactory`, `requestFactory`, and `httpClient`.
* Individual providers may introduce more collaborators, as needed.
*/
public function __construct(array $options = [], array $collaborators = [])
{
Expand All @@ -53,15 +62,15 @@ public function __construct(array $options = [], array $collaborators = [])
parent::__construct($options, $collaborators);
}


/**
* Creates an access token from a response.
*
* The grant that was used to fetch the response can be used to provide
* additional context.
*
* @param array $response
* @param array $response
* @param AbstractGrant $grant
*
* @return AccessTokenInterface
*/
protected function createAccessToken(array $response, AbstractGrant $grant)

Check failure on line 76 in src/Providers/Custom/LinkedIn/Provider/LinkedIn.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.3

Method FoF\OAuth\Providers\Custom\LinkedIn\Provider\LinkedIn::createAccessToken() has invalid return type FoF\OAuth\Providers\Custom\LinkedIn\Provider\AccessTokenInterface.

Check failure on line 76 in src/Providers/Custom/LinkedIn/Provider/LinkedIn.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

Method FoF\OAuth\Providers\Custom\LinkedIn\Provider\LinkedIn::createAccessToken() has invalid return type FoF\OAuth\Providers\Custom\LinkedIn\Provider\AccessTokenInterface.

Check failure on line 76 in src/Providers/Custom/LinkedIn/Provider/LinkedIn.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

Method FoF\OAuth\Providers\Custom\LinkedIn\Provider\LinkedIn::createAccessToken() has invalid return type FoF\OAuth\Providers\Custom\LinkedIn\Provider\AccessTokenInterface.

Check failure on line 76 in src/Providers/Custom/LinkedIn/Provider/LinkedIn.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

Method FoF\OAuth\Providers\Custom\LinkedIn\Provider\LinkedIn::createAccessToken() has invalid return type FoF\OAuth\Providers\Custom\LinkedIn\Provider\AccessTokenInterface.

Check failure on line 76 in src/Providers/Custom/LinkedIn/Provider/LinkedIn.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

Method FoF\OAuth\Providers\Custom\LinkedIn\Provider\LinkedIn::createAccessToken() has invalid return type FoF\OAuth\Providers\Custom\LinkedIn\Provider\AccessTokenInterface.
Expand All @@ -87,7 +96,7 @@ protected function getScopeSeparator()
}

/**
* Get authorization url to begin OAuth flow
* Get authorization url to begin OAuth flow.
*
* @return string
*/
Expand All @@ -97,7 +106,7 @@ public function getBaseAuthorizationUrl()
}

/**
* Get access token url to retrieve token
* Get access token url to retrieve token.
*
* @return string
*/
Expand All @@ -107,7 +116,7 @@ public function getBaseAccessTokenUrl(array $params)
}

/**
* Get provider url to fetch user details
* Get provider url to fetch user details.
*
* @param AccessToken $token
*
Expand Down Expand Up @@ -135,9 +144,12 @@ protected function getDefaultScopes()
* Check a provider response for errors.
*
* @param ResponseInterface $response
* @param array $data Parsed response data
* @return void
* @param array $data Parsed response data
*
* @throws IdentityProviderException
*
* @return void
*
* @see https://developer.linkedin.com/docs/guide/v2/error-handling
*/
protected function checkResponse(ResponseInterface $response, $data)
Expand All @@ -157,9 +169,12 @@ protected function checkResponse(ResponseInterface $response, $data)
* Check a provider response for unauthorized errors.
*
* @param ResponseInterface $response
* @param array $data Parsed response data
* @return void
* @param array $data Parsed response data
*
* @throws LinkedInAccessDeniedException
*
* @return void
*
* @see https://developer.linkedin.com/docs/guide/v2/error-handling
*/
protected function checkResponseUnauthorized(ResponseInterface $response, $data)
Expand All @@ -176,8 +191,9 @@ protected function checkResponseUnauthorized(ResponseInterface $response, $data)
/**
* Generate a user object from a successful user details request.
*
* @param array $response
* @param array $response
* @param AccessToken $token
*
* @return LinkedInResourceOwner
*/
protected function createResourceOwner(array $response, AccessToken $token)
Expand Down
50 changes: 30 additions & 20 deletions src/Providers/Custom/LinkedIn/Provider/LinkedInResourceOwner.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
<?php
<?php

/*
* This file is part of fof/oauth.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\OAuth\Providers\Custom\LinkedIn\Provider;

use League\OAuth2\Client\Tool\ArrayAccessorTrait;

/**
* @property array $response
* @property array $response
* @property string $uid
*/
class LinkedInResourceOwner extends \League\OAuth2\Client\Provider\GenericResourceOwner
{

use ArrayAccessorTrait;

/**
* Raw response
* Raw response.
*
* @var array
*/
protected $response = [];

/**
* Sorted profile pictures
* Sorted profile pictures.
*
* @var array
*/
Expand All @@ -35,9 +43,9 @@ class LinkedInResourceOwner extends \League\OAuth2\Client\Provider\GenericResour
/**
* Creates new resource owner.
*
* @param array $response
* @param array $response
*/
public function __construct(array $response = array())
public function __construct(array $response = [])
{
$this->response = $response;
$this->setSortedProfilePictures();
Expand All @@ -54,7 +62,7 @@ public function getAttribute($key)
}

/**
* Get user first name
* Get user first name.
*
* @return string|null
*/
Expand All @@ -64,7 +72,7 @@ public function getFirstName()
}

/**
* Get user user id
* Get user user id.
*
* @return string|null
*/
Expand All @@ -74,9 +82,10 @@ public function getId()
}

/**
* Get specific image by size
* Get specific image by size.
*
* @param int $size
*
* @param integer $size
* @return array|null
*/
public function getImageBySize($size)
Expand All @@ -89,7 +98,7 @@ public function getImageBySize($size)
}

/**
* Get available user image sizes
* Get available user image sizes.
*
* @return array
*/
Expand All @@ -101,7 +110,7 @@ public function getImageSizes()
}

/**
* Get user image url
* Get user image url.
*
* @return string|null
*/
Expand All @@ -114,7 +123,7 @@ public function getImageUrl()
}

/**
* Get user last name
* Get user last name.
*
* @return string|null
*/
Expand All @@ -134,7 +143,7 @@ public function getSortedProfilePictures()
}

/**
* Get user url
* Get user url.
*
* @return string|null
*/
Expand All @@ -146,7 +155,7 @@ public function getUrl()
}

/**
* Get user email, if available
* Get user email, if available.
*
* @return string|null
*/
Expand All @@ -170,23 +179,24 @@ private function setSortedProfilePictures()
return
isset($element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage'])
&& strtoupper($element['authorizationMethod']) === 'PUBLIC'
&& isset($element['identifiers'][0]['identifier'])
;
&& isset($element['identifiers'][0]['identifier']);
});
// order images by width, LinkedIn profile pictures are always squares, so that should be good enough
usort($pictures, function ($elementA, $elementB) {
$wA = $elementA['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];
$wB = $elementB['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];

return $wA - $wB;
});
$pictures = array_map(function ($element) {
// this is an URL, no idea how many of identifiers there can be, so take the first one.
$url = $element['identifiers'][0]['identifier'];
$type = $element['identifiers'][0]['mediaType'];
$width = $element['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'];

return [
'width' => $width,
'url' => $url,
'width' => $width,
'url' => $url,
'contentType' => $type,
];
}, $pictures);
Expand Down
14 changes: 12 additions & 2 deletions src/Providers/Custom/LinkedIn/Token/LinkedInAccessToken.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of fof/oauth.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\OAuth\Providers\Custom\LinkedIn\Token;

class LinkedInAccessToken extends \League\OAuth2\Client\Token\AccessToken
Expand All @@ -13,7 +22,8 @@ class LinkedInAccessToken extends \League\OAuth2\Client\Token\AccessToken
* Constructs an access token.
*
* @param array $options An array of options returned by the service provider
* in the access token request. The `access_token` option is required.
* in the access token request. The `access_token` option is required.
*
* @throws InvalidArgumentException if `access_token` is not provided in `$options`.
*/
public function __construct(array $options = [])

Check failure on line 29 in src/Providers/Custom/LinkedIn/Token/LinkedInAccessToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.3

PHPDoc tag @throws with type FoF\OAuth\Providers\Custom\LinkedIn\Token\InvalidArgumentException is not subtype of Throwable

Check failure on line 29 in src/Providers/Custom/LinkedIn/Token/LinkedInAccessToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

PHPDoc tag @throws with type FoF\OAuth\Providers\Custom\LinkedIn\Token\InvalidArgumentException is not subtype of Throwable

Check failure on line 29 in src/Providers/Custom/LinkedIn/Token/LinkedInAccessToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

PHPDoc tag @throws with type FoF\OAuth\Providers\Custom\LinkedIn\Token\InvalidArgumentException is not subtype of Throwable

Check failure on line 29 in src/Providers/Custom/LinkedIn/Token/LinkedInAccessToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

PHPDoc tag @throws with type FoF\OAuth\Providers\Custom\LinkedIn\Token\InvalidArgumentException is not subtype of Throwable

Check failure on line 29 in src/Providers/Custom/LinkedIn/Token/LinkedInAccessToken.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

PHPDoc tag @throws with type FoF\OAuth\Providers\Custom\LinkedIn\Token\InvalidArgumentException is not subtype of Throwable
Expand All @@ -32,7 +42,7 @@ public function __construct(array $options = [])
/**
* Returns the refresh token expiration timestamp, if defined.
*
* @return integer|null
* @return int|null
*/
public function getRefreshTokenExpires()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Flarum\Forum\Auth\Registration;
use Flarum\Settings\SettingsRepositoryInterface;
use FoF\OAuth\Provider;
use League\OAuth2\Client\Provider\AbstractProvider;
use FoF\OAuth\Providers\Custom\LinkedIn\Provider\LinkedIn as LinkedInProvider;
use League\OAuth2\Client\Provider\AbstractProvider;

class LinkedIn extends Provider
{
Expand Down

0 comments on commit a1a134c

Please sign in to comment.