1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Carnage\Cqrs\MessageBus\MessageBusInterface; |
6
|
|
|
use Carnage\Cqrs\Service\EventCatcher; |
7
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\CompletePurchase; |
8
|
|
|
use ConferenceTools\Tickets\Domain\Command\Ticket\ReserveTickets; |
9
|
|
|
use ConferenceTools\Tickets\Domain\Event\Ticket\TicketPurchaseCreated; |
10
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
11
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\Delegate; |
12
|
|
|
use ConferenceTools\Tickets\Domain\ValueObject\TicketReservationRequest; |
13
|
|
|
use ConferenceTools\Tickets\Report\ReportInterface; |
14
|
|
|
use ConferenceTools\Tickets\Report\ReportManager; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
class ReportToCsv extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ReportManager |
25
|
|
|
*/ |
26
|
|
|
private $reportManager; |
27
|
|
|
|
28
|
|
|
public static function build(ReportManager $reportManager) |
29
|
|
|
{ |
30
|
|
|
$instance = new self(); |
31
|
|
|
$instance->reportManager = $reportManager; |
32
|
|
|
return $instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function configure() |
36
|
|
|
{ |
37
|
|
|
$this->setName('tickets:report-to-csv') |
38
|
|
|
->setDescription('Creates a csv export of a report') |
39
|
|
|
->setDefinition([ |
40
|
|
|
new InputArgument('report', InputArgument::REQUIRED, 'Report to run'), |
41
|
|
|
new InputArgument('outputFile', InputArgument::OPTIONAL, 'File to output report to', '/tmp/report.csv') |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$reportName = $input->getArgument('report'); |
48
|
|
|
if (!$this->reportManager->has($reportName)) { |
49
|
|
|
throw new \Exception('Invalid report name'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$outputFile = $input->getArgument('outputFile'); |
53
|
|
|
|
54
|
|
|
if (file_exists($outputFile)) { |
55
|
|
|
if (!is_writable($outputFile)) { |
56
|
|
|
throw new \Exception(sprintf('Cannot write to output file: %s', $outputFile)); |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
if (!is_writable(dirname($outputFile))) { |
60
|
|
|
throw new \Exception(sprintf('Cannot write to output directory: %s', dirname($outputFile))); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @var ReportInterface $report */ |
65
|
|
|
$report = $this->reportManager->get($reportName); |
66
|
|
|
|
67
|
|
|
$reportData = $report->produceReport(); |
68
|
|
|
|
69
|
|
|
$h = fopen($outputFile, 'w+'); |
70
|
|
|
$header = array_keys(current($reportData) ?: []); |
71
|
|
|
fputcsv($h, $header); |
72
|
|
|
|
73
|
|
|
foreach ($reportData as $datum) { |
74
|
|
|
fputcsv($h, $datum); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
fclose($h); |
78
|
|
|
} |
79
|
|
|
} |