Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

MonologLoggerFactory::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Loggers\MonologLoggerFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Loggers;
22
23
use Monolog\Logger;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use TechDivision\Import\ConfigurationInterface;
26
use TechDivision\Import\Utils\ConfigurationKeys;
27
use TechDivision\Import\Utils\ConfigurationUtil;
28
use TechDivision\Import\Configuration\LoggerConfigurationInterface;
29
30
/**
31
 * Monolog Logger factory implementation.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2019 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import
37
 * @link      http://www.techdivision.com
38
 */
39
class MonologLoggerFactory
40
{
41
42
    /**
43
     * The DI container instance.
44
     *
45
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
46
     */
47
    protected $container;
48
49
    /**
50
     * The handler factory for the monolog logger factory.
51
     *
52
     * @var \TechDivision\Import\Loggers\MonologLoggerHandlerFactoryInterface
53
     */
54
    protected $monologLoggerHandlerFactory;
55
56
    /**
57
     * Initialize the factory with the DI container instance.
58
     *
59
     * @param \Symfony\Component\DependencyInjection\ContainerInterface         $container                   The DI container instance
60
     * @param \TechDivision\Import\Loggers\MonologLoggerHandlerFactoryInterface $monologLoggerHandlerFactory The Monolog Logger handler factory instance
61
     */
62
    public function __construct(
63
        ContainerInterface $container,
64
        MonologLoggerHandlerFactoryInterface $monologLoggerHandlerFactory
65
    ) {
66
        $this->container = $container;
67
        $this->monologLoggerHandlerFactory = $monologLoggerHandlerFactory;
68
    }
69
70
    /**
71
     * Return's the DI container instance.
72
     *
73
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance
74
     */
75
    protected function getContainer()
76
    {
77
        return $this->container;
78
    }
79
80
    /**
81
     * Return's the Monolog Logger handler factory.
82
     *
83
     * @return \TechDivision\Import\Loggers\MonologLoggerHandlerFactoryInterface The Monolog Logger handler factory instance
84
     */
85
    protected function getMonologLoggerHandlerFactory()
86
    {
87
        return $this->monologLoggerHandlerFactory;
88
    }
89
90
    /**
91
     * Creates a new logger instance based on the passed logger configuration.
92
     *
93
     * @param \TechDivision\Import\ConfigurationInterface                     $configuration       The system configuration
94
     * @param \TechDivision\Import\Configuration\LoggerConfigurationInterface $loggerConfiguration The logger configuration
95
     *
96
     * @return \Psr\Log\LoggerInterface The logger instance
97
     */
98
    public function factory(
99
        ConfigurationInterface $configuration,
0 ignored issues
show
Unused Code introduced by
The parameter $configuration 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...
100
        LoggerConfigurationInterface $loggerConfiguration
101
    ) {
102
103
        // load the available processors from the configuration
104
        $availableProcessors = $loggerConfiguration->getProcessors();
0 ignored issues
show
Bug introduced by
The method getProcessors() does not seem to exist on object<TechDivision\Impo...ConfigurationInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
106
        // initialize the processors
107
        $processors = array();
108
        /** @var \TechDivision\Import\Configuration\Logger\ProcessorConfigurationInterface $processorConfiguration */
109
        foreach ($availableProcessors as $processorConfiguration) {
110
            // create the processor (factory) instance
111
            $possibleProcessor = $this->getContainer()->get($processorConfiguration->getId());
112
            // query whether or not we've a factory or the instance
113
            if ($possibleProcessor instanceof ProcessorFactoryInterface) {
114
                $processors[] = $possibleProcessor->factory($processorConfiguration);
115
            } else {
116
                $processors[] = $possibleProcessor;
117
            }
118
        }
119
120
        // load the available handlers from the configuration
121
        $availableHandlers = $loggerConfiguration->getHandlers();
122
123
        // initialize the handlers
124
        $handlers = array();
125
        /** @var \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration */
126
        foreach ($availableHandlers as $handlerConfiguration) {
127
            $handlers[] = $this->getMonologLoggerHandlerFactory()->factory($handlerConfiguration);
128
        }
129
130
        // prepare the logger params
131
        $loggerParams = array(
132
            ConfigurationKeys::NAME       => $loggerConfiguration->getChannelName(),
133
            ConfigurationKeys::HANDLERS   => $handlers,
134
            ConfigurationKeys::PROCESSORS => $processors
135
        );
136
137
        // append the params from the logger configuration
138
        $loggerParams = array_merge($loggerParams, $loggerConfiguration->getParams());
139
140
        // initialize the Monolog Logger instance itself
141
        $reflectionClass = new \ReflectionClass(Logger::class);
142
        return $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $loggerParams));
143
    }
144
}
145