Skip to content

Commit b3c3ae6

Browse files
committed
code improvement
1 parent 9e6ab02 commit b3c3ae6

File tree

7 files changed

+39
-37
lines changed

7 files changed

+39
-37
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"require-dev": {
2424
"ext-pdo": "*",
25-
"phpstan/phpstan": "^2.0",
25+
"phpstan/phpstan": "^2.1",
2626
"phpunit/phpunit": "^10.0",
2727
"phpstan/phpstan-strict-rules": "^2.0",
2828
"phpstan/phpstan-deprecation-rules": "^2.0"

src/Model/Client/ApiException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ApiException extends \RuntimeException
88
{
99
public function __construct(
1010
string $message,
11-
private Request $request,
12-
private ?Response $response = null,
11+
private readonly Request $request,
12+
private readonly ?Response $response = null,
1313
\Throwable $previous = null
1414
) {
1515
parent::__construct($message, $response?->getStatusCode() ?? 0, $previous);

src/Model/Client/ResponseApiException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ResponseApiException extends ApiException
99
final public function __construct(
1010
string $message,
1111
Request $request,
12-
private Response $response,
12+
private readonly Response $response,
1313
\Throwable $previous = null
1414
) {
1515
parent::__construct($message, $request, $response, $previous);

src/Model/Client/ResponsePromise.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,32 @@ final class ResponsePromise
1313
* @param non-empty-string $apiName
1414
*/
1515
public function __construct(
16-
private string $apiName,
17-
private Request $request,
18-
private PromiseInterface $promise
16+
public readonly string $apiName,
17+
public readonly Request $request,
18+
public readonly PromiseInterface $promise,
1919
) {
2020
}
2121

2222
/**
23+
* @deprecated will be removed
2324
* @return non-empty-string
2425
*/
2526
public function apiName(): string
2627
{
2728
return $this->apiName;
2829
}
2930

31+
/**
32+
* @deprecated will be removed
33+
*/
3034
public function request(): Request
3135
{
3236
return $this->request;
3337
}
3438

39+
/**
40+
* @deprecated will be removed
41+
*/
3542
public function wait(): Response
3643
{
3744
/** @var ResponseInterface $response */

src/Service/Client/ApiClient.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GuzzleHttp\RequestOptions;
1212
use Kayex\HttpCodes;
1313
use Psr\Http\Message\RequestFactoryInterface;
14+
use Psr\Http\Message\ResponseInterface;
1415
use SimpleAsFuck\ApiToolkit\Model\Client\ApiException;
1516
use SimpleAsFuck\ApiToolkit\Model\Client\BadRequestApiException;
1617
use SimpleAsFuck\ApiToolkit\Model\Client\ConflictApiException;
@@ -173,12 +174,17 @@ public function requestAsync(string $apiName, Request $request, array $options =
173174
public function waitRaw(ResponsePromise $promise): Response
174175
{
175176
try {
176-
$response = $promise->wait();
177+
/**
178+
* @var ResponseInterface $response
179+
* @throws RequestException|TransferException
180+
*/
181+
$response = $promise->promise->wait();
182+
$response = new Response($promise->request, $response);
177183
} catch (RequestException $exception) {
178184
$message = $exception->getMessage();
179185
$response = $exception->getResponse();
180186
if ($response !== null) {
181-
$this->deprecationsLogger?->logDeprecation($promise->apiName(), $promise->request(), $response);
187+
$this->deprecationsLogger?->logDeprecation($promise->apiName, $promise->request, $response);
182188

183189
$responseContent = $response->getBody()->getContents();
184190
$errorObject = Validator::make(\json_decode($responseContent))->object();
@@ -221,38 +227,26 @@ public function waitRaw(ResponsePromise $promise): Response
221227
}
222228

223229
$response = $response->withBody((new HttpFactory())->createStream($responseContent));
224-
$response = new Response($promise->request(), $response);
225-
if ($response->getStatusCode() === HttpCodes::HTTP_BAD_REQUEST) {
226-
throw new BadRequestApiException($message, $promise->request(), $response, $exception);
227-
}
228-
if ($response->getStatusCode() === HttpCodes::HTTP_UNAUTHORIZED) {
229-
throw new UnauthorizedApiException($message, $promise->request(), $response, $exception);
230-
}
231-
if ($response->getStatusCode() === HttpCodes::HTTP_FORBIDDEN) {
232-
throw new ForbiddenApiException($message, $promise->request(), $response, $exception);
233-
}
234-
if ($response->getStatusCode() === HttpCodes::HTTP_NOT_FOUND) {
235-
throw new NotFoundApiException($message, $promise->request(), $response, $exception);
236-
}
237-
if ($response->getStatusCode() === HttpCodes::HTTP_CONFLICT) {
238-
throw new ConflictApiException($message, $promise->request(), $response, $exception);
239-
}
240-
if ($response->getStatusCode() === HttpCodes::HTTP_GONE) {
241-
throw new GoneApiException($message, $promise->request(), $response, $exception);
242-
}
243-
if ($response->getStatusCode() === HttpCodes::HTTP_INTERNAL_SERVER_ERROR) {
244-
throw new InternalServerErrorApiException($message, $promise->request(), $response, $exception);
245-
}
230+
$response = new Response($promise->request, $response);
246231

247-
throw new ResponseApiException($message, $promise->request(), $response, $exception);
232+
match ($response->getStatusCode()) {
233+
HttpCodes::HTTP_BAD_REQUEST => throw new BadRequestApiException($message, $promise->request, $response, $exception),
234+
HttpCodes::HTTP_UNAUTHORIZED => throw new UnauthorizedApiException($message, $promise->request, $response, $exception),
235+
HttpCodes::HTTP_FORBIDDEN => throw new ForbiddenApiException($message, $promise->request, $response, $exception),
236+
HttpCodes::HTTP_NOT_FOUND => throw new NotFoundApiException($message, $promise->request, $response, $exception),
237+
HttpCodes::HTTP_CONFLICT => throw new ConflictApiException($message, $promise->request, $response, $exception),
238+
HttpCodes::HTTP_GONE => throw new GoneApiException($message, $promise->request, $response, $exception),
239+
HttpCodes::HTTP_INTERNAL_SERVER_ERROR => throw new InternalServerErrorApiException($message, $promise->request, $response, $exception),
240+
default => throw new ResponseApiException($message, $promise->request, $response, $exception),
241+
};
248242
}
249243

250-
throw new ApiException($message, $promise->request(), null, $exception);
244+
throw new ApiException($message, $promise->request, null, $exception);
251245
} catch (TransferException $exception) {
252-
throw new ApiException($exception->getMessage(), $promise->request(), null, $exception);
246+
throw new ApiException($exception->getMessage(), $promise->request, null, $exception);
253247
}
254248

255-
$this->deprecationsLogger?->logDeprecation($promise->apiName(), $promise->request(), $response);
249+
$this->deprecationsLogger?->logDeprecation($promise->apiName, $promise->request, $response);
256250
return $response;
257251
}
258252

src/Service/Webhook/LaravelMysqlRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\QueryException;
1010
use Illuminate\Support\Collection;
1111
use SimpleAsFuck\ApiToolkit\Model\Webhook\Params;
12+
use SimpleAsFuck\ApiToolkit\Model\Webhook\Priority;
1213
use SimpleAsFuck\ApiToolkit\Model\Webhook\Webhook;
1314
use SimpleAsFuck\Validator\Factory\Validator;
1415

@@ -49,7 +50,7 @@ public function loadForDispatching(string $type, array $attributes): iterable
4950

5051
$webhooks = [];
5152
foreach ($webhookMatches as $webhookId => $matchCount) {
52-
/** @var object{id: int, type: non-empty-string, listeningUrl: non-empty-string, priority: 50|100|200}|null $webhook */
53+
/** @var object{id: int, type: non-empty-string, listeningUrl: non-empty-string, priority: Priority::*}|null $webhook */
5354
$webhook = $connection->table('Webhook')->where('id', '=', $webhookId)->where('type', '=', $type)->first();
5455
if ($webhook === null) {
5556
continue;

test/Service/Webhook/LaravelMysqlRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function testLoadForDispatching(array $expectedUrls, string $type, array
9090
$connection = self::$connectionResolver->connection('default');
9191

9292
$expectedWebhooks = array_map(function (string $url) use ($connection, $attributes): Webhook {
93-
/** @var object{id: int, type: non-empty-string, listeningUrl: non-empty-string, priority: 50|100|200} $dbWebhook */
93+
/** @var object{id: int, type: non-empty-string, listeningUrl: non-empty-string, priority: Priority::*} $dbWebhook */
9494
$dbWebhook = $connection->selectOne('select * from Webhook where listeningUrl = ?', [$url]);
9595
return new Webhook(
9696
(string) $dbWebhook->id,

0 commit comments

Comments
 (0)