Completed
Push — multiple-reporters ( 4bf06a )
by Erin
01:52
created

CompositeReporter::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Peridot\Reporter;
3
4
use Evenement\EventEmitterInterface;
5
use Peridot\Configuration;
6
use Peridot\Core\HasEventEmitterTrait;
7
use Symfony\Component\Console\Output\BufferedOutput;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Combines multiple reporters.
12
 *
13
 * @package Peridot\Reporter
14
 */
15
class CompositeReporter extends AbstractBaseReporter
16
{
17
    /**
18
     * @param array $reporters
19
     * @param Configuration $configuration
20
     * @param OutputInterface $output
21
     * @param EventEmitterInterface $eventEmitter
22
     */
23
    public function __construct(
24
        array $reporters,
25
        Configuration $configuration,
26
        OutputInterface $output,
27
        EventEmitterInterface $eventEmitter
28
    ) {
29
        $this->reporters = $reporters;
30
31
        parent::__construct($configuration, $output, $eventEmitter);
32
    }
33
34
    /**
35
     * Initialize reporter. Setup and listen for runner events
36
     *
37
     * @return void
38
     */
39
    public function init()
40
    {
41
        $this->eventEmitter->on('runner.end', [$this, 'onRunnerEnd']);
42
    }
43
44
    /**
45
     * @param \Evenement\EventEmitterInterface $eventEmitter
46
     */
47
    public function setEventEmitter(EventEmitterInterface $eventEmitter)
48
    {
49
        parent::setEventEmitter($eventEmitter);
50
51
        array_map(function (ReporterInterface $reporter) use ($eventEmitter) {
52
            $reporter->setEventEmitter($eventEmitter);
53
        }, $this->reporters);
54
55
        return $this;
56
    }
57
58
    public function onRunnerEnd()
59
    {
60
        $stdout = $this->getOutput();
61
62
        array_map(function (ReporterInterface $reporter) use ($stdout) {
63
            $output = $reporter->getOutput();
64
65
            if ($output instanceof BufferedOutput && $content = $output->fetch()) {
66
                $stdout->writeln('');
67
                $stdout->write($content);
68
            }
69
        }, $this->reporters);
70
    }
71
72
    private $reporters;
73
}
74