Completed
Pull Request — master (#93)
by Alessandro
02:47
created

ProcessPrinter::printFormattedWithCounter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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