Skip to content

Commit 77b1435

Browse files
Initializing v2.0 optimized for PHP 8 & Symfony 5 support, dropping support for previous versions entirely. Major backward compatibility breaks from v1 but overall logic remains exactly the same. Removes many useless features and dependencies.
1 parent 3dd9347 commit 77b1435

32 files changed

+288
-978
lines changed

Action/RedirectUrlSetterAction.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

CleverAgeOAuthApiBundle.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
<?php declare(strict_types=1);
1+
<?php
22
/*
3-
* This file is part of the CleverAge/OAuthApiBundle package.
4-
*
5-
* Copyright (C) 2017-2019 Clever-Age
6-
*
7-
* For the full copyright and license information, please view the LICENSE
3+
* This file is part of the CleverAge/OAuthApiBundle package. * Copyright (C) 2017-2021 Clever-Age * For the full copyright and license information, please view the LICENSE
84
* file that was distributed with this source code.
95
*/
6+
declare(strict_types=1);
107

118
namespace CleverAge\OAuthApiBundle;
129

1310
use Symfony\Component\HttpKernel\Bundle\Bundle;
1411

15-
/**
16-
* @author Vincent Chalnot <[email protected]>
17-
*/
1812
class CleverAgeOAuthApiBundle extends Bundle
1913
{
2014
}

Client/ApiClient.php

Lines changed: 31 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,46 @@
11
<?php
2-
declare(strict_types=1);
32
/*
4-
* This file is part of the CleverAge/OAuthApiBundle package.
5-
*
6-
* Copyright (C) 2017-2019 Clever-Age
7-
*
8-
* For the full copyright and license information, please view the LICENSE
3+
* This file is part of the CleverAge/OAuthApiBundle package. * Copyright (C) 2017-2021 Clever-Age * For the full copyright and license information, please view the LICENSE
94
* file that was distributed with this source code.
105
*/
6+
declare(strict_types=1);
117

128
namespace CleverAge\OAuthApiBundle\Client;
139

1410
use CleverAge\OAuthApiBundle\Exception\ApiDeserializationException;
1511
use CleverAge\OAuthApiBundle\Exception\ApiRequestException;
1612
use CleverAge\OAuthApiBundle\Exception\RequestFailedException;
1713
use CleverAge\OAuthApiBundle\Request\ApiRequestInterface;
18-
use Http\Client\Exception as HttpException;
19-
use GuzzleHttp\Psr7\Request;
20-
use Http\Client\HttpClient;
14+
use Nyholm\Psr7\Stream;
15+
use Psr\Http\Client\ClientInterface;
16+
use Psr\Http\Client\RequestExceptionInterface;
17+
use Psr\Http\Message\RequestFactoryInterface;
18+
use Psr\Http\Message\RequestInterface;
2119
use Psr\Log\LoggerInterface;
2220
use Symfony\Component\Serializer\SerializerInterface;
2321

