Test Failed
Pull Request — master (#814)
by Maxim
07:25
created

Options::isAutoAck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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