Issues (867)

blog/src/Middleware/ApiDataWrapper.php (6 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Middleware;
6
7
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Psr\Http\Server\MiddlewareInterface;
0 ignored issues
show
The type Psr\Http\Server\MiddlewareInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Psr\Http\Server\RequestHandlerInterface;
0 ignored issues
show
The type Psr\Http\Server\RequestHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\DataResponse\DataResponse;
0 ignored issues
show
The type Yiisoft\DataResponse\DataResponse was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use OpenApi\Attributes as OA;
0 ignored issues
show
The type OpenApi\Attributes was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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