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

HandlerWrapper::handleBatch()   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 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Loggers\Handlers\HandlerWrapper
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\Handlers;
22
23
use Monolog\Handler\HandlerInterface;
24
use Monolog\Formatter\FormatterInterface;
25
use TechDivision\Import\Loggers\HandlerFactoryInterface;
26
use TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface;
27
28
/**
29
 * A wrapper implementation for log handlers that adds a reset() method that allows to reset
30
 * log handlers e. g. in case the target directory changes during the import process.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2019 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import
36
 * @link      http://www.techdivision.com
37
 */
38
class HandlerWrapper implements ResetAwareHandlerInterface
39
{
40
41
    /**
42
     * The wrapped handler instance.
43
     *
44
     * @var \Monolog\Handler\HandlerInterface
45
     */
46
    protected $handler;
47
48
    /**
49
     * The handler factory instance.
50
     *
51
     * @var \TechDivision\Import\Loggers\HandlerFactoryInterface
52
     */
53
    protected $handlerFactory;
54
55
    /**
56
     * The handler configuration instance.
57
     *
58
     * @var \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface
59
     */
60
    protected $handlerConfiguration;
61
62
    /**
63
     * Initializes the wrapper with the handler instance.
64
     *
65
     * @param \TechDivision\Import\Loggers\HandlerFactoryInterface                    $handlerFactory       The handler factory instance
66
     * @param \TechDivision\Import\Configuration\Logger\HandlerConfigurationInterface $handlerConfiguration The handler configuration instance
67
     */
68
    public function __construct(HandlerFactoryInterface $handlerFactory, HandlerConfigurationInterface $handlerConfiguration)
69
    {
70
71
        // initialize the handler factory and the configuration
72
        $this->handlerFactory = $handlerFactory;
73
        $this->handlerConfiguration = $handlerConfiguration;
74
75
        // initialze the wrapper
76
        $this->reset();
77
    }
78
79
    /**
80
     * Reset's the handler instance.
81
     *
82
     * @return void
83
     */
84
    public function reset()
85
    {
86
87
        // initialize the variable for the old formatter instance
88
        $oldFormatter = null;
89
90
        // try to preserve the old formatter instance
91
        if ($this->handler instanceof HandlerInterface) {
92
            $oldFormatter = $this->handler->getFormatter();
93
        }
94
95
        // create a new handler instance
96
        $this->handler = $this->handlerFactory->factory($this->handlerConfiguration);
97
98
        // set the old formatter instance, if available
99
        if ($oldFormatter instanceof FormatterInterface) {
100
            $this->handler->setFormatter($oldFormatter);
101
        }
102
    }
103
104
    /**
105
     * Checks whether the given record will be handled by this handler.
106
     *
107
     * This is mostly done for performance reasons, to avoid calling processors for nothing.
108
     *
109
     * Handlers should still check the record levels within handle(), returning false in isHandling()
110
     * is no guarantee that handle() will not be called, and isHandling() might not be called
111
     * for a given record.
112
     *
113
     * @param array $record Partial log record containing only a level key
114
     *
115
     * @return boolean TRUE if the handler has to handle the record, FALSE otherwise
116
     */
117
    public function isHandling(array $record)
118
    {
119
        return $this->handler->isHandling($record);
120
    }
121
122
    /**
123
     * Handles a record.
124
     *
125
     * All records may be passed to this method, and the handler should discard
126
     * those that it does not want to handle.
127
     *
128
     * The return value of this function controls the bubbling process of the handler stack.
129
     * Unless the bubbling is interrupted (by returning true), the Logger class will keep on
130
     * calling further handlers in the stack with a given log record.
131
     *
132
     * @param  array $record The record to handle
133
     *
134
     * @return boolean TRUE means that this handler handled the record, and that bubbling is not permitted.
135
     *                 FALSE means the record was either not processed or that this handler allows bubbling.
136
     */
137
    public function handle(array $record)
138
    {
139
        return $this->handler->handle($record);
140
    }
141
142
    /**
143
     * Handles a set of records at once.
144
     *
145
     * @param array $records The records to handle (an array of record arrays)
146
     *
147
     * @return void
148
     */
149
    public function handleBatch(array $records)
150
    {
151
        $this->handler->handleBatch($records);
152
    }
153
154
    /**
155
     * Return's the handlers formatter instance.
156
     *
157
     * @return \Monolog\Formatter\FormatterInterface The formatter instance
158
     */
159
    public function getFormatter()
160
    {
161
        return $this->handler->getFormatter();
162
    }
163
164
    /**
165
     * Adds a processor in the stack.
166
     *
167
     * @param callable $callback The processor to add
168
     *
169
     * @return \TechDivision\Import\Loggers\Handlers\HandlerWrapper The handler instance
170
     */
171
    public function pushProcessor($callback)
172
    {
173
        $this->handler->pushProcessor($callback);
174
        return $this;
175
    }
176
177
    /**
178
     * Removes the processor on top of the stack and returns it.
179
     *
180
     * @return \TechDivision\Import\Loggers\Handlers\HandlerWrapper The handler instance
181
     */
182
    public function popProcessor()
183
    {
184
        $this->handler->popProcessor();
185
        return $this;
186
    }
187
188
    /**
189
     * Sets the handler's formatter instance.
190
     *
191
     * @param  \Monolog\Formatter\FormatterInterface $formatter The formatter instance
192
     *
193
     * @return \TechDivision\Import\Loggers\Handlers\HandlerWrapper The handler instance
194
     */
195
    public function setFormatter(FormatterInterface $formatter)
196
    {
197
        $this->handler->setFormatter($formatter);
198
        return $this;
199
    }
200
}
201