Passed
Push — master ( de3d61...be839c )
by Alec
13:42 queued 13s
created

StyleFrameFactory::refineSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Factory;
6
7
use AlecRabbit\Spinner\Contract\IFrame;
8
use AlecRabbit\Spinner\Core\Contract\IWidthMeasurer;
9
use AlecRabbit\Spinner\Core\StyleFrame;
10
11
final class StyleFrameFactory implements Contract\IStyleFrameFactory
12
{
13
    public function __construct(
14
        protected IWidthMeasurer $widthMeasurer,
15
    ) {
16
    }
17
18
    public function create(string $sequence, ?int $width = null): IFrame
19
    {
20
        return
21
            new StyleFrame(
22
                $sequence,
23
                $width ?? $this->measure($sequence)
24
            );
25
    }
26
27
    protected function measure(string $sequence): int
28
    {
29
        return
30
            $this->widthMeasurer->measureWidth(
31
                $this->refineSequence($sequence)
32
            );
33
    }
34
35
    protected function refineSequence(string $sequence): string|array
36
    {
37
        return str_replace('%s', '', $sequence);
38
    }
39
}
40