-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReport.php
150 lines (125 loc) · 3.28 KB
/
Report.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Jh\Import\Report;
use Jh\Import\Report\Handler\Handler;
use Jh\Import\LogLevel;
/**
* @author Aydin Hassan <[email protected]>
*/
class Report
{
/**
* @var Handler[]
*/
private $handlers = [];
/**
* @var string
*/
private $importName;
/**
* @var string
*/
private $sourceId;
/**
* @var bool
*/
private $isSuccessful = true;
/**
* @var ReportItem[]
*/
private $items = [];
/**
* @var array
*/
public static $failedLogLevels = [
LogLevel::EMERGENCY => LogLevel::EMERGENCY,
LogLevel::ALERT => LogLevel::ALERT,
LogLevel::CRITICAL => LogLevel::CRITICAL,
LogLevel::ERROR => LogLevel::ERROR,
];
public function __construct(array $handlers, string $importName, string $sourceId)
{
$this->handlers = $handlers;
$this->importName = $importName;
$this->sourceId = $sourceId;
}
public function addHandler(Handler $handler)
{
$this->handlers[] = $handler;
}
public function start(\DateTime $startTime = null)
{
$startTime = $startTime ?: new \DateTime();
foreach ($this->handlers as $handler) {
$handler->start($this, $startTime);
}
}
public function newItem(string $referenceLine, string $idField, string $idValue): ReportItem
{
$this->items[] = $item = new ReportItem($this->handlers, $referenceLine, $idField, $idValue);
return $item;
}
/**
* @return string
*/
public function getImportName(): string
{
return $this->importName;
}
/**
* @return string
*/
public function getSourceId(): string
{
return $this->sourceId;
}
public function addInfo(string $info): void
{
$this->addMessage(LogLevel::INFO, $info);
}
public function addError(string $error): void
{
$this->addMessage(LogLevel::ERROR, $error);
}
public function addWarning(string $warning): void
{
$this->addMessage(LogLevel::WARNING, $warning);
}
public function addMessage(string $logLevel, $message): void
{
$message = new Message($logLevel, $message);
if (isset(static::$failedLogLevels[$message->getLogLevel()])) {
$this->isSuccessful = false;
}
foreach ($this->handlers as $handler) {
$handler->handleMessage($message);
}
}
public function finish(\DateTime $finishTime = null, int $memoryUsage = null): void
{
$finishTime = $finishTime ?: new \DateTime();
$memoryUsage = $memoryUsage ?: memory_get_usage(true);
foreach ($this->handlers as $handler) {
$handler->finish($this, $finishTime, $memoryUsage);
}
}
public function isSuccessful(): bool
{
if (!$this->isSuccessful) {
return false;
}
return array_reduce($this->items, function ($carry, ReportItem $reportItem) {
if (!$carry) {
return false;
}
return $reportItem->isSuccessful();
}, true);
}
public function getItems(): array
{
return $this->items;
}
public function getHandlers(): array
{
return $this->handlers;
}
}