-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathConsoleHandler.php
67 lines (54 loc) · 1.55 KB
/
ConsoleHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Jh\Import\Report\Handler;
use Jh\Import\LogLevel;
use Jh\Import\Progress\CliProgress;
use Jh\Import\Report\Message;
use Jh\Import\Report\Report;
use Jh\Import\Report\ReportItem;
/**
* @author Aydin Hassan <[email protected]>
*/
class ConsoleHandler implements Handler
{
/**
* @var array
*/
private $acceptedLogLevels;
/**
* @var CliProgress
*/
private $cliProgress;
public function __construct(CliProgress $cliProgress, $minErrorLevel)
{
if (!isset(LogLevel::$levels[$minErrorLevel])) {
throw new \InvalidArgumentException('Non existent log level');
}
$this->acceptedLogLevels = array_filter(LogLevel::$levels, function ($level) use ($minErrorLevel) {
return $level >= LogLevel::$levels[$minErrorLevel];
});
$this->cliProgress = $cliProgress;
}
public function start(Report $report, \DateTime $startTime): void
{
// noop
}
public function finish(Report $report, \DateTime $finishTime, int $memoryUsage): void
{
// noop
}
public function handleMessage(Message $message): void
{
$this->write($message);
}
public function handleItemMessage(ReportItem $item, Message $message): void
{
$this->write($message);
}
private function write(Message $message): void
{
if (!isset($this->acceptedLogLevels[$message->getLogLevel()])) {
return;
}
$this->cliProgress->addLog($message->getLogLevel(), $message->getMessage());
}
}