Passed
Push — master ( 093d25...9ddd4f )
by Mr
01:54
created

ErrorResponder::respondToJson()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Middleware\Action;
10
11
use Daikon\Boot\Middleware\Action\Responder;
12
use Daikon\Boot\Middleware\ActionHandler;
13
use Laminas\Diactoros\Response\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Laminas\Diactoros\Response\JsonResponse 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...
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
final class ErrorResponder extends Responder
18
{
19
    public function respondToJson(ServerRequestInterface $request): ResponseInterface
20
    {
21
        $message = $request->getAttribute(ActionHandler::ATTR_STATUS_MESSAGE, 'Internal server error.');
22
        $statusCode = $request->getAttribute(ActionHandler::ATTR_STATUS_CODE, self::STATUS_INTERNAL_SERVER_ERROR);
23
        $errors = $request->getAttribute(ActionHandler::ATTR_ERRORS, []);
24
25
        return new JsonResponse(
26
            ['message' => $message] + (!empty($errors) ? ['errors' => $errors] : []),
27
            $statusCode
28
        );
29
    }
30
}
31