Passed
Push — master ( c3e4c9...e46620 )
by Aleksei
02:36 queued 45s
created

DataBucket::withStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
namespace roxblnfk\SmartStream\Data;
4
5
use Yiisoft\Http\Status;
6
7
class DataBucket
8
{
9
    /** @var mixed */
10
    protected $data;
11
    protected ?int $code = null;
12
    /** @var string[] */
13
    protected array $headers = [];
14
    protected ?string $format = null;
15
    protected iterable $params = [];
16
17
    protected const IS_CONVERTABLE = true;
18
19 32
    public function __construct($data, string $format = null, iterable $params = [])
20
    {
21 32
        $this->data = $data;
22 32
        $this->format = $format;
23 32
        $this->params = $params;
24 32
    }
25 8
    public function getStatusCode(): ?int
26
    {
27 8
        return $this->code;
28
    }
29 9
    public function getFormat(): ?string
30
    {
31 9
        return $this->format;
32
    }
33 4
    public function getParams(): iterable
34
    {
35 4
        return $this->params;
36
    }
37 4
    public function getData()
38
    {
39 4
        return $this->data;
40
    }
41 13
    public function isConvertable(): bool
42
    {
43 13
        return static::IS_CONVERTABLE;
44
    }
45 11
    public function hasFormat(): bool
46
    {
47 11
        return static::IS_CONVERTABLE && $this->format !== null;
48
    }
49
50 8
    public function hasHeader(string $name): bool
51
    {
52 8
        return array_key_exists($name, $this->headers);
53
    }
54 8
    public function getHeaderLine(string $name): ?string
55
    {
56 8
        return $this->headers[$name] ?? null;
57
    }
58
    public function getHeaders(): array
59
    {
60
        return $this->headers;
61
    }
62
63 4
    public function withStatusCode(?int $code = Status::OK): self
64
    {
65 4
        $clone = clone $this;
66 4
        $clone->setStatus($code);
67 4
        return $clone;
68
    }
69 8
    public function withHeader(string $name, string $value): self
70
    {
71 8
        $clone = clone $this;
72 8
        $clone->setHeader($name, $value);
73 8
        return $clone;
74
    }
75 5
    public function withFormat(?string $format, array $params = null): self
76
    {
77 5
        $clone = clone $this;
78 5
        $clone->setFormat($format, $params);
79 5
        return $clone;
80
    }
81 4
    public function withoutHeader(string $name): self
82
    {
83 4
        $clone = clone $this;
84 4
        $clone->unsetHeader($name);
85 4
        return $clone;
86
    }
87
88 4
    protected function unsetHeader(string $name): void
89
    {
90 4
        unset($this->headers[$name]);
91 4
    }
92 15
    protected function setHeader(string $name, ?string $value): void
93
    {
94 15
        if ($value === null) {
95
            $this->unsetHeader($name);
96
        } else {
97 15
            $this->headers[$name] = $value;
98
        }
99 15
    }
100 5
    protected function setFormat(?string $format, ?iterable $params): void
101
    {
102 5
        $this->format = $format;
103 5
        if ($params !== null) {
104 1
            $this->params = $params;
105
        }
106 5
    }
107 9
    protected function setStatus(?int $code): void
108
    {
109 9
        $this->code = $code;
110 9
    }
111
}
112