Completed
Pull Request — develop (#346)
by ANTHONIUS
06:57
created

LoggerAbstractFactory::createServiceWithName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * (this file is taken from ZF 2.2)
6
 *
7
 * @filesource
8
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
9
 * @license   MIT
10
 */
11
12
/** LoggerAbstractFactory.php */
13
namespace Core\Log;
14
15
use Interop\Container\ContainerInterface;
16
use Zend\ServiceManager\AbstractFactoryInterface;
17
use Zend\ServiceManager\ServiceLocatorInterface;
18
use Zend\Log\Logger;
19
20
/**
21
 * Logger abstract service factory.
22
 *
23
 * Allow to configure multiple loggers for application.
24
 */
25
class LoggerAbstractFactory implements AbstractFactoryInterface
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $config;
31
32
    /**
33
     * Configuration key holding logger configuration
34
     *
35
     * @var string
36
     */
37
    protected $configKey = 'log';
38
39
    /**
40
     * Create a new Logger instance
41
     *
42
     * @param ContainerInterface        $container
43
     * @param string                    $requestedName
44
     * @param array|null                $options
45
     * @return Logger
46
     */
47
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
48
    {
49
        $config  = $this->getConfig($container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Interop\Container\ContainerInterface> is not a sub-type of object<Zend\ServiceManag...erviceLocatorInterface>. It seems like you assume a child interface of the interface Interop\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
50
        $config  = $config[$requestedName];
51
        if (is_string($config) || isset($config['service'])) {
52
            $serviceName = is_string($config) ? $config : $config['service'];
53
            return $container->get($serviceName);
54
        }
55
        $this->processConfig($config, $container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Interop\Container\ContainerInterface> is not a sub-type of object<Zend\ServiceManag...erviceLocatorInterface>. It seems like you assume a child interface of the interface Interop\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
56
        return new Logger($config);
57
    }
58
59
    /**
60
     * Check if the factory can create an instance for the given $requestedName service
61
     *
62
     * @param ContainerInterface        $container
63
     * @param string                    $requestedName
64
     * @param array|null                $options
65
     * @return bool
66
     */
67
    public function canCreate(ContainerInterface $container, $requestedName, array $options = null)
0 ignored issues
show
Coding Style introduced by
function canCreate() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
        $config = $this->getConfig($container);
0 ignored issues
show
Compatibility introduced by
$container of type object<Interop\Container\ContainerInterface> is not a sub-type of object<Zend\ServiceManag...erviceLocatorInterface>. It seems like you assume a child interface of the interface Interop\Container\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
        if (empty($config)) {
71
            return false;
72
        }
73
74
        return isset($config[$requestedName]);
75
    }
76
77
    /**
78
     * Determines if we can create a Logger instance with give $requestedName
79
     *
80
     * @param  ServiceLocatorInterface $services
81
     * @param  string                  $name
82
     * @param  string                  $requestedName
83
     * @return bool
84
     */
85
    public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
86
    {
87
        return $this->canCreate($services,$requestedName);
88
    }
89
90
    /**
91
     * Create a Logger instance with given $requestedName service
92
     *
93
     * @param  ServiceLocatorInterface $services
94
     * @param  string                  $name
95
     * @param  string                  $requestedName
96
     * @return Logger
97
     */
98
    public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
99
    {
100
        return $this($services,$requestedName);
101
    }
102
103
    /**
104
     * Retrieve configuration for loggers, if any
105
     *
106
     * @param  ServiceLocatorInterface $services
107
     * @return array
108
     */
109
    protected function getConfig(ServiceLocatorInterface $services)
110
    {
111
        if ($this->config !== null) {
112
            return $this->config;
113
        }
114
115
        if (!$services->has('Config')) {
116
            $this->config = array();
117
            return $this->config;
118
        }
119
120
        $config = $services->get('Config');
121
        if (!isset($config[$this->configKey])) {
122
            $this->config = array();
123
            return $this->config;
124
        }
125
126
        $this->config = $config[$this->configKey];
127
        return $this->config;
128
    }
129
130
    protected function processConfig(&$config, ServiceLocatorInterface $services)
131
    {
132
        if (!isset($config['writer_plugin_manager'])) {
133
            $config['writer_plugin_manager'] = $services->get('LogWriterManager');
134
        }
135
        if (!isset($config['processor_plugin_manager'])) {
136
            $config['processor_plugin_manager'] = $services->get('LogProcessorManager');
137
        }
138
139
        if (!isset($config['writers'])) {
140
            return;
141
        }
142
143
        foreach ($config['writers'] as $index => $writerConfig) {
144
            if (!isset($writerConfig['options']['db'])
145
            || !is_string($writerConfig['options']['db'])
146
            ) {
147
                continue;
148
            }
149
            if (!$services->has($writerConfig['options']['db'])) {
150
                continue;
151
            }
152
153
            // Retrieve the DB service from the service locator, and
154
            // inject it into the configuration.
155
            $db = $services->get($writerConfig['options']['db']);
156
            $config['writers'][$index]['options']['db'] = $db;
157
        }
158
    }
159
}
160