|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Hal\Application\Command\Job; |
|
11
|
|
|
use Hal\Application\Formater\FormaterInterface; |
|
12
|
|
|
use Hal\Component\Result\ResultCollection; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Job report |
|
19
|
|
|
* |
|
20
|
|
|
* @author Jean-François Lépine <https://twitter.com/Halleck45> |
|
21
|
|
|
*/ |
|
22
|
|
|
class ReportWriter implements JobInterface |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Destination |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $destination; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Formater |
|
34
|
|
|
* |
|
35
|
|
|
* @var FormaterInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $formater; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Output |
|
41
|
|
|
* |
|
42
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $output; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor |
|
48
|
|
|
* |
|
49
|
|
|
* @param string|null $destination |
|
50
|
|
|
* @param OutputInterface $output |
|
51
|
|
|
* @param FormaterInterface $formater |
|
52
|
|
|
*/ |
|
53
|
|
|
function __construct($destination, OutputInterface $output, FormaterInterface $formater) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$this->destination = $destination; |
|
56
|
|
|
$this->output = $output; |
|
57
|
|
|
$this->formater = $formater; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function execute(ResultCollection $collection, ResultCollection $aggregatedResults) { |
|
64
|
|
|
if(!$this->destination) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$dir = dirname($this->destination); |
|
69
|
|
|
if(!file_exists($dir)) { |
|
70
|
|
|
mkdir($dir, 0777, true); |
|
71
|
|
|
} |
|
72
|
|
|
$this->output->writeln(sprintf('Generating %s Report...', $this->formater->getName())); |
|
73
|
|
|
$handle = fopen($this->destination, 'w'); |
|
74
|
|
|
$stream = new StreamOutput($handle); |
|
75
|
|
|
$stream->write($this->formater->terminate($collection, $aggregatedResults)); |
|
76
|
|
|
fclose($handle); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.