LoggerFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\HTTPlugModule\PluginFactory;
6
7
use Http\Client\Common\Plugin;
8
use Http\Client\Common\Plugin\CachePlugin;
9
use Http\Client\Common\Plugin\LoggerPlugin;
10
use Psr\Container\ContainerInterface;
11
use Psr\Log\LoggerInterface;
12
13
class LoggerFactory implements PluginFactory
14
{
15
    private ContainerInterface $container;
16
17
    public function __construct(ContainerInterface $container)
18 3
    {
19
        $this->container = $container;
20 3
    }
21 3
22
    /**
23
     * @param array<string, mixed> $config
24
     */
25
    public function createPlugin(array $config = []): Plugin
26
    {
27
        if (! class_exists(CachePlugin::class)) {
28 3
            throw new \LogicException('To use the logger plugin you need to install the "php-http/logger-plugin" package.');
29
        }
30 3
31
        $formatter = $config['formatter'] ?? null;
32
33
        return new LoggerPlugin(
34 3
            $this->container->get($config['logger'] ?? LoggerInterface::class),
35
            $formatter ? $this->container->get($formatter) : null
36 3
        );
37 3
    }
38
}
39