Passed
Branch test-coverage (8dc675)
by Brad
12:43
created

ResponseBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setFixtureDirectory() 0 4 1
A setBodyFromFixture() 0 6 1
A setBody() 0 5 1
A build() 0 6 1
A setStatus() 0 5 1
A setHeaders() 0 5 1
A toJsonFixture() 0 4 1
A fromFixture() 0 9 1
A __construct() 0 6 1
1
<?php
2
3
namespace Muzzle;
4
5
use GuzzleHttp\Psr7\Response;
6
use Muzzle\Messages\DecodableResponse;
7
use Muzzle\Messages\JsonFixture;
8
use Psr\Http\Message\ResponseInterface;
9
10
class ResponseBuilder
11
{
12
13
    /**
14
     * @var string
15
     */
16
    protected static $fixturePath;
17
18
    /**
19
     * @var int|null
20
     */
21
    private $status;
22
    /**
23
     * @var array
24
     */
25
    private $headers;
26
    /**
27
     * @var string|null|resource|\Psr\Http\Message\StreamInterface
28
     */
29
    private $body;
30
31
    /**
32
     * @param int|null $status
33
     * @param array $headers
34
     * @param string|null|resource|\Psr\Http\Message\StreamInterface $body
35
     */
36
    public function __construct(int $status = HttpStatus::OK, array $headers = [], $body = null)
37
    {
38
39
        $this->status = $status;
40
        $this->headers = $headers;
41
        $this->body = $body;
42
    }
43
44
    public static function fromFixture(
45
        string $fixture,
46
        int $status = HttpStatus::OK,
47
        array $headers = []
48
    ) : ResponseInterface {
49
50
        return (new static($status, $headers))
51
            ->setBodyFromFixture($fixture)
52
            ->toJsonFixture();
53
    }
54
55
    public function toJsonFixture() : JsonFixture
56
    {
57
58
        return new JsonFixture($this->status, $this->headers, $this->body);
59
    }
60
61
62
    public static function setFixtureDirectory(string $path) : void
63
    {
64
65
        static::$fixturePath = rtrim($path, '/') . '/';
66
    }
67
68
    public function setStatus(?int $status) : ResponseBuilder
69
    {
70
71
        $this->status = $status;
72
        return $this;
73
    }
74
75
    public function setHeaders(array $headers) : ResponseBuilder
76
    {
77
78
        $this->headers = $headers;
79
        return $this;
80
    }
81
82
    /**
83
     * @param string|null|resource|\Psr\Http\Message\StreamInterface $body
84
     * @return ResponseBuilder
85
     */
86
    public function setBody($body) : ResponseBuilder
87
    {
88
89
        $this->body = $body;
90
        return $this;
91
    }
92
93
    public function setBodyFromFixture(string $path) : ResponseBuilder
94
    {
95
96
        $this->body = fopen(static::$fixturePath . ltrim($path, '/'), 'r');
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return ResponseInterface|DecodableResponse
103
     */
104
    public function build() : ResponseInterface
105
    {
106
107
        $response = new Response($this->status, $this->headers, $this->body);
108
109
        return new DecodableResponse($response);
110
    }
111
}
112