Passed
Push — master ( 823aee...b9b37f )
by Alec
13:15 queued 12s
created

AProgressWidgetFactory::createProcedureWidget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 2
b 0
f 0
nc 1
nop 5
dl 0
loc 27
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
// 24.02.23
5
6
namespace AlecRabbit\Spinner\Extras\A;
7
8
use AlecRabbit\Spinner\Contract\IFrame;
9
use AlecRabbit\Spinner\Contract\IFrameCollection;
10
use AlecRabbit\Spinner\Contract\IInterval;
11
use AlecRabbit\Spinner\Contract\IProcedure;
12
use AlecRabbit\Spinner\Core\Factory\A\AWidgetFactory;
13
use AlecRabbit\Spinner\Core\Factory\FrameFactory;
14
use AlecRabbit\Spinner\Core\Factory\RevolverFactory;
15
use AlecRabbit\Spinner\Core\FrameRenderer;
16
use AlecRabbit\Spinner\Core\Pattern\Char\Custom;
17
use AlecRabbit\Spinner\Core\Revolver\Contract\IRevolver;
18
use AlecRabbit\Spinner\Core\Widget\Contract\IWidgetComposite;
19
use AlecRabbit\Spinner\Extras\Contract\IProgressBarSprite;
20
use AlecRabbit\Spinner\Extras\Contract\IProgressValue;
21
use AlecRabbit\Spinner\Extras\Contract\IProgressWidgetFactory;
22
use AlecRabbit\Spinner\Extras\Procedure\ProgressBarProcedure;
23
use AlecRabbit\Spinner\Extras\Procedure\ProgressFrameProcedure;
24
use AlecRabbit\Spinner\Extras\Procedure\ProgressStepsProcedure;
25
use AlecRabbit\Spinner\Extras\Procedure\ProgressValueProcedure;
26
use AlecRabbit\Spinner\Extras\ProgressBarSprite;
0 ignored issues
show
Bug introduced by
The type AlecRabbit\Spinner\Extras\ProgressBarSprite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use AlecRabbit\Spinner\Extras\Revolver\ProceduralRevolver;
28
29
abstract class AProgressWidgetFactory extends AWidgetFactory implements IProgressWidgetFactory
30
{
31
    public static function createProgressSteps(
32
        IProgressValue $progressValue,
33
        ?IInterval $updateInterval = null,
34
        ?IFrame $leadingSpacer = null,
35
        ?IFrame $trailingSpacer = null,
36
    ): IWidgetComposite {
37
        $updateInterval ??= static::getDefaultUpdateInterval();
38
39
        $revolver =
40
            static::getWidgetRevolverBuilder()
41
                ->withCharRevolver(
42
                    new ProceduralRevolver(
43
                        new ProgressStepsProcedure(
44
                            $progressValue,
45
                        ),
46
                        $updateInterval
47
                    )
48
                )
49
                ->build();
50
51
        return
52
            static::create(
53
                $revolver,
54
                $leadingSpacer,
55
                $trailingSpacer
56
            );
57
    }
58
59
    public static function createProgressBar(
60
        IProgressValue $progressValue,
61
        ?IProgressBarSprite $sprite = null,
62
        ?IInterval $updateInterval = null,
63
        ?IFrame $leadingSpacer = null,
64
        ?IFrame $trailingSpacer = null,
65
    ): IWidgetComposite {
66
        $sprite ??= new ProgressBarSprite();
67
68
        $procedure =
69
            new ProgressBarProcedure(
70
                $progressValue,
71
                $sprite,
72
            );
73
74
        return
75
            static::createProcedureWidget(
76
                $procedure,
77
                $updateInterval,
78
                $leadingSpacer,
79
                $trailingSpacer
80
            );
81
    }
82
83
    public static function createProcedureWidget(
84
        IProcedure $procedure,
85
        ?IInterval $updateInterval = null,
86
        ?IFrame $leadingSpacer = null,
87
        ?IFrame $trailingSpacer = null,
88
        ?IRevolver $styleRevolver = null,
89
    ): IWidgetComposite {
90
        $updateInterval ??= static::getDefaultUpdateInterval();
91
92
        $revolver =
93
            static::getWidgetRevolverBuilder()
94
                ->withStyleRevolver(
95
                    $styleRevolver ?? RevolverFactory::defaultStyleRevolver()
96
                )
97
                ->withCharRevolver(
98
                    new ProceduralRevolver(
99
                        $procedure,
100
                        $updateInterval
101
                    )
102
                )
103
                ->build();
104
105
        return
106
            static::create(
107
                $revolver,
108
                $leadingSpacer,
109
                $trailingSpacer
110
            );
111
    }
112
113
    public static function createProgressFrame(
114
        IProgressValue $progressValue,
115
        ?IFrameCollection $frames = null,
116
        ?IInterval $updateInterval = null,
117
        ?IFrame $leadingSpacer = null,
118
        ?IFrame $trailingSpacer = null,
119
    ): IWidgetComposite {
120
        $frames ??= self::defaultFrames();
121
122
        $procedure =
123
            new ProgressFrameProcedure(
124
                $progressValue,
125
                $frames,
126
            );
127
128
        return
129
            static::createProcedureWidget(
130
                $procedure,
131
                $updateInterval,
132
                $leadingSpacer,
133
                $trailingSpacer
134
            );
135
    }
136
137
    private static function defaultFrames(): IFrameCollection
138
    {
139
        $pattern =
140
            new Custom([
141
                FrameFactory::create(' ', 1),
142
                FrameFactory::create('▁', 1),
143
                FrameFactory::create('▂', 1),
144
                FrameFactory::create('▃', 1),
145
                FrameFactory::create('▄', 1),
146
                FrameFactory::create('▅', 1),
147
                FrameFactory::create('▆', 1),
148
                FrameFactory::create('▇', 1),
149
                FrameFactory::create('█', 1),
150
            ]);
151
152
        return
153
            (new FrameRenderer($pattern))->render();
154
    }
155
156
    public static function createProgressValue(
157
        IProgressValue $progressValue,
158
        ?string $format = null,
159
        ?IInterval $updateInterval = null,
160
        ?IFrame $leadingSpacer = null,
161
        ?IFrame $trailingSpacer = null,
162
    ): IWidgetComposite {
163
        $procedure =
164
            new ProgressValueProcedure(
165
                $progressValue,
166
                $format
167
            );
168
169
        return
170
            static::createProcedureWidget(
171
                $procedure,
172
                $updateInterval,
173
                $leadingSpacer,
174
                $trailingSpacer
175
            );
176
    }
177
}
178