JsonResponder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 100
ccs 22
cts 29
cp 0.7586
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A setJsonOptions() 0 5 1
A setResponseContentType() 0 5 1
A __invoke() 0 4 1
A createResponse() 0 28 5
1
<?php
2
namespace Germania\Responder;
3
4
use Psr\Http\Message\ResponseInterface;
5
use Psr\Http\Message\ResponseFactoryInterface;
6
use Slim\Psr7\Factory\ResponseFactory;
7
8
class JsonResponder implements ResponderInterface
9
{
10
11 1
    use ResponseFactoryTrait;
12
13
14
    /**
15
     * @var int
16
     */
17
    public $json_options = 0;
18
19
20
    /**
21
     * Response content type
22
     * @var string
23
     */
24
    public $response_content_type = 'application/json';
25
26
27
28
    /**
29
     * @param int                      $json_options
30
     * @param ResponseFactoryInterface $response_factory
31
     */
32 14
    public function __construct(int $json_options = 0, ResponseFactoryInterface $response_factory = null )
33
    {
34 14
        $this->setResponseFactory($response_factory ?: new ResponseFactory);
35 14
        $this->setJsonOptions($json_options);
36 14
    }
37
38
39
    /**
40
     * Sets the JSON options.
41
     *
42
     * @param int $json_options
43
     */
44 14
    public function setJsonOptions( int $json_options )
45
    {
46 14
        $this->json_options = $json_options;
47 14
        return $this;
48
    }
49
50
51
    /**
52
     * Sets the response content type.
53
     *
54
     * @param string $content_type
55
     */
56
    public function setResponseContentType( string $content_type )
57
    {
58
        $this->response_content_type = $content_type;
59
        return $this;
60
    }
61
62
63
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function __invoke( $thingy, int $status = 500 ) : ResponseInterface
69
    {
70
        return $this->createResponse( $thingy, $status);
71
    }
72
73
74
    /**
75
     * @inheritDoc
76
     */
77 20
    public function createResponse( $thingy, int $status = 200) : ResponseInterface
78
    {
79 20
        if (is_resource($thingy)) {
80 2
            $msg = sprintf("Can't work with resource types.");
81 2
            throw new ResponderInvalidArgumentException($msg);
82
        }
83
84 18
        if (is_object($thingy)
85 18
        and !$thingy instanceOf \JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
86 2
            $msg = sprintf("Expected JsonSerializable instance, instead got '%s'.", get_class($thingy));
87 2
            throw new ResponderInvalidArgumentException($msg);
88
        }
89
90
        try {
91 16
            $json_thingy = json_encode($thingy, $this->json_options);
92
93 16
            $response = $this->getResponseFactory()->createResponse()
94 16
                                                   ->withHeader('Content-type', $this->response_content_type)
95 16
                                                   ->withStatus($status);
96
97 16
            $response->getBody()->write($json_thingy);
98
        }
99
        catch (\Throwable $e) {
100
            throw new ResponderRuntimeException("Caught exception during response creation", 1, $e);
101
        }
102
103 16
        return $response;
104
    }
105
106
107
}
108