Passed
Push — master ( b9b37f...a27485 )
by Alec
02:44 queued 14s
created

AFrameCollectionRenderer::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
// 10.03.23
5
6
namespace AlecRabbit\Spinner\Core\A;
7
8
use AlecRabbit\Spinner\Contract\IFrame;
9
use AlecRabbit\Spinner\Contract\IFrameCollection;
10
use AlecRabbit\Spinner\Contract\IFrameCollectionRenderer;
11
use AlecRabbit\Spinner\Contract\IPattern;
12
use AlecRabbit\Spinner\Core\Factory\A\ADefaultsAwareClass;
13
use AlecRabbit\Spinner\Core\FrameCollection;
14
use AlecRabbit\Spinner\Exception\InvalidArgumentException;
15
use Generator;
16
use Stringable;
17
18
abstract class AFrameCollectionRenderer extends ADefaultsAwareClass implements IFrameCollectionRenderer
19
{
20
    protected ?IPattern $pattern = null;
21
22
    /** @inheritdoc */
23
    public function pattern(IPattern $pattern): IFrameCollectionRenderer
24
    {
25
        $clone = clone $this;
26
        $clone->pattern = $pattern;
27
        return $clone;
28
    }
29
30
    /** @inheritdoc */
31
    public function render(): IFrameCollection
32
    {
33
        $cb =
34
            /**
35
             * @return Generator<IFrame>
36
             * @throws InvalidArgumentException
37
             */
38
            function (): Generator {
39
                if (null === $this->pattern) {
40
                    throw new InvalidArgumentException('Pattern is not set.');
41
                }
42
                /** @var IFrame|Stringable|string|int|array<string,int|null> $entry */
43
                foreach ($this->pattern->getPattern() as $entry) {
44
                    if ($entry instanceof IFrame) {
45
                        yield $entry;
46
                        continue;
47
                    }
48
                    yield $this->create($entry);
49
                }
50
            };
51
52
        return
53
            new FrameCollection($cb());
54
    }
55
56
    /**
57
     * @throws InvalidArgumentException
58
     */
59
    protected function create(Stringable|string|int|array $entry): IFrame
60
    {
61
        if ($entry instanceof Stringable) {
0 ignored issues
show
introduced by
$entry is never a sub-type of Stringable.
Loading history...
62
            $entry = (string)$entry;
63
        }
64
65
        if (is_string($entry) || is_int($entry)) {
0 ignored issues
show
introduced by
The condition is_int($entry) is always false.
Loading history...
66
            return
67
                $this->createFrame($entry);
68
        }
69
70
        return $this->createFromArray($entry);
71
    }
72
73
    /**
74
     * @throws InvalidArgumentException
75
     */
76
    abstract protected function createFrame(int|string $entry): IFrame;
77
78
    /**
79
     * @throws InvalidArgumentException
80
     */
81
    abstract protected function createFromArray(array $entry): IFrame;
82
}
83