Passed
Pull Request — master (#814)
by Maxim
07:03
created

Options::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue;
6
7
class Options implements OptionsInterface, ExtendedOptionsInterface, \JsonSerializable
8
{
9
    /**
10
     * @var array<non-empty-string, array<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, array<string>> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, array<string>>.
Loading history...
11
     */
12
    private array $headers = [];
13
    private ?int $delay = null;
14
    private ?string $queue = null;
15
16 2
    public function withQueue(?string $queue): self
17
    {
18 2
        $options = clone $this;
19 2
        $options->queue = $queue;
20
21 2
        return $options;
22
    }
23
24 3
    public function getQueue(): ?string
25
    {
26 3
        return $this->queue;
27
    }
28
29 5
    public function withDelay(?int $delay): self
30
    {
31 5
        $options = clone $this;
32 5
        $options->delay = $delay;
33
34 5
        return $options;
35
    }
36
37 3
    public function getDelay(): ?int
38
    {
39 3
        return $this->delay;
40
    }
41
42
    /**
43
     * @return array<non-empty-string, array<string>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, array<string>> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, array<string>>.
Loading history...
44
     */
45 3
    public function getHeaders(): array
46
    {
47 3
        return $this->headers;
48
    }
49
50
    /**
51
     * @param non-empty-string $name Header field name.
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
52
     */
53 1
    public function hasHeader(string $name): bool
54
    {
55 1
        return isset($this->headers[$name]) && \count($this->headers[$name]) > 0;
56
    }
57
58
    /**
59
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
60
     * @return array<string>
61
     */
62 1
    public function getHeader(string $name): array
63
    {
64 1
        return $this->headers[$name] ?? [];
65
    }
66
67
    /**
68
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
69
     */
70 1
    public function getHeaderLine(string $name): string
71
    {
72 1
        return \implode(',', $this->getHeader($name));
73
    }
74
75
    /**
76
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
77
     * @param non-empty-string|array<non-empty-string> $value
78
     */
79 4
    public function withHeader(string $name, string|array $value): self
80
    {
81 4
        $value = \is_iterable($value) ? $value : [$value];
82
83 4
        $self = clone $this;
84 4
        $self->headers[$name] = [];
85
86 4
        foreach ($value as $item) {
87 4
            $self->headers[$name][] = $item;
88
        }
89
90 4
        return $self;
91
    }
92
93
    /**
94
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
95
     * @param non-empty-string|array<non-empty-string> $value
96
     */
97 1
    public function withAddedHeader(string $name, string|array $value): self
98
    {
99
        /** @var iterable<non-empty-string> $value */
100 1
        $value = \is_iterable($value) ? $value : [$value];
101
102
        /** @var array<non-empty-string> $headers */
103 1
        $headers = $this->headers[$name] ?? [];
104
105 1
        foreach ($value as $item) {
106 1
            $headers[] = $item;
107
        }
108
109 1
        return $this->withHeader($name, $headers);
110
    }
111
112
    /**
113
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
114
     */
115 1
    public function withoutHeader(string $name): self
116
    {
117 1
        if (!isset($this->headers[$name])) {
118
            return $this;
119
        }
120
121 1
        $self = clone $this;
122 1
        unset($self->headers[$name]);
123 1
        return $self;
124
    }
125
126 1
    public function jsonSerialize(): array
127
    {
128
        return [
129 1
            'delay' => $this->delay,
130 1
            'queue' => $this->queue,
131 1
            'headers' => $this->headers,
132
        ];
133
    }
134
135
    public static function delayed(int $delay): Options
136
    {
137
        $options = new self();
138
        $options->delay = $delay;
139
140
        return $options;
141
    }
142
143 3
    public static function onQueue(?string $queue): Options
144
    {
145 3
        $options = new self();
146 3
        $options->queue = $queue;
147
148 3
        return $options;
149
    }
150
}
151