-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDownloadCommand.php
More file actions
108 lines (96 loc) · 3.25 KB
/
DownloadCommand.php
File metadata and controls
108 lines (96 loc) · 3.25 KB
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
<?php
// SPDX-FileCopyrightText: 2022 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Reporting\Clicommands;
use Icinga\Application\Hook\PdfexportHook;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Pdfexport\ProvidedHook\Pdfexport;
use Icinga\Module\Reporting\Cli\Command;
use Icinga\Module\Reporting\Database;
use Icinga\Module\Reporting\Model;
use Icinga\Module\Reporting\Report;
use Icinga\Util\Environment;
use InvalidArgumentException;
use ipl\Stdlib\Filter;
class DownloadCommand extends Command
{
/**
* Download report with specified ID as PDF, CSV or JSON
*
* USAGE
*
* icingacli reporting download <id> [--format=<pdf|csv|json>]
*
* OPTIONS
*
* --format=<pdf|csv|json>
* Download report as PDF, CSV or JSON. Defaults to pdf.
*
* --output=<file>
* Save report to the specified <file>.
*
* EXAMPLES
*
* Download report with ID 1:
* icingacli reporting download 1
*
* Download report with ID 1 as CSV:
* icingacli reporting download 1 --format=csv
*
* Download report with ID 1 as JSON to the specified file:
* icingacli reporting download 1 --format=json --output=sla.json
*/
public function defaultAction()
{
$id = $this->params->getStandalone();
if ($id === null) {
$this->fail($this->translate('Argument id is mandatory'));
}
/** @var Model\Report $report */
$report = Model\Report::on(Database::get())
->with('timeframe')
->filter(Filter::equal('id', $id))
->first();
if ($report === null) {
throw new NotFoundError('Report not found');
}
Environment::raiseExecutionTime();
Environment::raiseMemoryLimit();
$report = Report::fromModel($report);
/** @var string $format */
$format = $this->params->get('format', 'pdf');
$format = strtolower($format);
switch ($format) {
case 'pdf':
// TODO: Remove this once the dependency on the Pdfexport module is removed
$exporter = method_exists(PdfexportHook::class, 'first')
? PdfexportHook::first()
: Pdfexport::first();
$content = $exporter->htmlToPdf($report->toPdf());
break;
case 'csv':
$content = $report->toCsv();
break;
case 'json':
$content = $report->toJson();
break;
default:
throw new InvalidArgumentException(sprintf('Format %s is not supported', $format));
}
/** @var string $output */
$output = $this->params->get('output');
if ($output === null) {
$name = sprintf(
'%s (%s) %s',
$report->getName(),
$report->getTimeframe()->getName(),
date('Y-m-d H:i')
);
$output = "$name.$format";
} elseif (is_dir($output)) {
$this->fail($this->translate(sprintf('%s is a directory', $output)));
}
file_put_contents($output, $content);
echo "$output\n";
}
}