Completed
Push — master ( 400051...4d5855 )
by Alessandro
07:06
created

ProcessPrinter::printFormattedWithCounter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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