Add API base URL castomization option

This commit is contained in:
2024-05-07 12:44:10 +03:00
parent c8b7f9901a
commit 3380b4a0ae
10 changed files with 127 additions and 59 deletions

View File

@@ -1,12 +1,9 @@
<?php
declare(strict_types=1);
<?php declare(strict_types=1);
namespace Sept\OAuth2\Client\Provider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;
@@ -54,17 +51,20 @@ class SeptemberFirstProvider extends GenericProvider
const USERINFO_PATH = '/2.0/userinfo';
/**
* Undocumented function
* Constructor
*
* @param mixed[] $options
* @param object[] $collaborators
*/
public function __construct(array $options = [], array $collaborators = [])
{
$authBase = $options['authBase'] ?? static::AUTH_BASE;
$apiBase = $options['apiBase'] ?? static::API_BASE;
$defaultOptions = [
'urlAuthorize' => static::AUTH_BASE.static::AUTHORIZE_PATH,
'urlAccessToken' => static::API_BASE.static::ACCESS_TOKEN_PATH,
'urlResourceOwnerDetails' => static::API_BASE.static::USERINFO_PATH,
'urlAuthorize' => $authBase.static::AUTHORIZE_PATH,
'urlAccessToken' => $apiBase.static::ACCESS_TOKEN_PATH,
'urlResourceOwnerDetails' => $apiBase.static::USERINFO_PATH,
'scopes' => static::SCOPES_DEFAULT,
'scopeSeparator' => static::SCOPES_SEPARATOR,
];
@@ -83,7 +83,7 @@ class SeptemberFirstProvider extends GenericProvider
*/
protected function checkResponse(ResponseInterface $response, $data): void
{
if (! empty($data['error'])) {
if (isset($data['error'])) {
throw new IdentityProviderException($data['error'].': '.$data['message'], 0, $response);
}
}

View File

@@ -1,6 +1,4 @@
<?php
declare(strict_types=1);
<?php declare(strict_types=1);
namespace Sept\OAuth2\Client\Provider;
@@ -155,7 +153,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface
*/
public function getBirthday(): ?\DateTime
{
return ! empty($this->data['birthday']) ? new \DateTime($this->data['birthday']) : null;
return isset($this->data['birthday']) ? new \DateTime($this->data['birthday']) : null;
}
/**
@@ -168,7 +166,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface
*/
public function getAvatarUrl(bool $rejectDefaultAvatar = false): ?string
{
return ($rejectDefaultAvatar && $this->isDefaultAvatar()) ? null : $this->getField('avatar');
return ($rejectDefaultAvatar && ($this->isDefaultAvatar() ?? false)) ? null : $this->getField('avatar');
}
/**
@@ -182,7 +180,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface
public function getAvatarSizeUrl(int $size, int $ratioMultiplier = 1, bool $addVersion = true): ?string
{
$ratio = ($ratioMultiplier > 1) ? '@' . $ratioMultiplier . 'x' : '';
$url = static::AVATAR_BASE .'/'. $this->getId() . ($size ? '.' : '') . $size . $ratio . '.jpeg';
$url = static::AVATAR_BASE .'/'. $this->getId() . (((bool) $size)? '.' : '') . $size . $ratio . '.jpeg';
return $url . ($addVersion ? $this->getAvatarVersionQuery() : '');
}
@@ -244,7 +242,8 @@ class SeptemberFirstUser implements ResourceOwnerInterface
public function getAvatarVersionQuery(): string
{
$query = '';
if ($version = $this->getField('avatar_version')) {
$version = $this->getField('avatar_version');
if ((bool) $version) {
$query .= '?v=' . $version;
}
return $query;
@@ -321,7 +320,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface
public function getAddressID(): ?int
{
$id = $this->getField('address.id');
return $id ? (int) $id : null;
return ((bool) $id) ? ((int) $id) : null;
}
/**
@@ -553,7 +552,7 @@ class SeptemberFirstUser implements ResourceOwnerInterface
*/
public static function getFieldFromArray(string $key, ?array $array): mixed
{
if (strpos($key, '.')) { // key.subKey.subSubKey
if ((bool) strpos($key, '.')) { // key.subKey.subSubKey
list ($key, $subKey) = explode('.', $key, 2);
return isset($array[$key]) ? static::getFieldFromArray($subKey, $array[$key]) : null;
}