Passed
Pull Request — master (#215)
by Wilmer
14:22
created

LazyConnectionDependencies::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Connection;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Log\LoggerInterface;
9
use RuntimeException;
10
use Yiisoft\Db\Cache\QueryCache;
11
use Yiisoft\Db\Cache\SchemaCache;
12
use Yiisoft\Factory\Definitions\Normalizer;
13
use Yiisoft\Factory\Exceptions\InvalidConfigException;
14
use Yiisoft\Profiler\ProfilerInterface;
15
16
final class LazyConnectionDependencies
17
{
18
    private ContainerInterface $container;
19
    private ?LoggerInterface $logger = null;
20
    private ?ProfilerInterface $profiler = null;
21
    private ?QueryCache $queryCache = null;
22
    private ?SchemaCache $schemaCache = null;
23
24
    public function __construct(ContainerInterface $container)
25
    {
26
        $this->container = $container;
27
    }
28
29
    /**
30
     * Get `LoggerInterface` instance.
31
     *
32
     * @throws InvalidConfigException
33
     *
34
     * @psalm-suppress InvalidReturnStatement
35
     * @psalm-suppress InvalidReturnType
36
     *
37
     * @return LoggerInterface
38
     */
39
    public function logger(): LoggerInterface
40
    {
41
        if ($this->logger !== null) {
42
            return $this->logger;
43
        }
44
45
        $this->logger = $this->create(LoggerInterface::class);
46
47
        return $this->logger;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->logger returns the type null which is incompatible with the type-hinted return Psr\Log\LoggerInterface.
Loading history...
48
    }
49
50
    /**
51
     * Get `ProfilerInterface` instance.
52
     *
53
     * @throws InvalidConfigException
54
     *
55
     * @psalm-suppress InvalidReturnStatement
56
     * @psalm-suppress InvalidReturnType
57
     *
58
     * @return ProfilerInterface
59
     */
60
    public function profiler(): ProfilerInterface
61
    {
62
        if ($this->profiler !== null) {
63
            return $this->profiler;
64
        }
65
66
        $this->profiler = $this->create(ProfilerInterface::class);
67
        return $this->profiler;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->profiler returns the type null which is incompatible with the type-hinted return Yiisoft\Profiler\ProfilerInterface.
Loading history...
68
    }
69
70
    /**
71
     * Get `QueryCache` instance.
72
     *
73
     * @throws InvalidConfigException
74
     *
75
     * @psalm-suppress InvalidReturnStatement
76
     * @psalm-suppress InvalidReturnType
77
     *
78
     * @return QueryCache
79
     */
80
    public function queryCache(): QueryCache
81
    {
82
        if ($this->queryCache !== null) {
83
            return $this->queryCache;
84
        }
85
86
        $this->queryCache = $this->create(QueryCache::class);
87
        return $this->queryCache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queryCache returns the type null which is incompatible with the type-hinted return Yiisoft\Db\Cache\QueryCache.
Loading history...
88
    }
89
90
    /**
91
     * Get `SchemaCache` instance.
92
     *
93
     * @throws InvalidConfigException
94
     *
95
     * @psalm-suppress InvalidReturnStatement
96
     * @psalm-suppress InvalidReturnType
97
     *
98
     * @return SchemaCache
99
     */
100
    public function schemaCache(): SchemaCache
101
    {
102
        if ($this->schemaCache !== null) {
103
            return $this->schemaCache;
104
        }
105
106
        $this->schemaCache = $this->create(SchemaCache::class);
107
        return $this->schemaCache;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->schemaCache returns the type null which is incompatible with the type-hinted return Yiisoft\Db\Cache\SchemaCache.
Loading history...
108
    }
109
110
    /**
111
     * Creates an instance of the specified class.
112
     *
113
     * @param string $class
114
     *
115
     * @throws InvalidConfigException
116
     * @throws RuntimeException If the created object is not an instance of the `LoggerInterface`.
117
     *
118
     * @return LoggerInterface|ProfilerInterface|QueryCache|SchemaCache The created instance.
119
     *
120
     * @psalm-suppress RedundantConditionGivenDocblockType
121
     * @psalm-suppress DocblockTypeContradiction
122
     */
123
    private function create(string $class): object
124
    {
125
        $definition = Normalizer::normalize($class);
126
        $instance = $definition->resolve($this->container);
127
128
        if (!($instance instanceof $class)) {
129
            throw new RuntimeException(sprintf(
130
                'The "%s" is not an instance of the "%s".',
131
                (is_object($instance) ? get_class($instance) : gettype($instance)),
132
                $class
133
            ));
134
        }
135
136
        return $instance;
137
    }
138
}
139