Conditions | 6 |
Paths | 14 |
Total Lines | 85 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
48 | public static function factory( |
||
49 | ConfigurationInterface $configuration, |
||
50 | LoggerConfigurationInterface $loggerConfiguration |
||
51 | ) { |
||
52 | |||
53 | // initialize the processors |
||
54 | $processors = array(); |
||
55 | /** @var \TechDivision\Import\Configuration\Logger\ProcessorConfigurationInterface $processorConfiguration */ |
||
56 | foreach ($loggerConfiguration->getProcessors() as $processorConfiguration) { |
||
57 | $reflectionClass = new \ReflectionClass($processorConfiguration->getType()); |
||
58 | $processors[] = $reflectionClass->newInstanceArgs(LoggerFactory::prepareConstructorArgs($reflectionClass, $processorConfiguration->getParams())); |
||
59 | } |
||
60 | |||
61 | // initialize the handlers |
||
62 | $handlers = array(); |
||
63 | /** @var \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration */ |
||
64 | foreach ($loggerConfiguration->getHandlers() as $handlerConfiguration) { |
||
65 | // query whether or not, we've a swift mailer configuration |
||
66 | if ($swiftMailerConfiguration = $handlerConfiguration->getSwiftMailer()) { |
||
67 | // load the factory that creates the swift mailer instance |
||
68 | $factory = $swiftMailerConfiguration->getFactory(); |
||
69 | // create the swift mailer instance |
||
70 | $swiftMailer = $factory::factory($swiftMailerConfiguration); |
||
71 | |||
72 | // load the generic logger configuration |
||
73 | $bubble = $handlerConfiguration->getParam(LoggerKeys::BUBBLE); |
||
74 | $logLevel = $handlerConfiguration->getParam(LoggerKeys::LOG_LEVEL); |
||
75 | |||
76 | // load sender/receiver configuration |
||
77 | $to = $swiftMailerConfiguration->getParam(SwiftMailerKeys::TO); |
||
78 | $from = $swiftMailerConfiguration->getParam(SwiftMailerKeys::FROM); |
||
79 | $subject = $swiftMailerConfiguration->getParam(SwiftMailerKeys::SUBJECT); |
||
80 | $contentType = $swiftMailerConfiguration->getParam(SwiftMailerKeys::CONTENT_TYPE); |
||
81 | |||
82 | // initialize the message template |
||
83 | $message = $swiftMailer->createMessage() |
||
84 | ->setSubject(sprintf('[%s] %s', $configuration->getSystemName(), $subject)) |
||
85 | ->setFrom($from) |
||
86 | ->setTo($to) |
||
87 | ->setBody('', $contentType); |
||
88 | |||
89 | // initialize the handler node |
||
90 | $reflectionClass = new \ReflectionClass($handlerConfiguration->getType()); |
||
91 | $handler = $reflectionClass->newInstanceArgs(array($swiftMailer, $message, $logLevel, $bubble)); |
||
92 | |||
93 | } else { |
||
94 | // initialize the handler node |
||
95 | $reflectionClass = new \ReflectionClass($handlerConfiguration->getType()); |
||
96 | |||
97 | // load the params |
||
98 | $params = $handlerConfiguration->getParams(); |
||
99 | |||
100 | // set the default log level, if not already set explicitly |
||
101 | if (!isset($params['level'])) { |
||
102 | $params['level'] = $configuration->getLogLevel(); |
||
103 | } |
||
104 | |||
105 | // create the handler instance |
||
106 | $handler = $reflectionClass->newInstanceArgs(LoggerFactory::prepareConstructorArgs($reflectionClass, $params)); |
||
107 | } |
||
108 | |||
109 | // if we've a formatter, initialize the formatter also |
||
110 | if ($formatterConfiguration = $handlerConfiguration->getFormatter()) { |
||
111 | $reflectionClass = new \ReflectionClass($formatterConfiguration->getType()); |
||
112 | $handler->setFormatter($reflectionClass->newInstanceArgs(LoggerFactory::prepareConstructorArgs($reflectionClass, $formatterConfiguration->getParams()))); |
||
113 | } |
||
114 | |||
115 | // add the handler |
||
116 | $handlers[] = $handler; |
||
117 | } |
||
118 | |||
119 | // prepare the logger params |
||
120 | $loggerParams = array( |
||
121 | 'name' => $loggerConfiguration->getChannelName(), |
||
122 | 'handlers' => $handlers, |
||
123 | 'processors' => $processors |
||
124 | ); |
||
125 | |||
126 | // append the params from the logger configuration |
||
127 | $loggerParams = array_merge($loggerParams, $loggerConfiguration->getParams()); |
||
128 | |||
129 | // initialize the logger instance itself |
||
130 | $reflectionClass = new \ReflectionClass($loggerConfiguration->getType()); |
||
131 | return $reflectionClass->newInstanceArgs(LoggerFactory::prepareConstructorArgs($reflectionClass, $loggerParams)); |
||
132 | } |
||
133 | |||
174 |