Passed
Pull Request — master (#57)
by Witold
07:04
created

ithName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * @author Evgeny Shpilevsky <[email protected]>
4
 */
5
6
namespace EnliteMonolog\Service;
7
8
use Interop\Container\ContainerInterface;
9
use Monolog\Logger;
10
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
11
use Zend\ServiceManager\ServiceLocatorInterface;
12
13
final class MonologServiceAbstractFactory implements AbstractFactoryInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $config;
19
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 3
    public function canCreate(ContainerInterface $container, $requestedName)
25
    {
26 3
        return $this->has($container, $requestedName);
27
    }
28
29
    /**
30
     * @param ServiceLocatorInterface|ContainerInterface $container
31
     * @param $requestedName
32
     * @return bool
33
     */
34 3
    private function has($container, $requestedName)
35
    {
36 3
        $config = $this->getConfig($container);
37 3
        return isset($config[$requestedName]);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 3
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
44
    {
45 3
        return $this->createLogger($container, $requestedName);
46
    }
47
48 3
    private function createLogger($container, $requestedName): Logger
49
    {
50 3
        $config = $this->getConfig($container);
51
52 3
        $factory = new MonologServiceFactory();
53 3
        return $factory->createLogger($container, new MonologOptions($config[$requestedName]));
54
    }
55
56
    /**
57
     * @param ServiceLocatorInterface|ContainerInterface $container
58
     */
59 8
    public function getConfig($container): array
60
    {
61 8
        if (null !== $this->config) {
62 3
            return $this->config;
63
        }
64
65 7
        $config = $container->get('config');
66
67 7
        $this->config = $config['EnliteMonolog'] ?? [];
68
69 7
        return $this->config;
70
    }
71
72 1
    public function setConfig(array $config): void
73
    {
74 1
        $this->config = $config;
75 1
    }
76
}
77