|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AlecRabbit\Spinner\Core\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use AlecRabbit\Spinner\Contract\Output\IBufferedOutput; |
|
8
|
|
|
use AlecRabbit\Spinner\Core\Builder\Contract\ISequenceStateWriterBuilder; |
|
9
|
|
|
use AlecRabbit\Spinner\Core\Feature\Resolver\Contract\IInitializationResolver; |
|
10
|
|
|
use AlecRabbit\Spinner\Core\Output\Contract\IConsoleCursor; |
|
11
|
|
|
use AlecRabbit\Spinner\Core\Output\Contract\ISequenceStateWriter; |
|
12
|
|
|
use AlecRabbit\Spinner\Core\Output\SequenceStateWriter; |
|
13
|
|
|
use AlecRabbit\Spinner\Exception\LogicException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @psalm-suppress PossiblyNullArgument |
|
17
|
|
|
*/ |
|
18
|
|
|
final class SequenceStateWriterBuilder implements ISequenceStateWriterBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
private ?IBufferedOutput $bufferedOutput = null; |
|
21
|
|
|
private ?IConsoleCursor $cursor = null; |
|
22
|
|
|
private ?IInitializationResolver $initializationResolver = null; |
|
23
|
|
|
|
|
24
|
|
|
public function build(): ISequenceStateWriter |
|
25
|
|
|
{ |
|
26
|
|
|
$this->validate(); |
|
27
|
|
|
|
|
28
|
|
|
return new SequenceStateWriter( |
|
29
|
|
|
output: $this->bufferedOutput, |
|
|
|
|
|
|
30
|
|
|
cursor: $this->cursor, |
|
|
|
|
|
|
31
|
|
|
initializationResolver: $this->initializationResolver, |
|
|
|
|
|
|
32
|
|
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function validate(): void |
|
36
|
|
|
{ |
|
37
|
|
|
match (true) { |
|
38
|
|
|
$this->bufferedOutput === null => throw new LogicException('Output is not set.'), |
|
39
|
|
|
$this->cursor === null => throw new LogicException('Cursor is not set.'), |
|
40
|
|
|
$this->initializationResolver === null => throw new LogicException('Initialization resolver is not set.'), |
|
41
|
|
|
default => null, |
|
42
|
|
|
}; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function withOutput(IBufferedOutput $bufferedOutput): ISequenceStateWriterBuilder |
|
46
|
|
|
{ |
|
47
|
|
|
$clone = clone $this; |
|
48
|
|
|
$clone->bufferedOutput = $bufferedOutput; |
|
49
|
|
|
return $clone; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function withCursor(IConsoleCursor $cursor): ISequenceStateWriterBuilder |
|
53
|
|
|
{ |
|
54
|
|
|
$clone = clone $this; |
|
55
|
|
|
$clone->cursor = $cursor; |
|
56
|
|
|
return $clone; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function withInitializationResolver( |
|
60
|
|
|
IInitializationResolver $initializationResolver |
|
61
|
|
|
): ISequenceStateWriterBuilder { |
|
62
|
|
|
$clone = clone $this; |
|
63
|
|
|
$clone->initializationResolver = $initializationResolver; |
|
64
|
|
|
return $clone; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|