Passed
Pull Request — master (#68)
by Aleksei
02:29
created

DeferredSchemaProviderDecorator::getProvider()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 7
nop 0
crap 5
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 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 ?self $nextProvider;
17
    private ?SchemaProviderInterface $latestProvider = null;
18
    private bool $resolved = false;
19
    private ContainerInterface $container;
20
21
    /**
22
     * @param ContainerInterface $container
23
     * @param $provider
24
     * @param null|self $nextProvider
25
     */
26 14
    public function __construct(ContainerInterface $container, $provider, ?self $nextProvider)
27
    {
28 14
        $this->provider = $provider;
29 14
        $this->container = $container;
30 14
        $this->nextProvider = $nextProvider;
31 14
    }
32 14
    public function withConfig(array $config): self
33
    {
34 14
        $provider = !$this->resolved && count($this->config) === 0 ? $this->provider : $this->getProvider();
35 14
        $new = new self($this->container, $provider, $this->nextProvider);
36 14
        $new->config = $config;
37 14
        return $new;
38
    }
39 11
    public function read(?SchemaProviderInterface $latestProvider = null): ?array
40
    {
41 11
        $latestProvider = $latestProvider ?? $this->latestProvider;
42 11
        if ($latestProvider !== null && $this->nextProvider !== null) {
43 1
            $nextProvider = $this->nextProvider->withLatestProvider($latestProvider);
44
        } else {
45 11
            $nextProvider = $this->nextProvider ?? $latestProvider;
46
        }
47 11
        return $this->getProvider()->read($nextProvider);
48
    }
49 3
    public function clear(): bool
50
    {
51 3
        return $this->getProvider()->clear();
52
    }
53
54
    /**
55
     * @psalm-suppress InvalidReturnType,InvalidReturnStatement
56
     */
57 14
    private function getProvider(): SchemaProviderInterface
58
    {
59 14
        if ($this->resolved) {
60 1
            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...
61
        }
62 14
        $provider = $this->provider;
63 14
        if (is_string($provider)) {
64 6
            $provider = $this->container->get($provider);
65
        }
66 14
        if (!$provider instanceof SchemaProviderInterface) {
67 1
            throw new BadDeclarationException('Provider', SchemaProviderInterface::class, $provider);
68
        }
69 13
        $this->provider = count($this->config) > 0 ? $provider->withConfig($this->config) : $provider;
70 13
        $this->resolved = true;
71 13
        return $this->provider;
72
    }
73 1
    private function withLatestProvider(SchemaProviderInterface $provider): self
74
    {
75
        // resolve provider
76 1
        $this->getProvider();
77 1
        $new = clone $this;
78 1
        $new->latestProvider = $provider;
79 1
        return $new;
80
    }
81
}
82