Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade PHP #15

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:7-cli
FROM php:8.1-cli

ARG COMPOSER_FLAGS="--prefer-dist --no-interaction"
ARG DEBIAN_FRONTEND=noninteractive
Expand Down
16 changes: 12 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
}
],
"require": {
"php": "^7.1",
"guzzlehttp/guzzle": "^6.0",
"php": "^8.1",
"guzzlehttp/guzzle": "^7.0",
"monolog/monolog": "^2.1"
},
"require-dev": {
"keboola/coding-standard": "^9.0",
"phpstan/phpstan": "^0.12.59",
"keboola/coding-standard": "^15.0",
"phpstan/phpstan": "^1.4",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpunit/phpunit": "^9.5"
},
Expand All @@ -31,6 +31,7 @@
"tests": "phpunit",
"phpstan": "phpstan analyse ./src ./tests --level=max --no-progress -c phpstan.neon",
"phpcs": "phpcs -n --ignore=vendor --extensions=php .",
"phpcbf": "phpcbf -n --ignore=vendor --extensions=php .",
"phplint": "parallel-lint -j 10 --exclude vendor .",
"check": [
"@phplint",
Expand All @@ -42,5 +43,12 @@
"@check",
"@tests"
]
},
"config": {
"sort-packages": true,
"optimize-autoloader": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
11 changes: 11 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<?xml version="1.0"?>
<ruleset name="Project">
<rule ref="vendor/keboola/coding-standard/src/ruleset.xml"/>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint">
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint">
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint"/>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint">
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint"/>
</rule>
</ruleset>
4 changes: 3 additions & 1 deletion src/Exception/RestApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Keboola\Google\ClientBundle\Exception;

class RestApiException extends \Exception
use Exception;

class RestApiException extends Exception
{
}
20 changes: 11 additions & 9 deletions src/Google/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(
string $clientSecret,
string $accessToken = '',
string $refreshToken = '',
?Logger $logger = null
?Logger $logger = null,
) {
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
Expand All @@ -68,7 +68,7 @@ public function __construct(
public static function createRetryMiddleware(
callable $decider,
callable $callback,
?callable $delay = null
?callable $delay = null,
): callable {
return function (callable $handler) use ($decider, $callback, $delay) {
return new RetryCallbackMiddleware($decider, $callback, $handler, $delay);
Expand All @@ -80,7 +80,7 @@ public function createRetryDecider(int $maxRetries = 5): callable
return function (
$retries,
RequestInterface $request,
?ResponseInterface $response = null
?ResponseInterface $response = null,
) use ($maxRetries) {
$decision = $this->decideRetry($retries, $maxRetries, $response);
if ($decision) {
Expand All @@ -97,7 +97,7 @@ public function createRetryCallback(): callable

return function (
RequestInterface $request,
?ResponseInterface $response = null
?ResponseInterface $response = null,
) use ($api) {
if ($response && $response->getStatusCode() === 401) {
$tokens = $api->refreshToken();
Expand All @@ -114,7 +114,7 @@ protected function getClient(string $baseUri = self::API_URI): Client
$handlerStack->push(self::createRetryMiddleware(
$this->createRetryDecider($this->maxBackoffs),
$this->createRetryCallback(),
$this->delayFn
$this->delayFn,
));

return new Client([
Expand Down Expand Up @@ -172,7 +172,7 @@ public function getAuthorizationUrl(
string $scope,
string $approvalPrompt = 'force',
string $accessType = 'offline',
string $state = ''
string $state = '',
): string {
$params = [
'response_type=code',
Expand Down Expand Up @@ -209,6 +209,7 @@ public function authorize(string $code, string $redirectUri): array
],
]);

/** @var array<string, string> $responseBody */
$responseBody = json_decode((string) $response->getBody(), true);

$this->accessToken = $responseBody['access_token'];
Expand All @@ -233,6 +234,7 @@ public function refreshToken(): array
],
]);

/** @var array<string, string> $responseBody */
$responseBody = json_decode($response->getBody()->getContents(), true);

$this->accessToken = $responseBody['access_token'];
Expand All @@ -251,7 +253,7 @@ public function request(
string $url,
string $method = 'GET',
array $addHeaders = [],
array $options = []
array $options = [],
): Response {
$method = strtolower($method);
if (!in_array($method, ['get', 'head', 'post', 'put', 'patch', 'delete', 'options'])) {
Expand Down Expand Up @@ -279,7 +281,7 @@ public function request(
protected function logRetryRequest(
int $retries,
RequestInterface $request,
?ResponseInterface $response = null
?ResponseInterface $response = null,
): void {
if ($this->logger !== null) {
$headersForLog = $request->getHeaders();
Expand All @@ -305,7 +307,7 @@ protected function logRetryRequest(

$this->logger->info(
sprintf('Retrying request (%sx) - reason: %s', $retries, $response->getReasonPhrase()),
$context
$context,
);
} else {
$this->logger->info(sprintf('Retrying request (%sx)', $retries), $context);
Expand Down
8 changes: 4 additions & 4 deletions src/Guzzle/RetryCallbackMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
callable $decider,
callable $callback,
callable $nextHandler,
?callable $delay = null
?callable $delay = null,
) {
$this->decider = $decider;
$this->callback = $callback;
Expand Down Expand Up @@ -78,7 +78,7 @@ public function __invoke(RequestInterface $request, array $options): PromiseInte
return $fn($request, $options)
->then(
$this->onFulfilled($request, $options),
$this->onRejected($request, $options)
$this->onRejected($request, $options),
);
}

Expand All @@ -90,7 +90,7 @@ private function onFulfilled(RequestInterface $request, array $options): callabl
$options['retries'],
$request,
$response,
null
null,
)) {
return $response;
}
Expand All @@ -109,7 +109,7 @@ private function onRejected(RequestInterface $req, array $options): callable
$options['retries'],
$req,
null,
$reason
$reason,
)) {
return new RejectedPromise($reason);
}
Expand Down
15 changes: 9 additions & 6 deletions tests/RestApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Keboola\Google\ClientBundle\Tests;

use Exception;
use GuzzleHttp\Exception\ClientException;
use Keboola\Google\ClientBundle\Google\RestApi;
use Monolog\Handler\TestHandler;
Expand Down Expand Up @@ -42,7 +43,7 @@ protected function initApi(): RestApi
$this->clientSecret,
'',
$this->refreshToken,
$this->logger
$this->logger,
);
}

Expand Down Expand Up @@ -76,6 +77,7 @@ public function testRequest(): void
$restApi = $this->initApi();
$response = $restApi->request('/oauth2/v3/userinfo');
$body = json_decode($response->getBody()->getContents(), true);
self::assertIsArray($body);

$this->assertArrayHasKey('sub', $body);
$this->assertArrayHasKey('email', $body);
Expand All @@ -99,6 +101,7 @@ public function testDelayFn(): void
$time = $timer->stop()->asSeconds();

$body = json_decode($response->getBody()->getContents(), true);
self::assertIsArray($body);

$this->assertArrayHasKey('sub', $body);
$this->assertArrayHasKey('email', $body);
Expand All @@ -124,7 +127,7 @@ public function testRetries(): void
'request' => [
'uri' => 'https://www.googleapis.com/auth/invalid-scope',
'headers' => [
'User-Agent' => ['GuzzleHttp/6.5.5 curl/7.74.0 PHP/7.4.33'],
'User-Agent' => ['GuzzleHttp/7'],
'Host' => ['www.googleapis.com'],
'Accept' => ['application/json'],
'Authorization' => '*****',
Expand All @@ -145,7 +148,7 @@ public function testRetries(): void
" it to craft your OAuth2 request.\n",
],
],
$value['context']
$value['context'],
);
}
}
Expand All @@ -160,7 +163,7 @@ public function testDoNotRetryOnWrongCredentials(): void
$this->getEnv('CLIENT_SECRET') . 'invalid',
'',
$this->getEnv('REFRESH_TOKEN'),
$logger
$logger,
);

try {
Expand All @@ -172,7 +175,7 @@ public function testDoNotRetryOnWrongCredentials(): void
$this->assertCount(1, $testHandler->getRecords());
$this->assertStringContainsString(
'Retrying request (0x) - reason: Unauthorized',
$testHandler->getRecords()[0]['message']
$testHandler->getRecords()[0]['message'],
);
}

Expand All @@ -181,7 +184,7 @@ protected function getEnv(string $name): string
$value = getenv($name);

if ($value === false) {
throw new \Exception(sprintf('Environment variable "%s" cannot be empty', $name));
throw new Exception(sprintf('Environment variable "%s" cannot be empty', $name));
}

return $value;
Expand Down
Loading