ResponseDataWrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 31
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A process() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Debug\Middleware;
6
7
use OpenApi\Annotations as OA;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\DataResponse\DataResponse;
13
use Yiisoft\DataResponse\DataResponseFactoryInterface;
14
use Yiisoft\Http\Status;
15
use Yiisoft\Router\CurrentRoute;
16
use Yiisoft\Yii\Debug\Api\Debug\Exception\NotFoundException;
17
18
/**
19
 * @OA\Schema(schema="DebugResponse", description="Yii Debug Api response")
20
 * @OA\Schema(
21
 *     schema="DebugSuccessResponse",
22
 *     allOf={
23
 *          @OA\Schema(ref="#/components/schemas/DebugResponse"),
24
 *          @OA\Schema(
25
 *
26
 *              @OA\Property(
27
 *                   description="ID",
28
 *                   title="ID",
29
 *                   property="id",
30
 *                   format="string"
31
 *              ),
32
 *              @OA\Property(
33
 *                   description="Data",
34
 *                   title="Data",
35
 *                   property="data",
36
 *                   type="object",
37
 *                   nullable=true
38
 *              ),
39
 *              @OA\Property(
40
 *                   description="Error",
41
 *                   title="Error",
42
 *                   property="error",
43
 *                   format="string",
44
 *                   nullable=true,
45
 *                   example=null
46
 *              ),
47
 *              @OA\Property(
48
 *                   description="Success",
49
 *                   title="Success",
50
 *                   property="success",
51
 *                   type="boolean",
52
 *                   example=true
53
 *              )
54
 *          )
55
 *     }
56
 * )
57
 *
58
 * @OA\Schema(
59
 *     schema="DebugNotFoundResponse",
60
 *     allOf={
61
 *          @OA\Schema(ref="#/components/schemas/DebugResponse"),
62
 *          @OA\Schema(
63
 *
64
 *              @OA\Property(
65
 *                   description="ID",
66
 *                   title="ID",
67
 *                   property="id",
68
 *                   format="string"
69
 *              ),
70
 *              @OA\Property(
71
 *                   description="Data",
72
 *                   title="Data",
73
 *                   property="data",
74
 *                   type="object",
75
 *                   nullable=true,
76
 *                   example=null
77
 *              ),
78
 *              @OA\Property(
79
 *                   description="Error",
80
 *                   title="Error",
81
 *                   property="error",
82
 *                   format="string",
83
 *              ),
84
 *              @OA\Property(
85
 *                   description="Success",
86
 *                   title="Success",
87
 *                   property="success",
88
 *                   type="boolean",
89
 *                   example=false
90
 *              )
91
 *          )
92
 *     }
93
 * )
94
 */
95
final class ResponseDataWrapper implements MiddlewareInterface
96
{
97
    public function __construct(private DataResponseFactoryInterface $responseFactory, private CurrentRoute $currentRoute)
98
    {
99
    }
100
101
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
102
    {
103
        $data = [
104
            'id' => $this->currentRoute->getArgument('id'),
105
            'data' => null,
106
            'error' => null,
107
            'success' => true,
108
        ];
109
        try {
110
            $response = $handler->handle($request);
111
            if (!$response instanceof DataResponse) {
112
                return $response;
113
            }
114
            $data['data'] = $response->getData();
115
            $status = $response->getStatusCode();
116
            if ($status >= 400) {
117
                $data['success'] = false;
118
            }
119
        } catch (NotFoundException $exception) {
120
            $data['success'] = false;
121
            $data['error'] = $exception->getMessage();
122
            $status = Status::NOT_FOUND;
123
        }
124
125
        return $this->responseFactory->createResponse($data, $status);
126
    }
127
}
128