|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the hogosha-monitor package |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Feel free to edit as you please, and have fun. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Hogosha\Monitor\Renderer; |
|
17
|
|
|
|
|
18
|
|
|
use Hogosha\Monitor\Guesser\StatusGuesser; |
|
19
|
|
|
use Hogosha\Monitor\Model\ResultCollection; |
|
20
|
|
|
use Webmozart\Console\Api\IO\IO; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class CsvRenderer implements RendererInterface |
|
26
|
|
|
{ |
|
27
|
|
|
protected $io; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param IO $io |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(IO $io) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->io = $io; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function render(ResultCollection $resultCollection) |
|
43
|
|
|
{ |
|
44
|
|
|
$stream = fopen('php://temp', 'w'); |
|
45
|
|
|
fputcsv( |
|
46
|
|
|
$stream, |
|
47
|
|
|
[ |
|
48
|
|
|
'Global Status', |
|
49
|
|
|
'Status Code', |
|
50
|
|
|
'Name', |
|
51
|
|
|
'Reponse Time', |
|
52
|
|
|
'Http Error Log', |
|
53
|
|
|
'Validator Error Log', |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$statusGuesser = new StatusGuesser(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($resultCollection as $result) { |
|
60
|
|
|
fputcsv( |
|
61
|
|
|
$stream, |
|
62
|
|
|
[ |
|
63
|
|
|
$statusGuesser->isFailed($result) ? 'FAIL' : 'OK', |
|
64
|
|
|
$result->getStatusCode(), |
|
65
|
|
|
$result->getUrl()->getName(), |
|
66
|
|
|
$result->getReponseTime(), |
|
67
|
|
|
$result->getHandlerError(), |
|
68
|
|
|
$result->getValidatorError(), |
|
69
|
|
|
] |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
rewind($stream); |
|
73
|
|
|
$this->io->write(stream_get_contents($stream)); |
|
74
|
|
|
fclose($stream); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|