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

MonologServiceAbstractFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 87
ccs 22
cts 26
cp 0.8462
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateServiceWithName() 0 4 1
A canCreate() 0 4 1
A has() 0 5 1
A __invoke() 0 4 1
A getConfig() 0 16 3
A setConfig() 0 4 1
A createServiceWithName() 0 4 1
A createLogger() 0 7 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