Completed
Pull Request — master (#94)
by Tim
05:44
created

LoggerFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 97
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3
ccs 0
cts 50
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B factory() 0 85 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\LoggerFactory
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 2016 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\Utils;
22
23
use TechDivision\Import\ConfigurationInterface;
24
use TechDivision\Import\Configuration\LoggerConfigurationInterface;
25
26
/**
27
 * Logger factory implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class LoggerFactory
36
{
37
38
    /**
39
     * Creates a new logger instance based on the passed logger configuration.
40
     *
41
     * @param \TechDivision\Import\ConfigurationInterface                     $configuration       The system configuration
42
     * @param \TechDivision\Import\Configuration\LoggerConfigurationInterface $loggerConfiguration The logger configuration
43
     *
44
     * @return \Psr\Log\LoggerInterface The logger instance
45
     */
46
    public static function factory(
47
        ConfigurationInterface $configuration,
48
        LoggerConfigurationInterface $loggerConfiguration
49
    ) {
50
51
        // initialize the processors
52
        $processors = array();
53
        /** @var \TechDivision\Import\Configuration\Logger\ProcessorConfigurationInterface $processorConfiguration */
54
        foreach ($loggerConfiguration->getProcessors() as $processorConfiguration) {
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...
55
            $reflectionClass = new \ReflectionClass($processorConfiguration->getType());
56
            $processors[] = $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $processorConfiguration->getParams()));
57
        }
58
59
        // initialize the handlers
60
        $handlers = array();
61
        /** @var \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration */
62
        foreach ($loggerConfiguration->getHandlers() as $handlerConfiguration) {
63
            // query whether or not, we've a swift mailer configuration
64
            if ($swiftMailerConfiguration = $handlerConfiguration->getSwiftMailer()) {
65
                // load the factory that creates the swift mailer instance
66
                $factory = $swiftMailerConfiguration->getFactory();
67
                // create the swift mailer instance
68
                $swiftMailer = $factory::factory($swiftMailerConfiguration);
69
70
                // load the generic logger configuration
71
                $bubble = $handlerConfiguration->getParam(LoggerKeys::BUBBLE);
72
                $logLevel = $handlerConfiguration->getParam(LoggerKeys::LOG_LEVEL);
73
74
                // load sender/receiver configuration
75
                $to = $swiftMailerConfiguration->getParam(SwiftMailerKeys::TO);
76
                $from = $swiftMailerConfiguration->getParam(SwiftMailerKeys::FROM);
77
                $subject = $swiftMailerConfiguration->getParam(SwiftMailerKeys::SUBJECT);
78
                $contentType = $swiftMailerConfiguration->getParam(SwiftMailerKeys::CONTENT_TYPE);
79
80
                // initialize the message template
81
                $message = $swiftMailer->createMessage()
82
                    ->setSubject(sprintf('[%s] %s', $configuration->getSystemName(), $subject))
83
                    ->setFrom($from)
84
                    ->setTo($to)
85
                    ->setBody('', $contentType);
86
87
                // initialize the handler node
88
                $reflectionClass = new \ReflectionClass($handlerConfiguration->getType());
89
                $handler = $reflectionClass->newInstanceArgs(array($swiftMailer, $message, $logLevel, $bubble));
90
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
91
            } else {
92
                // initialize the handler node
93
                $reflectionClass = new \ReflectionClass($handlerConfiguration->getType());
94
95
                // load the params
96
                $params = $handlerConfiguration->getParams();
97
98
                // set the default log level, if not already set explicitly
99
                if (!isset($params[ConfigurationKeys::LEVEL])) {
100
                    $params[ConfigurationKeys::LEVEL] = $configuration->getLogLevel();
101
                }
102
103
                // create the handler instance
104
                $handler = $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $params));
105
            }
106
107
            // if we've a formatter, initialize the formatter also
108
            if ($formatterConfiguration = $handlerConfiguration->getFormatter()) {
109
                $reflectionClass = new \ReflectionClass($formatterConfiguration->getType());
110
                $handler->setFormatter($reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $formatterConfiguration->getParams())));
111
            }
112
113
            // add the handler
114
            $handlers[] = $handler;
115
        }
116
117
        // prepare the logger params
118
        $loggerParams = array(
119
            ConfigurationKeys::NAME       => $loggerConfiguration->getChannelName(),
120
            ConfigurationKeys::HANDLERS   => $handlers,
121
            ConfigurationKeys::PROCESSORS => $processors
122
        );
123
124
        // append the params from the logger configuration
125
        $loggerParams = array_merge($loggerParams, $loggerConfiguration->getParams());
126
127
        // initialize the logger instance itself
128
        $reflectionClass = new \ReflectionClass($loggerConfiguration->getType());
129
        return $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $loggerParams));
130
    }
131
}
132