Test Failed
Pull Request — master (#816)
by butschster
06:13 queued 32s
created

Options::withContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
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
    private array $context = [];
16 2
17
    public function withContext(array $context): self
18 2
    {
19 2
        $options = clone $this;
20
        $options->context = $context;
21 2
22
        return $options;
23
    }
24 3
25
    public function withQueue(?string $queue): self
26 3
    {
27
        $options = clone $this;
28
        $options->queue = $queue;
29 5
30
        return $options;
31 5
    }
32 5
33
    public function getQueue(): ?string
34 5
    {
35
        return $this->queue;
36
    }
37 3
38
    public function withDelay(?int $delay): self
39 3
    {
40
        $options = clone $this;
41
        $options->delay = $delay;
42
43
        return $options;
44
    }
45 3
46
    public function getDelay(): ?int
47 3
    {
48
        return $this->delay;
49
    }
50
51
    /**
52
     * @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...
53 1
     */
54
    public function getHeaders(): array
55 1
    {
56
        return $this->headers;
57
    }
58
59
    /**
60
     * @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...
61
     */
62 1
    public function hasHeader(string $name): bool
63
    {
64 1
        return isset($this->headers[$name]) && \count($this->headers[$name]) > 0;
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
     * @return array<string>
70 1
     */
71
    public function getHeader(string $name): array
72 1
    {
73
        return $this->headers[$name] ?? [];
74
    }
75
76
    /**
77
     * @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...
78
     */
79 4
    public function getHeaderLine(string $name): string
80
    {
81 4
        return \implode(',', $this->getHeader($name));
82
    }
83 4
84 4
    /**
85
     * @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...
86 4
     * @param non-empty-string|array<non-empty-string> $value
87 4
     */
88
    public function withHeader(string $name, string|array $value): self
89
    {
90 4
        $value = \is_iterable($value) ? $value : [$value];
91
92
        $self = clone $this;
93
        $self->headers[$name] = [];
94
95
        foreach ($value as $item) {
96
            $self->headers[$name][] = $item;
97 1
        }
98
99
        return $self;
100 1
    }
101
102
    /**
103 1
     * @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...
104
     * @param non-empty-string|array<non-empty-string> $value
105 1
     */
106 1
    public function withAddedHeader(string $name, string|array $value): self
107
    {
108
        /** @var iterable<non-empty-string> $value */
109 1
        $value = \is_iterable($value) ? $value : [$value];
110
111
        /** @var array<non-empty-string> $headers */
112
        $headers = $this->headers[$name] ?? [];
113
114
        foreach ($value as $item) {
115 1
            $headers[] = $item;
116
        }
117 1
118
        return $this->withHeader($name, $headers);
119
    }
120
121 1
    /**
122 1
     * @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...
123 1
     */
124
    public function withoutHeader(string $name): self
125
    {
126 1
        if (!isset($this->headers[$name])) {
127
            return $this;
128
        }
129 1
130 1
        $self = clone $this;
131 1
        unset($self->headers[$name]);
132
        return $self;
133
    }
134
135
    public function jsonSerialize(): array
136
    {
137
        return [
138
            'delay' => $this->delay,
139
            'queue' => $this->queue,
140
            'headers' => $this->headers,
141
        ];
142
    }
143 3
144
    public static function delayed(int $delay): Options
145 3
    {
146 3
        $options = new self();
147
        $options->delay = $delay;
148 3
149
        return $options;
150
    }
151
152
    public static function onQueue(?string $queue): Options
153
    {
154
        $options = new self();
155
        $options->queue = $queue;
156
157
        return $options;
158
    }
159
}
160