Completed
Push — develop ( 6a168f...92c47e )
by
unknown
12:16
created

LoggerAbstractFactory::processConfig()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 5.3846
cc 8
eloc 15
nc 20
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * (this file is taken from ZF 2.2)
6
 *
7
 * @filesource
8
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
9
 * @license   MIT
10
 */
11
12
/** LoggerAbstractFactory.php */
13
namespace Core\Log;
14
15
use Zend\ServiceManager\AbstractFactoryInterface;
16
use Zend\ServiceManager\ServiceLocatorInterface;
17
use Zend\Log\Logger;
18
19
/**
20
 * Logger abstract service factory.
21
 *
22
 * Allow to configure multiple loggers for application.
23
 */
24
class LoggerAbstractFactory implements AbstractFactoryInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $config;
30
31
    /**
32
     * Configuration key holding logger configuration
33
     *
34
     * @var string
35
     */
36
    protected $configKey = 'log';
37
38
    /**
39
     * @param  ServiceLocatorInterface $services
40
     * @param  string                  $name
41
     * @param  string                  $requestedName
42
     * @return bool
43
     */
44
    public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
45
    {
46
        $config = $this->getConfig($services);
47
        if (empty($config)) {
48
            return false;
49
        }
50
51
        return isset($config[$requestedName]);
52
    }
53
54
    /**
55
     * @param  ServiceLocatorInterface $services
56
     * @param  string                  $name
57
     * @param  string                  $requestedName
58
     * @return Logger
59
     */
60
    public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
61
    {
62
        $config  = $this->getConfig($services);
63
        $config  = $config[$requestedName];
64
        if (is_string($config) || isset($config['service'])) {
65
            $serviceName = is_string($config) ? $config : $config['service'];
66
            return $services->get($serviceName);
67
        }
68
        $this->processConfig($config, $services);
69
        return new Logger($config);
70
    }
71
72
    /**
73
     * Retrieve configuration for loggers, if any
74
     *
75
     * @param  ServiceLocatorInterface $services
76
     * @return array
77
     */
78
    protected function getConfig(ServiceLocatorInterface $services)
79
    {
80
        if ($this->config !== null) {
81
            return $this->config;
82
        }
83
84
        if (!$services->has('Config')) {
85
            $this->config = array();
86
            return $this->config;
87
        }
88
89
        $config = $services->get('Config');
90
        if (!isset($config[$this->configKey])) {
91
            $this->config = array();
92
            return $this->config;
93
        }
94
95
        $this->config = $config[$this->configKey];
96
        return $this->config;
97
    }
98
99
    protected function processConfig(&$config, ServiceLocatorInterface $services)
100
    {
101
        if (!isset($config['writer_plugin_manager'])) {
102
            $config['writer_plugin_manager'] = $services->get('LogWriterManager');
103
        }
104
        if (!isset($config['processor_plugin_manager'])) {
105
            $config['processor_plugin_manager'] = $services->get('LogProcessorManager');
106
        }
107
108
        if (!isset($config['writers'])) {
109
            return;
110
        }
111
112
        foreach ($config['writers'] as $index => $writerConfig) {
113
            if (!isset($writerConfig['options']['db'])
114
            || !is_string($writerConfig['options']['db'])
115
            ) {
116
                continue;
117
            }
118
            if (!$services->has($writerConfig['options']['db'])) {
119
                continue;
120
            }
121
122
            // Retrieve the DB service from the service locator, and
123
            // inject it into the configuration.
124
            $db = $services->get($writerConfig['options']['db']);
125
            $config['writers'][$index]['options']['db'] = $db;
126
        }
127
    }
128
}
129