1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\SmokerBundle\Message; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
|
7
|
|
|
class MessageCollector implements MessageCollectorInterface |
8
|
|
|
{ |
9
|
|
|
private $errors = []; |
10
|
|
|
private $success = []; |
11
|
|
|
private $warnings = []; |
12
|
|
|
|
13
|
|
|
const LINE_SEPARATOR = '###END###'.PHP_EOL; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Filesystem |
17
|
|
|
*/ |
18
|
|
|
private $fileSystem; |
19
|
|
|
|
20
|
|
|
private $cacheFile; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* MessageCollector constructor. |
24
|
|
|
*/ |
25
|
3 |
|
public function __construct(string $cacheDirectory) |
26
|
|
|
{ |
27
|
3 |
|
$this->cacheFile = $cacheDirectory.'/smoker/smoker.messages'; |
28
|
3 |
|
$this->fileSystem = new Filesystem(); |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
public function initialize(): void |
32
|
|
|
{ |
33
|
|
|
$this->fileSystem->dumpFile($this->cacheFile, ''); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function read(): array |
37
|
|
|
{ |
38
|
|
|
$messages = [ |
39
|
|
|
'success' => [], |
40
|
|
|
'errors' => [], |
41
|
|
|
'warnings' => [], |
42
|
|
|
]; |
43
|
|
|
$content = file_get_contents($this->cacheFile); |
44
|
|
|
$data = explode(self::LINE_SEPARATOR, $content); |
45
|
|
|
|
46
|
|
|
foreach ($data as $messageData) { |
47
|
|
|
$messageData = explode('=', $messageData); |
48
|
|
|
|
49
|
|
|
if ('ERROR' === $messageData[0]) { |
50
|
|
|
$messages['errors'][] = unserialize($messageData[1]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ('WARNING' === $messageData[0]) { |
54
|
|
|
$messages['warnings'][] = unserialize($messageData[1]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ('SUCCESS' === $messageData[0]) { |
58
|
|
|
$messages['success'][] = unserialize($messageData[1]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $messages; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function flush(): void |
69
|
|
|
{ |
70
|
|
|
foreach ($this->errors as $error) { |
71
|
|
|
$this->fileSystem->appendToFile($this->cacheFile, 'ERROR='.serialize($error).self::LINE_SEPARATOR); |
72
|
|
|
} |
73
|
|
|
$this->errors = []; |
74
|
|
|
|
75
|
|
|
foreach ($this->warnings as $warning) { |
76
|
|
|
$this->fileSystem->appendToFile($this->cacheFile, 'WARNING='.serialize($warning).self::LINE_SEPARATOR); |
77
|
|
|
} |
78
|
|
|
$this->warnings = []; |
79
|
|
|
|
80
|
|
|
foreach ($this->success as $success) { |
81
|
|
|
$this->fileSystem->appendToFile($this->cacheFile, 'SUCCESS='.serialize($success).self::LINE_SEPARATOR); |
82
|
|
|
} |
83
|
|
|
$this->success = []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function addError(string $url, string $message, int $code = 500, \Exception $exception = null): void |
90
|
|
|
{ |
91
|
|
|
$error = [ |
92
|
1 |
|
'url' => $url, |
93
|
1 |
|
'message' => $message, |
94
|
1 |
|
'code' => $code, |
95
|
|
|
]; |
96
|
|
|
|
97
|
1 |
|
if (null !== $exception) { |
98
|
1 |
|
$error['stacktrace'] = $exception->getTraceAsString(); |
99
|
|
|
} |
100
|
1 |
|
$this->errors[] = $error; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function addSuccess(string $url, string $message, int $code = 200): void |
107
|
|
|
{ |
108
|
1 |
|
$this->success[] = [ |
109
|
1 |
|
'url' => $url, |
110
|
1 |
|
'message' => $message, |
111
|
1 |
|
'code' => $code, |
112
|
|
|
]; |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function addWarning(string $url, string $message, int $code = 200): void |
119
|
|
|
{ |
120
|
1 |
|
$this->warnings[] = [ |
121
|
1 |
|
'url' => $url, |
122
|
1 |
|
'message' => $message, |
123
|
1 |
|
'code' => $code, |
124
|
|
|
]; |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
3 |
|
public function getErrors(): array |
131
|
|
|
{ |
132
|
3 |
|
return $this->errors; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
3 |
|
public function getSuccess(): array |
139
|
|
|
{ |
140
|
3 |
|
return $this->success; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
3 |
|
public function getWarnings(): array |
147
|
|
|
{ |
148
|
3 |
|
return $this->warnings; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|