Passed
Pull Request — master (#240)
by Wilmer
16:10
created

LazyConnectionDependencies::schemaCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
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\Profiler\ProfilerInterface;
13
14
final class LazyConnectionDependencies
15
{
16
    private ContainerInterface $container;
17
    private ?LoggerInterface $logger = null;
18
    private ?ProfilerInterface $profiler = null;
19
    private ?QueryCache $queryCache = null;
20
21
    public function __construct(ContainerInterface $container)
22
    {
23 3016
        $this->container = $container;
24
    }
25 3016
26 3016
    /**
27
     * Get `LoggerInterface` instance.
28
     *
29
     * @psalm-suppress InvalidReturnStatement
30
     * @psalm-suppress InvalidReturnType
31
     *
32
     * @return LoggerInterface
33
     */
34
    public function logger(): LoggerInterface
35
    {
36
        if ($this->logger !== null) {
37
            return $this->logger;
38 1853
        }
39
40 1853
        $this->logger = $this->create(LoggerInterface::class);
41 1853
42
        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...
43
    }
44 1853
45
    /**
46 1853
     * Get `ProfilerInterface` instance.
47
     *
48
     * @psalm-suppress InvalidReturnStatement
49
     * @psalm-suppress InvalidReturnType
50
     *
51
     * @return ProfilerInterface
52
     */
53
    public function profiler(): ProfilerInterface
54
    {
55
        if ($this->profiler !== null) {
56
            return $this->profiler;
57
        }
58
59 1853
        $this->profiler = $this->create(ProfilerInterface::class);
60
        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...
61 1853
    }
62 1853
63
    /**
64
     * Get `QueryCache` instance.
65 1853
     *
66 1853
     * @psalm-suppress InvalidReturnStatement
67
     * @psalm-suppress InvalidReturnType
68
     *
69
     * @return QueryCache
70
     */
71
    public function queryCache(): QueryCache
72
    {
73
        if ($this->queryCache !== null) {
74
            return $this->queryCache;
75
        }
76
77
        $this->queryCache = $this->create(QueryCache::class);
78
        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...
79 1697
    }
80
81 1697
    /**
82 1397
     * Creates an instance of the specified class.
83
     *
84
     * @param string $class
85 1697
     *
86 1697
     * @throws RuntimeException If the created object is not an instance of the `LoggerInterface`.
87
     *
88
     * @return LoggerInterface|ProfilerInterface|QueryCache|SchemaCache The created instance.
89
     */
90
    private function create(string $class): object
91
    {
92
        $instance = $this->container->get($class);
93
94
        if (!($instance instanceof $class)) {
95
            throw new RuntimeException(sprintf(
96
                'The "%s" is not an instance of the "%s".',
97
                (is_object($instance) ? get_class($instance) : gettype($instance)),
98
                $class
99 1627
            ));
100
        }
101 1627
102 1567
        return $instance;
103
    }
104
}
105