Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

StreamHandlerFactory::factory()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 37
cp 0
rs 7.6282
c 0
b 0
f 0
cc 8
nc 32
nop 1
crap 72

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/**
4
 * TechDivision\Import\Loggers\StreamHandlerFactory
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\Handler\StreamHandler;
24
use TechDivision\Import\Utils\CacheKeys;
25
use TechDivision\Import\Utils\RegistryKeys;
26
use TechDivision\Import\Utils\ConfigurationUtil;
27
use TechDivision\Import\Utils\ConfigurationKeys;
28
use TechDivision\Import\Services\RegistryProcessorInterface;
29
use TechDivision\Import\Configuration\ConfigurationInterface;
30
use TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface;
31
use Ramsey\Uuid\Uuid;
32
33
/**
34
 * Handler factory implementation for a stream handler that changes the log
35
 * filename depending on the target directory of the actual import.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2019 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import
41
 * @link      http://www.techdivision.com
42
 */
43
class StreamHandlerFactory implements HandlerFactoryInterface
44
{
45
46
    /**
47
     * The log level to use.
48
     *
49
     * @var string
50
     */
51
    protected $defaultLogLevel;
52
53
    /**
54
     * The target directory to place the log file within.
55
     *
56
     * @var string
57
     */
58
    protected $targetDirectory;
59
60
    /**
61
     * The actual log filename.
62
     *
63
     * @var string
64
     */
65
    protected $stream;
66
67
    /**
68
     * The registry processor instance.
69
     *
70
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
71
     */
72
    protected $registryProcessor;
73
74
    /**
75
     * Initialize the processor with the actual configuration and registry processor instance.
76
     *
77
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration     The actual configuration instance
78
     * @param \TechDivision\Import\Services\RegistryProcessorInterface  $registryProcessor The registry processor instance
79
     */
80
    public function __construct(ConfigurationInterface $configuration, RegistryProcessorInterface $registryProcessor)
81
    {
82
83
        // load the default values for the log level and the target directory from the configuration
84
        $this->defaultLogLevel = $configuration->getLogLevel();
85
        $this->targetDirectory = $configuration->getTargetDir();
86
87
        // set the registry processor instance
88
        $this->registryProcessor = $registryProcessor;
89
    }
90
91
    /**
92
     * Creates a new formatter instance based on the passed configuration.
93
     *
94
     * @param \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration The handler configuration
95
     *
96
     * @return \Monolog\Handler\HandlerInterface The handler instance
97
     */
98
    public function factory(HandlerConfigurationInterface $handlerConfiguration)
99
    {
100
101
        // initialize the status array
102
        $status = array();
103
104
        // try to load the actual import status from the registry processor
105
        if ($this->registryProcessor->hasAttribute(CacheKeys::STATUS)) {
106
            $status = $this->registryProcessor->getAttribute(CacheKeys::STATUS);
107
        }
108
109
        // query whether or not the target directory has been changed
110
        if (isset($status[RegistryKeys::TARGET_DIRECTORY])) {
111
            $this->targetDirectory = $status[RegistryKeys::TARGET_DIRECTORY];
112
        }
113
114
        // prepare the log filename
115
        $stream = implode(DIRECTORY_SEPARATOR, array($this->targetDirectory, $handlerConfiguration->getParam(ConfigurationKeys::STREAM)));
116
117
        // override the filename in the params
118
        $params = array_replace($handlerConfiguration->getParams(), array(ConfigurationKeys::STREAM => $stream));
119
120
        // set the default log level, if not already set explicitly
121
        if (!isset($params[ConfigurationKeys::LEVEL])) {
122
            $params[ConfigurationKeys::LEVEL] = $this->defaultLogLevel;
123
        }
124
125
        // query wehther or not the log filename has been changed, if yes rename it
126
        if ($this->stream && is_file($this->stream) && $this->stream !== $stream) {
127
            rename($this->stream, $stream);
128
        }
129
130
        // set the new log filename
131
        $this->stream = $stream;
132
133
        // we've to query whether or not the status has been initialized
134
        if (isset($status[RegistryKeys::FILES])) {
135
            // if yes, merge the log filename into the status
136
            $status = array_merge_recursive(
137
                $status,
138
                array(
139
                    RegistryKeys::FILES => array(
140
                        $this->stream => array(
141
                            Uuid::uuid4()->toString() => array(
142
                                RegistryKeys::STATUS => 1,
143
                                RegistryKeys::PROCESSED_ROWS => 0
144
                            )
145
                        )
146
                    )
147
                )
148
            );
149
150
            // add the logger to the import artefacts
151
            $this->registryProcessor->mergeAttributesRecursive(CacheKeys::STATUS, $status);
152
        }
153
154
        // create and return the handler instance
155
        $reflectionClass = new \ReflectionClass(StreamHandler::class);
156
        return $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $params));
157
    }
158
}
159