Completed
Pull Request — master (#202)
by Erin
01:46
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
     * Return the wrapped reporters.
36
     *
37
     * @return array
38
     */
39
    public function getReporters()
40
    {
41
        return $this->reporters;
42
    }
43
44
    /**
45
     * Initialize reporter. Setup and listen for runner events.
46
     *
47
     * @return void
48
     */
49
    public function init()
50
    {
51
        $this->eventEmitter->on('runner.end', [$this, 'onRunnerEnd']);
52
    }
53
54
    /**
55
     * @param \Evenement\EventEmitterInterface $eventEmitter
56
     */
57
    public function setEventEmitter(EventEmitterInterface $eventEmitter)
58
    {
59
        parent::setEventEmitter($eventEmitter);
60
61
        array_map(function (ReporterInterface $reporter) use ($eventEmitter) {
62
            $reporter->setEventEmitter($eventEmitter);
63
        }, $this->reporters);
64
65
        return $this;
66
    }
67
68
    public function onRunnerEnd()
69
    {
70
        $stdout = $this->getOutput();
71
72
        array_map(function (ReporterInterface $reporter) use ($stdout) {
73
            $output = $reporter->getOutput();
74
75
            if ($output instanceof BufferedOutput && $content = $output->fetch()) {
76
                $stdout->writeln('');
77
                $stdout->write($content);
78
            }
79
        }, $this->reporters);
80
    }
81
82
    private $reporters;
83
}
84