Passed
Pull Request — master (#68)
by Aleksei
04:14
created

DeferredSchemaProviderDecorator::withConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Provider;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Yii\Cycle\Exception\BadDeclarationException;
9
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;
10
11
final class DeferredSchemaProviderDecorator implements SchemaProviderInterface
12
{
13
    /** @var SchemaProviderInterface|string */
14
    private $provider;
15
    private array $config = [];
16
    private ?SchemaProviderInterface $nextProvider;
17
    private bool $resolved = false;
18
    private ContainerInterface $container;
19
20
    /**
21
     * @param ContainerInterface $container
22
     * @param $provider
23
     * @param null|SchemaProviderInterface $nextProvider
24
     */
25 12
    public function __construct(ContainerInterface $container, $provider, ?SchemaProviderInterface $nextProvider)
26
    {
27 12
        $this->provider = $provider;
28 12
        $this->container = $container;
29 12
        $this->nextProvider = $nextProvider;
30 12
    }
31
    /**
32
     * @psalm-suppress InvalidReturnType,InvalidReturnStatement
33
     */
34 12
    private function getProvider(): SchemaProviderInterface
35
    {
36 12
        if ($this->resolved) {
37
            return $this->provider;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->provider could return the type string which is incompatible with the type-hinted return Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface. Consider adding an additional type-check to rule them out.
Loading history...
38
        }
39 12
        $provider = $this->provider;
40 12
        if (is_string($provider)) {
41 5
            $provider = $this->container->get($provider);
42
        }
43 12
        if (!$provider instanceof SchemaProviderInterface) {
44 1
            throw new BadDeclarationException('Provider', SchemaProviderInterface::class, $provider);
45
        }
46 11
        $this->provider = count($this->config) > 0 ? $provider->withConfig($this->config) : $provider;
47 11
        $this->resolved = true;
48 11
        return $this->provider;
49
    }
50 12
    public function withConfig(array $config): SchemaProviderInterface
51
    {
52 12
        $provider = !$this->resolved && count($this->config) === 0 ? $this->provider : $this->getProvider();
53 12
        $new = new self($this->container, $provider, $this->nextProvider);
54 12
        $new->config = $config;
55 12
        return $new;
56
    }
57 9
    public function read(?SchemaProviderInterface $nextProvider = null): ?array
58
    {
59 9
        return $this->getProvider()->read($this->nextProvider);
60
    }
61 3
    public function clear(): bool
62
    {
63 3
        return $this->getProvider()->clear();
64
    }
65
}
66