Passed
Pull Request — master (#68)
by Witold
01:56
created

canCreateServiceWithName()   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 Interop\Container\Exception\NotFoundException;
10
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
11
12
class MonologServiceAbstractFactory implements AbstractFactoryInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $config;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 2
    public function canCreate(ContainerInterface $container, $requestedName)
23
    {
24 2
        $config = $this->getConfig($container);
25 2
        return isset($config[$requestedName]);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
32
    {
33 2
        return $this->createLogger($container, $requestedName);
34
    }
35
36 2
    private function createLogger($container, $requestedName)
37
    {
38 2
        $config = $this->getConfig($container);
39
40 2
        $factory = new MonologServiceFactory();
41 2
        return $factory->createLogger($container, new MonologOptions($config[$requestedName]));
42
    }
43
44
    /**
45
     * @throws NotFoundException
46
     */
47 6
    public function getConfig(ContainerInterface $container): array
48
    {
49 6
        if (null !== $this->config) {
50 3
            return $this->config;
51
        }
52
53 5
        $config = $container->get('config');
54
55 5
        if (isset($config['EnliteMonolog'])) {
56 4
            $this->config = $config['EnliteMonolog'];
57
        } else {
58 1
            $this->config = array();
59
        }
60
61 5
        return $this->config;
62
    }
63
64
    /**
65
     * @param array $config
66
     */
67 1
    public function setConfig(array $config): void
68
    {
69 1
        $this->config = $config;
70 1
    }
71
}
72