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

StyleFrameFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 6 1
A measure() 0 5 1
A refineSequence() 0 3 1
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