LoggerProvider::lookupLoggerAdapters()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 28
ccs 15
cts 16
cp 0.9375
rs 9.0111
c 0
b 0
f 0
cc 6
nc 7
nop 0
crap 6.0087
1
<?php
2
3
/*
4
 *  This file is part of the Micro framework package.
5
 *
6
 *  (c) Stanislau Komar <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace Micro\Plugin\Logger\Business\Provider;
13
14
use Micro\Framework\Kernel\KernelInterface;
15
use Micro\Plugin\Logger\Configuration\LoggerPluginConfigurationInterface;
16
use Micro\Plugin\Logger\Configuration\LoggerProviderTypeConfigurationInterface;
17
use Micro\Plugin\Logger\Exception\LoggerAdapterAlreadyExistsException;
18
use Micro\Plugin\Logger\Exception\LoggerAdapterNameInvalidException;
19
use Micro\Plugin\Logger\Exception\LoggerAdapterNotRegisteredException;
20
use Micro\Plugin\Logger\Plugin\LoggerProviderPluginInterface;
21
use Psr\Log\LoggerInterface;
22
23
class LoggerProvider implements LoggerProviderInterface
24
{
25
    /**
26
     * @var array<string, LoggerInterface>
27
     */
28
    private array $loggerCollection;
29
    /**
30
     * @var array<string, LoggerProviderPluginInterface>
31
     */
32
    private array $loggerAdapters;
33
34 7
    public function __construct(
35
        private readonly KernelInterface $kernel,
36
        private readonly LoggerPluginConfigurationInterface $loggerPluginConfiguration
37
    ) {
38 7
        $this->loggerCollection = [];
39 7
        $this->loggerAdapters = [];
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 6
    public function getLogger(string $loggerName): LoggerInterface
46
    {
47 6
        if (\array_key_exists($loggerName, $this->loggerCollection)) {
48 2
            return $this->loggerCollection[$loggerName];
49
        }
50
51 6
        $loggerProviderTypeConfig = $this->loggerPluginConfiguration->getLoggerProviderTypeConfig($loggerName);
52 6
        $this->lookupLoggerAdapters();
53 3
        $logger = $this->createLogger($loggerProviderTypeConfig);
54
55 2
        $this->loggerCollection[$loggerName] = $logger;
56
57 2
        return $logger;
58
    }
59
60 3
    protected function createLogger(
61
        LoggerProviderTypeConfigurationInterface $loggerProviderTypeConfig,
62
    ): LoggerInterface {
63 3
        $loggerAdapterRegistered = array_keys($this->loggerAdapters);
64
65 3
        if (1 === \count($loggerAdapterRegistered)) {
66 1
            return $this->loggerAdapters[array_pop($loggerAdapterRegistered)]
67 1
                ->getLoggerFactory()
68 1
                ->create($loggerProviderTypeConfig);
69
        }
70
71 2
        $adapterType = mb_strtolower($loggerProviderTypeConfig->getType());
72
73 2
        foreach ($this->loggerAdapters as $providerName => $loggerAdapter) {
74 2
            if (mb_strtolower($providerName) === $adapterType) {
75 1
                return $loggerAdapter
76 1
                    ->getLoggerFactory()
77 1
                    ->create($loggerProviderTypeConfig);
78
            }
79
        }
80
81 1
        $loggerAdapterRegisteredImploded = implode(', ', $loggerAdapterRegistered);
82
83 1
        throw new LoggerAdapterNotRegisteredException(<<<EOF
84 1
                Logger adapter `$adapterType` is not registered. You may have forgotten to install a required plugin.
85 1
                Installed adapters: $loggerAdapterRegisteredImploded
86 1
                EOF);
87
    }
88
89 6
    protected function lookupLoggerAdapters(): void
90
    {
91 6
        if ($this->loggerAdapters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->loggerAdapters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
92
            return;
93
        }
94
95 6
        $iterator = $this->kernel->plugins(LoggerProviderPluginInterface::class);
96 6
        $installed = false;
97
        /** @var LoggerProviderPluginInterface $adapter */
98 6
        foreach ($iterator as $adapter) {
99 5
            $adapterName = trim($adapter->getLoggerAdapterName());
100 5
            if (!$adapterName) {
101 1
                throw new LoggerAdapterNameInvalidException(sprintf('Logger adapter `%s::getLoggerAdapterName()` is empty.', \get_class($adapter)));
102
            }
103
104 4
            if (\array_key_exists($adapterName, $this->loggerAdapters)) {
105 1
                throw new LoggerAdapterAlreadyExistsException(sprintf('Logger adapter with alias `%s` already exists.', $adapterName));
106
            }
107
108 4
            $this->loggerAdapters[$adapterName] = $adapter;
109 4
            $installed = true;
110
        }
111
112 4
        if ($installed) {
113 3
            return;
114
        }
115
116 1
        throw new LoggerAdapterNotRegisteredException(<<<EOF
117
                There are no logger adapters available.
118
                You should install one of the logger adapter plugin.
119
                We recommend using the package `micro/plugin-logger-monolog`.
120 1
                EOF);
121
    }
122
}
123