Passed
Push — master ( 947b70...265dd0 )
by Alexander
10:32
created

LazyConnectionDependencies   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 117
ccs 27
cts 30
cp 0.9
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A profiler() 0 8 2
A logger() 0 9 2
A queryCache() 0 8 2
A create() 0 13 3
A schemaCache() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Connection;
6
7
use Psr\Log\LoggerInterface;
8
use RuntimeException;
9
use Yiisoft\Db\Cache\QueryCache;
10
use Yiisoft\Db\Cache\SchemaCache;
11
use Yiisoft\Factory\DependencyResolver;
12
use Yiisoft\Factory\Exception\InvalidConfigException;
13
use Yiisoft\Profiler\ProfilerInterface;
14
15
final class LazyConnectionDependencies
16
{
17
    private DependencyResolver $dependencyResolver;
18
    private ?LoggerInterface $logger = null;
19
    private ?ProfilerInterface $profiler = null;
20
    private ?QueryCache $queryCache = null;
21
    private ?SchemaCache $schemaCache = null;
22
23 3016
    public function __construct(DependencyResolver $dependencyResolver)
24
    {
25 3016
        $this->dependencyResolver = $dependencyResolver;
26 3016
    }
27
28
    /**
29
     * Get `LoggerInterface` instance.
30
     *
31
     * @throws InvalidConfigException
32
     *
33
     * @psalm-suppress InvalidReturnStatement
34
     * @psalm-suppress InvalidReturnType
35
     *
36
     * @return LoggerInterface
37
     */
38 1853
    public function logger(): LoggerInterface
39
    {
40 1853
        if ($this->logger !== null) {
41 1853
            return $this->logger;
42
        }
43
44 1853
        $this->logger = $this->create(LoggerInterface::class);
45
46 1853
        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...
47
    }
48
49
    /**
50
     * Get `ProfilerInterface` instance.
51
     *
52
     * @throws InvalidConfigException
53
     *
54
     * @psalm-suppress InvalidReturnStatement
55
     * @psalm-suppress InvalidReturnType
56
     *
57
     * @return ProfilerInterface
58
     */
59 1853
    public function profiler(): ProfilerInterface
60
    {
61 1853
        if ($this->profiler !== null) {
62 1853
            return $this->profiler;
63
        }
64
65 1853
        $this->profiler = $this->create(ProfilerInterface::class);
66 1853
        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...
67
    }
68
69
    /**
70
     * Get `QueryCache` instance.
71
     *
72
     * @throws InvalidConfigException
73
     *
74
     * @psalm-suppress InvalidReturnStatement
75
     * @psalm-suppress InvalidReturnType
76
     *
77
     * @return QueryCache
78
     */
79 1697
    public function queryCache(): QueryCache
80
    {
81 1697
        if ($this->queryCache !== null) {
82 1397
            return $this->queryCache;
83
        }
84
85 1697
        $this->queryCache = $this->create(QueryCache::class);
86 1697
        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...
87
    }
88
89
    /**
90
     * Get `SchemaCache` instance.
91
     *
92
     * @throws InvalidConfigException
93
     *
94
     * @psalm-suppress InvalidReturnStatement
95
     * @psalm-suppress InvalidReturnType
96
     *
97
     * @return SchemaCache
98
     */
99 1627
    public function schemaCache(): SchemaCache
100
    {
101 1627
        if ($this->schemaCache !== null) {
102 1567
            return $this->schemaCache;
103
        }
104
105 1627
        $this->schemaCache = $this->create(SchemaCache::class);
106 1627
        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...
107
    }
108
109
    /**
110
     * Creates an instance of the specified class.
111
     *
112
     * @param string $class
113
     *
114
     * @throws InvalidConfigException
115
     * @throws RuntimeException If the created object is not an instance of the `LoggerInterface`.
116
     *
117
     * @return LoggerInterface|ProfilerInterface|QueryCache|SchemaCache The created instance.
118
     */
119 1873
    private function create(string $class): object
120
    {
121 1873
        $instance = $this->dependencyResolver->get($class);
122
123 1873
        if (!($instance instanceof $class)) {
124
            throw new RuntimeException(sprintf(
125
                'The "%s" is not an instance of the "%s".',
126
                (is_object($instance) ? get_class($instance) : gettype($instance)),
127
                $class
128
            ));
129
        }
130
131 1873
        return $instance;
132
    }
133
}
134