Completed
Push — master ( b4e8bc...5a947a )
by Derek Stephen
09:32
created

HtmlResponse::createBody()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.9
1
<?php
2
3
namespace Bone\Http\Response;
4
5
use Bone\Http\Response;
6
use Bone\Server\Traits\HasAttributesTrait;
0 ignored issues
show
Bug introduced by
The type Bone\Server\Traits\HasAttributesTrait 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...
7
use Laminas\Diactoros\Response\InjectContentTypeTrait;
8
9
class HtmlResponse extends Response
10
{
11
    use InjectContentTypeTrait;
12
13
    /**
14
     * Create an HTML response.
15
     *
16
     * Produces an HTML response with a Content-Type of text/html and a default
17
     * status of 200.
18
     *
19
     * @param string|StreamInterface $html HTML or stream for the message body.
0 ignored issues
show
Bug introduced by
The type Bone\Http\Response\StreamInterface 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...
20
     * @param int $status Integer status code for the response; 200 by default.
21
     * @param array $headers Array of headers to use at initialization.
22
     * @throws Exception\InvalidArgumentException if $html is neither a string or stream.
23
     */
24
    public function __construct($html, int $status = 200, array $headers = [])
25
    {
26
        parent::__construct(
27
            $this->createBody($html),
28
            $status,
29
            $this->injectContentType('text/html; charset=utf-8', $headers)
30
        );
31
    }
32
33
    /**
34
     * Create the message body.
35
     *
36
     * @param string|StreamInterface $html
37
     * @throws Exception\InvalidArgumentException if $html is neither a string or stream.
38
     */
39
    private function createBody($html) : StreamInterface
40
    {
41
        if ($html instanceof StreamInterface) {
42
            return $html;
43
        }
44
45
        if (! is_string($html)) {
0 ignored issues
show
introduced by
The condition is_string($html) is always true.
Loading history...
46
            throw new Exception\InvalidArgumentException(sprintf(
0 ignored issues
show
Bug introduced by
The type Bone\Http\Response\Excep...nvalidArgumentException 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...
47
                'Invalid content (%s) provided to %s',
48
                (is_object($html) ? get_class($html) : gettype($html)),
49
                __CLASS__
50
            ));
51
        }
52
53
        $body = new Stream('php://temp', 'wb+');
0 ignored issues
show
Bug introduced by
The type Bone\Http\Response\Stream 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...
54
        $body->write($html);
55
        $body->rewind();
56
        return $body;
57
    }
58
}