1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Reporter; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Exception\ConnectException; |
8
|
|
|
use GuzzleHttp\Exception\ServerException; |
9
|
|
|
use GuzzleHttp\RequestOptions; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Symfony\Component\Console\Helper\Table; |
12
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Send the collected data to the Inventorio Cloud. |
17
|
|
|
*/ |
18
|
|
|
class InventorioGradeReporter implements Reporter |
19
|
|
|
{ |
20
|
|
|
private const ENDPOINT_COLLECT = '/grade/'; |
21
|
|
|
|
22
|
|
|
private OutputInterface $output; |
23
|
|
|
private string $userId; |
24
|
|
|
private string $serverId; |
25
|
|
|
private string $inventorioServer; |
26
|
|
|
|
27
|
|
|
const SEVERITIES = [ |
28
|
|
|
0 => '<fg=white;bg=blue> low </>', |
29
|
|
|
500 => '<fg=white;bg=yellow> medium </>', |
30
|
|
|
1000 => '<fg=white;bg=red> high </>', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
private int $startTime; |
34
|
|
|
|
35
|
|
|
public function __construct(OutputInterface $output, string $inventorioServer, string $serverId, string $userId) |
36
|
|
|
{ |
37
|
|
|
$this->output = $output; |
38
|
|
|
$this->userId = $userId; |
39
|
|
|
$this->serverId = $serverId; |
40
|
|
|
$this->inventorioServer = $inventorioServer; |
41
|
|
|
$this->startTime = time(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public function report(array $collectionData): void |
48
|
|
|
{ |
49
|
|
|
$endpoint = $this->getPreparedEndpoint(); |
50
|
|
|
|
51
|
|
|
$client = new Client(); |
52
|
|
|
|
53
|
|
|
$payload = [ |
54
|
|
|
'userId' => $this->userId, |
55
|
|
|
'data' => $collectionData |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$response = $client->post($endpoint, [ |
60
|
|
|
RequestOptions::JSON => $payload, |
61
|
|
|
RequestOptions::TIMEOUT => 5, |
62
|
|
|
RequestOptions::CONNECT_TIMEOUT => 2 |
63
|
|
|
]); |
64
|
|
|
} catch (ConnectException $e) { |
65
|
|
|
throw new RuntimeException('Unable to connect to ' . $endpoint . '. Message: ' . $e->getMessage()); |
66
|
|
|
} catch (ServerException $e) { |
67
|
|
|
var_dump($e->getResponse()->getBody()->getContents()); |
|
|
|
|
68
|
|
|
throw new RuntimeException('Unable to connect to ' . $endpoint . ' (ServerException). Message: ' . $e->getMessage()); |
69
|
|
|
} catch (Exception $e) { |
70
|
|
|
// var_dump($e->getMessage()); |
71
|
|
|
var_dump('ahh'); |
72
|
|
|
throw $e; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$result = json_decode((string)$response->getBody(), true); |
76
|
|
|
|
77
|
|
|
if (!is_array($result) || !array_key_exists('status', $result)) { |
78
|
|
|
throw new RuntimeException('Unknown error.'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($result['status'] !== 'SUCCESS') { |
82
|
|
|
throw new RuntimeException($result['message']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$table = new Table($this->output); |
86
|
|
|
$table->setHeaders(['Severity', 'Name', 'Description', 'Assets']); |
87
|
|
|
|
88
|
|
|
$hints = $result['data']['hints']; |
89
|
|
|
$lastIndex = count($hints) - 1; |
90
|
|
|
|
91
|
|
|
foreach ($hints as $i => $hint) { |
92
|
|
|
$row = [ |
93
|
|
|
'severity' => self::SEVERITIES[$hint['definition']['severity']], |
94
|
|
|
'name' => $hint['definition']['name'], |
95
|
|
|
'description' => wordwrap($hint['definition']['description'], 40) |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$assets = []; |
99
|
|
|
|
100
|
|
|
if (array_key_exists('files', $hint['issue']['parameters'])) { |
101
|
|
|
$assets = array_keys($hint['issue']['parameters']['files']); |
102
|
|
|
foreach ($assets as $key => $asset) { |
103
|
|
|
$assets[$key] = '- ' . $asset; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (array_key_exists('websites', $hint['issue']['parameters'])) { |
108
|
|
|
$assets = $hint['issue']['parameters']['websites']; |
109
|
|
|
foreach ($assets as $key => $asset) { |
110
|
|
|
$assets[$key] = '- https://' . $asset; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$row['assets'] = implode("\n", $assets); |
115
|
|
|
|
116
|
|
|
$table->addRow($row); |
117
|
|
|
|
118
|
|
|
if ($i < $lastIndex) { |
119
|
|
|
$table->addRow(new TableSeparator()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$table->render(); |
124
|
|
|
|
125
|
|
|
$duration = time() - $this->startTime; |
|
|
|
|
126
|
|
|
|
127
|
|
|
/*if ($duration > 60) { |
128
|
|
|
$minutes = floor($duration / 60); |
129
|
|
|
$seconds = $duration - $minutes * 60; |
130
|
|
|
$this->output->writeln('Grading took ' . $minutes . ' minutes and ' . $seconds . ' seconds'); |
131
|
|
|
} else { |
132
|
|
|
$this->output->writeln('Grading took ' . (time() - $this->startTime) . ' seconds'); |
133
|
|
|
}*/ |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return the final endpoint where the collected data should be sent to. |
138
|
|
|
*/ |
139
|
|
|
private function getPreparedEndpoint(): string |
140
|
|
|
{ |
141
|
|
|
return str_replace('{serverId}', $this->serverId, $this->inventorioServer . self::ENDPOINT_COLLECT); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|