Passed
Push — master ( 16feba...6bc1c4 )
by Aleksei
02:17
created

DataBucket   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setHeader() 0 6 2
A withStatusCode() 0 5 1
A hasHeader() 0 3 1
A getHeaders() 0 3 1
A getFormat() 0 3 1
A getData() 0 3 1
A withoutHeader() 0 5 1
A unsetHeader() 0 3 1
A setFormat() 0 5 2
A getParams() 0 3 1
A isConvertable() 0 3 1
A __construct() 0 6 2
A getStatusCode() 0 3 1
A getHeaderLine() 0 3 1
A withFormat() 0 5 1
A withHeader() 0 5 1
A setStatusCode() 0 3 1
A hasFormat() 0 3 2
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 $statusCode = 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 162
    public function __construct($data, string $format = null, iterable $params = [])
20
    {
21 162
        $this->data = $data;
22 162
        if ($format !== null) {
23 2
            $this->format = $format;
24 2
            $this->params = $params;
25
        }
26 162
    }
27 13
    public function getStatusCode(): ?int
28
    {
29 13
        return $this->statusCode;
30
    }
31 66
    public function getFormat(): ?string
32
    {
33 66
        return $this->format;
34
    }
35 13
    public function getParams(): iterable
36
    {
37 13
        return $this->params;
38
    }
39 28
    public function getData()
40
    {
41 28
        return $this->data;
42
    }
43 103
    public function isConvertable(): bool
44
    {
45 103
        return static::IS_CONVERTABLE;
46
    }
47 60
    public function hasFormat(): bool
48
    {
49 60
        return static::IS_CONVERTABLE && $this->format !== null;
50
    }
51
52 12
    public function hasHeader(string $name): bool
53
    {
54 12
        return array_key_exists($name, $this->headers);
55
    }
56 19
    public function getHeaderLine(string $name): ?string
57
    {
58 19
        return $this->headers[$name] ?? null;
59
    }
60 9
    public function getHeaders(): array
61
    {
62 9
        return $this->headers;
63
    }
64
65 5
    public function withStatusCode(?int $code = Status::OK): self
66
    {
67 5
        $clone = clone $this;
68 5
        $clone->setStatusCode($code);
69 5
        return $clone;
70
    }
71 13
    public function withHeader(string $name, string $value): self
72
    {
73 13
        $clone = clone $this;
74 13
        $clone->setHeader($name, $value);
75 13
        return $clone;
76
    }
77 63
    public function withFormat(?string $format, array $params = null): self
78
    {
79 63
        $clone = clone $this;
80 63
        $clone->setFormat($format, $params);
81 63
        return $clone;
82
    }
83 4
    public function withoutHeader(string $name): self
84
    {
85 4
        $clone = clone $this;
86 4
        $clone->unsetHeader($name);
87 4
        return $clone;
88
    }
89
90 7
    protected function unsetHeader(string $name): void
91
    {
92 7
        unset($this->headers[$name]);
93 7
    }
94 49
    protected function setHeader(string $name, ?string $value): void
95
    {
96 49
        if ($value === null) {
97 3
            $this->unsetHeader($name);
98
        } else {
99 49
            $this->headers[$name] = $value;
100
        }
101 49
    }
102 63
    protected function setFormat(?string $format, ?iterable $params): void
103
    {
104 63
        $this->format = $format;
105 63
        if ($params !== null) {
106 10
            $this->params = $params;
107
        }
108 63
    }
109 14
    protected function setStatusCode(?int $code): void
110
    {
111 14
        $this->statusCode = $code;
112 14
    }
113
}
114