Completed
Push — 16.x ( 3879ec...b566e8 )
by Tim
02:21
created

StreamHandlerFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 122
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
C factory() 0 66 10
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 = $handlerConfiguration->getParam(ConfigurationKeys::STREAM);
116
117
        // query whether or not the given log file is relative to the target
118
        // directory (for backwards compatility the default value must be YES)
119
        if ($handlerConfiguration->hasParam(ConfigurationKeys::RELATIVE) ? $handlerConfiguration->getParam(ConfigurationKeys::RELATIVE) : true) {
120
            $stream = implode(DIRECTORY_SEPARATOR, array($this->targetDirectory, $stream));
121
        }
122
123
        // override the filename in the params
124
        $params = array_replace($handlerConfiguration->getParams(), array(ConfigurationKeys::STREAM => $stream));
125
126
        // set the default log level, if not already set explicitly
127
        if (!isset($params[ConfigurationKeys::LEVEL])) {
128
            $params[ConfigurationKeys::LEVEL] = $this->defaultLogLevel;
129
        }
130
131
        // query wehther or not the log filename has been changed, if yes rename it
132
        if ($this->stream && is_file($this->stream) && $this->stream !== $stream) {
133
            rename($this->stream, $stream);
134
        }
135
136
        // set the new log filename
137
        $this->stream = $stream;
138
139
        // we've to query whether or not the status has been initialized
140
        if (isset($status[RegistryKeys::FILES])) {
141
            // if yes, merge the log filename into the status
142
            $status = array_merge_recursive(
143
                $status,
144
                array(
145
                    RegistryKeys::FILES => array(
146
                        $this->stream => array(
147
                            Uuid::uuid4()->toString() => array(
148
                                RegistryKeys::STATUS => 1,
149
                                RegistryKeys::PROCESSED_ROWS => 0
150
                            )
151
                        )
152
                    )
153
                )
154
            );
155
156
            // add the logger to the import artefacts
157
            $this->registryProcessor->mergeAttributesRecursive(CacheKeys::STATUS, $status);
158
        }
159
160
        // create and return the handler instance
161
        $reflectionClass = new \ReflectionClass(StreamHandler::class);
162
        return $reflectionClass->newInstanceArgs(ConfigurationUtil::prepareConstructorArgs($reflectionClass, $params));
163
    }
164
}
165