1 | <?php |
||
2 | |||
3 | namespace Muzzle; |
||
4 | |||
5 | use GuzzleHttp\Psr7\Response; |
||
6 | use Muzzle\Messages\JsonFixture; |
||
7 | use Psr\Http\Message\ResponseInterface; |
||
8 | |||
9 | class ResponseBuilder |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected static $fixturePath; |
||
16 | |||
17 | /** |
||
18 | * @var int|null |
||
19 | */ |
||
20 | private $status; |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $headers; |
||
25 | /** |
||
26 | * @var null |
||
27 | */ |
||
28 | private $body; |
||
29 | |||
30 | /** |
||
31 | * @param int|null $status |
||
32 | * @param array $headers |
||
33 | * @param string|null|resource|\Psr\Http\Message\StreamInterface $body |
||
34 | */ |
||
35 | public function __construct(int $status = HttpStatus::OK, array $headers = [], $body = null) |
||
36 | { |
||
37 | |||
38 | $this->status = $status; |
||
39 | $this->headers = $headers; |
||
40 | $this->body = $body; |
||
41 | } |
||
42 | |||
43 | public static function fromFixture( |
||
44 | string $fixture, |
||
45 | int $status = HttpStatus::OK, |
||
46 | array $headers = [] |
||
47 | ) : ResponseInterface { |
||
48 | |||
49 | $builder = (new static($status, $headers))->setBodyFromFixture($fixture); |
||
50 | return new JsonFixture($builder->status, $builder->headers, $builder->body); |
||
51 | } |
||
52 | |||
53 | |||
54 | public static function setFixtureDirectory(string $path) : void |
||
55 | { |
||
56 | |||
57 | static::$fixturePath = rtrim($path, '/') . '/'; |
||
58 | } |
||
59 | |||
60 | public function setStatus(?int $status) : ResponseBuilder |
||
61 | { |
||
62 | |||
63 | $this->status = $status; |
||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | public function setHeaders(array $headers) : ResponseBuilder |
||
68 | { |
||
69 | |||
70 | $this->headers = $headers; |
||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string|null|resource|\Psr\Http\Message\StreamInterface $body |
||
76 | * @return ResponseBuilder |
||
77 | */ |
||
78 | public function setBody($body) : ResponseBuilder |
||
79 | { |
||
80 | |||
81 | $this->body = $body; |
||
82 | return $this; |
||
83 | } |
||
84 | |||
85 | public function setBodyFromFixture(string $path) : ResponseBuilder |
||
86 | { |
||
87 | |||
88 | $this->body = fopen(static::$fixturePath . ltrim($path, '/'), 'r'); |
||
0 ignored issues
–
show
|
|||
89 | |||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | public function build() : Response |
||
94 | { |
||
95 | |||
96 | return new Response($this->status, $this->headers, $this->body); |
||
97 | } |
||
98 | } |
||
99 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..