Completed
Push — output_parsers_refactor ( d2cb12...a7c18a )
by Alessandro
03:46
created

ProcessPrinter::onProcessTerminated()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 18
ccs 9
cts 11
cp 0.8182
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4.0961
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Lifecycle\ProcessEvent;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Class ProcessPrinter
10
 * @package Paraunit\Printer
11
 */
12
class ProcessPrinter
13
{
14
    /** @var  SingleResultFormatter */
15
    private $singleResultFormatter;
16
17
    /** @var  OutputInterface */
18
    private $output;
19
    
20
    /** @var int */
21
    private $counter = 0;
22
23
    /**
24
     * ProcessPrinter constructor.
25
     * @param SingleResultFormatter $singleResultFormatter
26
     */
27 14
    public function __construct(SingleResultFormatter $singleResultFormatter)
28
    {
29 14
        $this->singleResultFormatter = $singleResultFormatter;
30 14
    }
31
32
    /**
33
     * @param ProcessEvent $processEvent
34
     */
35 14
    public function onProcessTerminated(ProcessEvent $processEvent)
36
    {
37 14
        $process = $processEvent->getProcess();
38
39 14
        if ( ! $processEvent->has('output_interface')) {
40
            throw new \BadMethodCallException('missing output_interface');
41
        }
42
43 14
        $this->output = $processEvent->get('output_interface');
44
45 14
        if ( ! $this->output instanceof OutputInterface) {
46
            throw new \BadMethodCallException('output_interface, unexpected type: ' . get_class($this->output));
47
        }
48
49 14
        foreach ($process->getTestResults() as $testResult) {
50 14
            $this->printFormattedWithCounter($testResult);
51 14
        }
52 14
    }
53
54
    /**
55
     * @param string $singleTestResult
56
     */
57 14
    private function printFormattedWithCounter($singleTestResult)
58
    {
59 14
        if ($this->counter % 80 == 0 && $this->counter > 1) {
60 4
            $this->output->writeln('');
61 4
        }
62
63 14
        ++$this->counter;
64
65 14
        $this->output->write(
66 14
            $this->singleResultFormatter->formatSingleResult($singleTestResult)
67 14
        );
68 14
    }
69
}
70