2422
/**
25-
* {@inheritDoc}
26-
*
27-
* @author Vincent Chalnot <[email protected]>
23+
* @see ApiClientInterface
2824
*/
2925
class ApiClient implements ApiClientInterface
3026
{
31-
/** @var HttpClient */
32-
protected $client;
33-
34-
/** @var SerializerInterface */
35-
protected $serializer;
36-
37-
/** @var string */
38-
protected $baseUrl;
39-
40-
/** @var LoggerInterface */
41-
protected $logger;
42-
43-
/**
44-
* @param HttpClient $client
45-
* @param SerializerInterface $serializer
46-
* @param LoggerInterface $logger
47-
* @param string $baseUrl
48-
*/
4927
public function __construct(
50-
HttpClient $client,
51-
SerializerInterface $serializer,
52-
LoggerInterface $logger,
53-
string $baseUrl
28+
protected ClientInterface $client,
29+
protected RequestFactoryInterface $requestFactory,
30+
protected SerializerInterface $serializer,
31+
protected LoggerInterface $logger,
5432
) {
55-
$this->client = $client;
56-
$this->serializer = $serializer;
57-
$this->logger = $logger;
58-
$this->baseUrl = $baseUrl;
5933
}
6034

61-
/**
62-
* {@inheritDoc}
63-
*/
64-
public function query(ApiRequestInterface $apiRequest)
35+
public function query(ApiRequestInterface $apiRequest): object
6536
{
6637
$request = $this->getRequest($apiRequest);
6738
$normalizedData = $this->getResponseData($request);
6839

6940
return $this->deserialize($apiRequest, $normalizedData);
7041
}
7142

72-
/**
73-
* @param ApiRequestInterface $apiRequest
74-
* @param string|null $normalizedData
75-
*
76-
* @return object
77-
*/
78-
protected function deserialize(ApiRequestInterface $apiRequest, ?string $normalizedData)
43+
protected function deserialize(ApiRequestInterface $apiRequest, ?string $normalizedData): object
7944
{
8045
try {
8146
return $this->serializer->deserialize(
@@ -94,39 +59,26 @@ protected function deserialize(ApiRequestInterface $apiRequest, ?string $normali
9459
}
9560
}
9661

97-
/**
98-
* @param ApiRequestInterface $apiRequest
99-
*
100-
* @return Request
101-
*/
102-
protected function getRequest(ApiRequestInterface $apiRequest): Request
62+
protected function getRequest(ApiRequestInterface $apiRequest): RequestInterface
10363
{
104-
$serializedContent = null;
105-
if ($apiRequest->getContent()) {
106-
$serializedContent = $this->serializer->serialize(
107-
$apiRequest->getContent(),
108-
'json',
109-
$apiRequest->getSerializationContext()
110-
);
64+
$request = $this->requestFactory->createRequest($apiRequest->getMethod(), $apiRequest->getPath())
65+
->withHeader('Content-Type', 'application/json');
66+
67+
if (null === $apiRequest->getContent()) {
68+
return $request;
11169
}
11270

113-
return new Request(
114-
$apiRequest->getMethod(),
115-
$this->baseUrl.$apiRequest->getPath(),
116-
[
117-
'Content-Type' => 'application/json',
118-
],
119-
$serializedContent
71+
$serializedContent = $this->serializer->serialize(
72+
$apiRequest->getContent(),
73+
'json',
74+
$apiRequest->getSerializationContext()
12075
);
76+
77+
return $request->withBody(Stream::create($serializedContent));
12178
}
12279

123-
/**
124-
* @param Request $request
125-
*
126-
* @return string|null
127-
*/
12880
protected function getResponseData(
129-
Request $request
81+
RequestInterface $request
13082
): ?string {
13183
$this->logger->debug(
13284
"API Request: {$request->getMethod()} {$request->getUri()}",
@@ -138,7 +90,7 @@ protected function getResponseData(
13890
);
13991
try {
14092
$response = $this->client->sendRequest($request);
141-
} catch (HttpException $e) {
93+
} catch (RequestExceptionInterface $e) {
14294
throw ApiRequestException::create((string) $request->getUri(), $e);
14395
}
14496
$body = (string) $response->getBody();
@@ -159,21 +111,12 @@ protected function getResponseData(
159111
return $body;
160112
}
161113

162-
/**
163-
* @param \Exception $exception
164-
* @param string|null $normalizedData
165-
* @param string $className
166-
* @param array $deserializationContext
167-
*
168-
* @return ApiDeserializationException
169-
*/
170114
protected function handleError(
171115
\Exception $exception,
172116
string $normalizedData,
173117
string $className,
174118
array $deserializationContext
175119
): ApiDeserializationException {
176-
$errorObject = null;
177120
$errorClass = $deserializationContext['error_class'] ?? null;
178121
if ($errorClass) {
179122
try {
@@ -183,11 +126,11 @@ protected function handleError(
183126
'json',
184127
$deserializationContext
185128
);
186-
} /** @noinspection BadExceptionsProcessingInspection */ catch (\Exception $e) {
187-
$errorObject = \json_decode($normalizedData, true);
129+
} catch (\Exception) {
130+
$errorObject = \json_decode($normalizedData, true, 512, JSON_THROW_ON_ERROR);
188131
}
189132
} else {
190-
$errorObject = \json_decode($normalizedData, true);
133+
$errorObject = \json_decode($normalizedData, true, 512, JSON_THROW_ON_ERROR);
191134
}
192135

193136
return ApiDeserializationException::create($exception, $normalizedData, $className, $errorObject);

Client/ApiClientInterface.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
1-
<?php declare(strict_types=1);
1+
<?php
22
/*
3-
* This file is part of the CleverAge/OAuthApiBundle package.
4-
*
5-
* Copyright (C) 2017-2019 Clever-Age
6-
*
7-
* For the full copyright and license information, please view the LICENSE
3+
* This file is part of the CleverAge/OAuthApiBundle package. * Copyright (C) 2017-2021 Clever-Age * For the full copyright and license information, please view the LICENSE
84
* file that was distributed with this source code.
95
*/
6+
declare(strict_types=1);
107

118
namespace CleverAge\OAuthApiBundle\Client;
129

1310
use CleverAge\OAuthApiBundle\Request\ApiRequestInterface;
1411

1512
/**
16-
* Base logic used to query a remote API
17-
*
18-
* @author Vincent Chalnot <[email protected]>
13+
* Base logic used to query a remote API object
1914
*/
2015
interface ApiClientInterface
2116
{
22-
/**
23-
* @param ApiRequestInterface $request
24-
*
25-
* @return object
26-
*/
27-
public function query(ApiRequestInterface $request);
17+
public function query(ApiRequestInterface $apiRequest): object;
2818
}

0 commit comments

Comments
 (0)