forked from VantaFinance/daDataClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientErrorMiddleware.php
More file actions
48 lines (38 loc) · 1.62 KB
/
ClientErrorMiddleware.php
File metadata and controls
48 lines (38 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* DaData Client
*
* @author Vlad Shashkov <v.shashkov@pos-credit.ru>
* @copyright Copyright (c) 2023, The Vanta
*/
declare(strict_types=1);
namespace Vanta\Integration\DaData\Infrastructure\HttpClient\Middleware;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Vanta\Integration\DaData\Infrastructure\HttpClient\ConfigurationClient;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Exception\BadRequestException;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Exception\ForbiddenException;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Exception\NotFoundException;
use Vanta\Integration\DaData\Infrastructure\HttpClient\Exception\UnauthorizedException;
use Yiisoft\Http\Status;
final class ClientErrorMiddleware implements Middleware
{
public function process(Request $request, ConfigurationClient $configuration, callable $next): Response
{
$response = $next($request, $configuration);
$statusCode = $response->getStatusCode();
if (Status::UNAUTHORIZED == $statusCode) {
throw UnauthorizedException::create($response, $request);
}
if (Status::FORBIDDEN == $statusCode) {
throw ForbiddenException::create($response, $request);
}
if (Status::NOT_FOUND === $statusCode) {
throw NotFoundException::create($response, $request);
}
if ($statusCode >= Status::BAD_REQUEST && $statusCode <= Status::UNAVAILABLE_FOR_LEGAL_REASONS) {
throw BadRequestException::create($response, $request);
}
return $response;
}
}