Passed
Push — main ( 99b119...3ba23a )
by Darío
56s queued 12s
created

GuzzleResponseBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 22
c 2
b 0
f 0
dl 0
loc 63
ccs 26
cts 26
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A json() 0 6 1
A statusCode() 0 5 1
A headers() 0 5 1
A body() 0 6 1
A getBody() 0 3 1
A getJson() 0 3 1
A response() 0 6 2
A getHeaders() 0 3 1
A getStatusCode() 0 3 1
1
<?php
2
3
namespace EasyHttp\MockBuilder\ResponseBuilders;
4
5
use EasyHttp\MockBuilder\Contracts\ResponseBuilder;
6
use GuzzleHttp\Promise\FulfilledPromise;
7
use GuzzleHttp\Psr7\Response;
8
9
class GuzzleResponseBuilder implements ResponseBuilder
10
{
11
    private int $statusCode = 200;
12
    private array $headers = [];
13
    private string $body = '';
14
    private array $json = [];
15
16 6
    public function statusCode(int $statusCode): self
17
    {
18 6
        $this->statusCode = $statusCode;
19
20 6
        return $this;
21
    }
22
23 1
    public function getStatusCode(): int
24
    {
25 1
        return $this->statusCode;
26
    }
27
28 2
    public function headers(array $headers): self
29
    {
30 2
        $this->headers = $headers;
31
32 2
        return $this;
33
    }
34
35 1
    public function getHeaders(): array
36
    {
37 1
        return $this->headers;
38
    }
39
40 2
    public function getBody(): string
41
    {
42 2
        return $this->body;
43
    }
44
45 106
    public function body(string $body): self
46
    {
47 106
        $this->body = $body;
48 106
        $this->json = [];
49
50 106
        return $this;
51
    }
52
53 2
    public function getJson(): array
54
    {
55 2
        return $this->json;
56
    }
57
58 3
    public function json(array $json): self
59
    {
60 3
        $this->body = '';
61 3
        $this->json = $json;
62
63 3
        return $this;
64
    }
65
66 52
    public function response(): FulfilledPromise
67
    {
68 52
        $body = $this->json ? json_encode($this->json, true) : $this->body;
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $flags of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $body = $this->json ? json_encode($this->json, /** @scrutinizer ignore-type */ true) : $this->body;
Loading history...
69
70 52
        return new FulfilledPromise(
71 52
            new Response($this->statusCode, $this->headers, $body)
72
        );
73
    }
74
}
75