Passed
Push — master ( 93bfd0...6b63dc )
by Timothy
34s
created

createServiceWithName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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 Zend\ServiceManager\AbstractFactoryInterface;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
class MonologServiceAbstractFactory implements AbstractFactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\AbstractFactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\AbstractFactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $config;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 1
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
23
    {
24 1
        return $this->has($serviceLocator, $requestedName);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function canCreate(ContainerInterface $container, $requestedName)
31
    {
32
        return $this->has($container, $requestedName);
33
    }
34
35
    /**
36
     * @param ServiceLocatorInterface|ContainerInterface $container
37
     * @param $requestedName
38
     * @return bool
39
     */
40 1
    private function has($container, $requestedName)
41
    {
42 1
        $config = $this->getConfig($container);
43 1
        return isset($config[$requestedName]);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
50
    {
51 1
        return $this->createLogger($serviceLocator, $requestedName);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
58
    {
59
        return $this->createLogger($container, $requestedName);
60
    }
61
62 1
    private function createLogger($container, $requestedName)
63
    {
64 1
        $config = $this->getConfig($container);
65
66 1
        $factory = new MonologServiceFactory();
67 1
        return $factory->createLogger($container, new MonologOptions($config[$requestedName]));
68
    }
69
70
    /**
71
     * @param ServiceLocatorInterface|ContainerInterface $container
72
     * @return array
73
     */
74 5
    public function getConfig($container)
75
    {
76 5
        if (null !== $this->config) {
77 2
            return $this->config;
78
        }
79
80 4
        $config = $container->get('config');
81
82 4
        if (isset($config['EnliteMonolog'])) {
83 3
            $this->config = $config['EnliteMonolog'];
84
        } else {
85 1
            $this->config = array();
86
        }
87
88 4
        return $this->config;
89
    }
90
91
    /**
92
     * @param array $config
93
     */
94 1
    public function setConfig($config)
95
    {
96 1
        $this->config = $config;
97 1
    }
98
}
99