|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
|
|
|
|
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
|
|
|
|
|
9
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
|
|
|
|
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
|
|
|
|
|
11
|
|
|
use Yiisoft\DataResponse\DataResponse; |
|
|
|
|
|
|
12
|
|
|
use OpenApi\Attributes as OA; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
#[OA\Schema( |
|
15
|
|
|
schema: 'Response', |
|
16
|
|
|
properties: [ |
|
17
|
|
|
new OA\Property(property: 'status', type: 'string', enum: ['success', 'failed']), |
|
18
|
|
|
new OA\Property(property: 'error', type:'object', nullable: true), |
|
19
|
|
|
new OA\Property(property: 'data', type: 'object', nullable: true), |
|
20
|
|
|
] |
|
21
|
|
|
)] |
|
22
|
|
|
final class ApiDataWrapper implements MiddlewareInterface |
|
23
|
|
|
{ |
|
24
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
25
|
|
|
{ |
|
26
|
|
|
$response = $handler->handle($request); |
|
27
|
|
|
if ($response instanceof DataResponse) { |
|
28
|
|
|
$data = $response->getData(); |
|
29
|
|
|
if ($response->getStatusCode() !== 200) { |
|
30
|
|
|
if (is_string($data) && !empty($data)) { |
|
31
|
|
|
$message = $data; |
|
32
|
|
|
} else { |
|
33
|
|
|
$message = 'Unknown error'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $response->withData([ |
|
37
|
|
|
'status' => 'failed', |
|
38
|
|
|
'error' => ['message' => $message, 'status' => $response->getStatusCode()], |
|
39
|
|
|
]); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $response->withData(['status' => 'success', 'data' => $data]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $response; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths