Passed
Pull Request — master (#814)
by Maxim
06:42
created

Options::withPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
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, \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
21 2
    public function withQueue(?string $queue): self
22
    {
23 2
        $options = clone $this;
24 2
        $options->queue = $queue;
25
26 2
        return $options;
27
    }
28
29 3
    public function getQueue(): ?string
30
    {
31 3
        return $this->queue;
32
    }
33
34 5
    public function withDelay(?int $delay): self
35
    {
36 5
        $options = clone $this;
37 5
        $options->delay = $delay;
38
39 5
        return $options;
40
    }
41
42 3
    public function getDelay(): ?int
43
    {
44 3
        return $this->delay;
45
    }
46
47 2
    public function withPriority(int $priority): self
48
    {
49 2
        $options = clone $this;
50 2
        $options->priority = $priority;
51
52 2
        return $options;
53
    }
54
55 1
    public function getPriority(): int
56
    {
57 1
        return $this->priority;
58
    }
59
60 2
    public function autoAck(bool $autoAck = true): self
61
    {
62 2
        $options = clone $this;
63 2
        $options->autoAck = $autoAck;
64
65 2
        return $options;
66
    }
67
68 1
    public function isAutoAck(): bool
69
    {
70 1
        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 3
    public function getHeaders(): array
77
    {
78 3
        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 1
    public function hasHeader(string $name): bool
85
    {
86 1
        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 1
    public function getHeader(string $name): array
94
    {
95 1
        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 1
    public function getHeaderLine(string $name): string
102
    {
103 1
        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 4
    public function withHeader(string $name, string|array $value): self
111
    {
112 4
        $value = \is_iterable($value) ? $value : [$value];
113
114 4
        $self = clone $this;
115 4
        $self->headers[$name] = [];
116
117 4
        foreach ($value as $item) {
118 4
            $self->headers[$name][] = (string)$item;
119
        }
120
121 4
        return $self;
122
    }
123
124
    /**
125
     * @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...
126
     * @param non-empty-string|array<non-empty-string> $value
127
     */
128 1
    public function withAddedHeader(string $name, string|array $value): self
129
    {
130
        /** @var iterable<non-empty-string> $value */
131 1
        $value = \is_iterable($value) ? $value : [$value];
132
133
        /** @var array<non-empty-string> $headers */
134 1
        $headers = $this->headers[$name] ?? [];
135
136 1
        foreach ($value as $item) {
137 1
            $headers[] = $item;
138
        }
139
140 1
        return $this->withHeader($name, $headers);
141
    }
142
143
    /**
144
     * @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...
145
     */
146 1
    public function withoutHeader(string $name): self
147
    {
148 1
        if (!isset($this->headers[$name])) {
149
            return $this;
150
        }
151
152 1
        $self = clone $this;
153 1
        unset($self->headers[$name]);
154 1
        return $self;
155
    }
156
157 1
    public function jsonSerialize(): array
158
    {
159
        return [
160 1
            'delay' => $this->delay,
161 1
            'queue' => $this->queue,
162 1
            'priority' => $this->priority,
163 1
            'autoAck' => $this->autoAck,
164 1
            'headers' => $this->headers,
165
        ];
166
    }
167
168
    public static function delayed(int $delay): Options
169
    {
170
        $options = new self();
171
        $options->delay = $delay;
172
173
        return $options;
174
    }
175
176 3
    public static function onQueue(?string $queue): Options
177
    {
178 3
        $options = new self();
179 3
        $options->queue = $queue;
180
181 3
        return $options;
182
    }
183
}
184