ResponseBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Thruster\Component\HttpResponse;
4
5
use Psr\Http\Message\ResponseInterface;
6
use function Thruster\Component\HttpMessage\stream_for;
7
8
/**
9
 * Class ResponseBuilder
10
 *
11
 * @package Thruster\Component\HttpResponse
12
 * @author  Aurimas Niekis <[email protected]>
13
 */
14
class ResponseBuilder
15
{
16
    /**
17
     * @var ResponseInterface
18
     */
19
    private $response;
20
21 2
    private function __construct(ResponseInterface $response)
22
    {
23 2
        $this->response = $response;
24 2
    }
25
26 2
    public static function init(ResponseInterface $response) : self
27
    {
28 2
        return new static($response);
29
    }
30
31 2
    public function withJsonBody($data, string $contentType = 'application/json') : ResponseInterface
32
    {
33 2
        return $this->response
34 2
            ->withBody(stream_for(json_encode($data)))
35 2
            ->withHeader('Content-Type', $contentType);
36
    }
37
}
38