Passed
Push — master ( 2dffc4...9bc131 )
by Alexander
02:20
created

BaseProviderCollector::clear()   A

Complexity

Conditions 6
Paths 11

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.2222
cc 6
nc 11
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Provider\Support;
6
7
use Psr\Container\ContainerInterface;
8
use RuntimeException;
9
use SplFixedArray;
10
use Yiisoft\Yii\Cycle\Exception\CumulativeException;
11
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;
12
13
abstract class BaseProviderCollector implements SchemaProviderInterface
14
{
15
    protected const IS_SEQUENCE_PIPELINE = true;
16
17
    /** @var SplFixedArray<DeferredSchemaProviderDecorator>|null */
18
    protected ?SplFixedArray $providers = null;
19
    private ContainerInterface $container;
20
21 44
    public function __construct(ContainerInterface $container)
22
    {
23 44
        $this->container = $container;
24 44
    }
25
26
    /**
27
     * @return $this
28
     *
29
     * @psalm-immutable
30
     */
31 40
    public function withConfig(array $config): self
32
    {
33 40
        $new = clone $this;
34 40
        $new->providers = $this->createSequence($new->container, $config);
35 40
        return $new;
36
    }
37
38 13
    public function clear(): bool
39
    {
40 13
        if ($this->providers === null) {
41 2
            throw new RuntimeException(self::class . ' is not configured.');
42
        }
43 11
        $exceptions = [];
44 11
        $result = false;
45 11
        foreach ($this->providers as $provider) {
46
            try {
47 9
                $result = $provider->clear() || $result;
48 2
            } catch (\Throwable $e) {
49 2
                $exceptions[] = $e;
50
            }
51
        }
52 11
        if (count($exceptions)) {
53 2
            throw new CumulativeException(...$exceptions);
54
        }
55 9
        return $result;
56
    }
57
58 40
    private function createSequence(ContainerInterface $container, array $providers): SplFixedArray
59
    {
60 40
        $size = count($providers);
61 40
        $stack = new SplFixedArray($size);
62 40
        $nextProvider = null;
63 40
        foreach (array_reverse($providers) as $key => $definition) {
64 32
            $config = [];
65 32
            if (is_array($definition)) {
66 6
                if (is_string($key)) {
67 3
                    $config = $definition;
68 3
                    $definition = $key;
69
                } else {
70 3
                    $config = $definition[1] ?? [];
71 3
                    $definition = $definition[0];
72
                }
73
            }
74 32
            $nextProvider = (new DeferredSchemaProviderDecorator(
75 32
                $container,
76
                $definition,
77 32
                static::IS_SEQUENCE_PIPELINE ? $nextProvider : null
78 32
            ))->withConfig($config);
79 32
            $stack[--$size] = $nextProvider;
80
        }
81 40
        return $stack;
82
    }
83
}
84