Completed
Pull Request — master (#94)
by Alessandro
04:33
created

ProcessPrinter::onProcessParsingCompleted()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
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
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
/**
12
 * Class ProcessPrinter
13
 * @package Paraunit\Printer
14
 */
15
class ProcessPrinter implements EventSubscriberInterface
16
{
17
    /** @var  SingleResultFormatter */
18
    private $singleResultFormatter;
19
20
    /** @var  OutputInterface */
21
    private $output;
22
23
    /** @var int */
24
    private $counter = 0;
25
26
    /**
27
     * ProcessPrinter constructor.
28
     * @param SingleResultFormatter $singleResultFormatter
29
     * @param OutputInterface $output
30
     */
31 34
    public function __construct(SingleResultFormatter $singleResultFormatter, OutputInterface $output)
32
    {
33 34
        $this->singleResultFormatter = $singleResultFormatter;
34 34
        $this->output = $output;
35
    }
36
37 56
    public static function getSubscribedEvents(): array
38
    {
39
        return [
40 56
            ProcessEvent::PROCESS_PARSING_COMPLETED => 'onProcessParsingCompleted',
41
        ];
42
    }
43
44
    /**
45
     * @param ProcessEvent $processEvent
46
     * @throws \BadMethodCallException
47
     */
48 34
    public function onProcessParsingCompleted(ProcessEvent $processEvent)
49
    {
50 34
        $process = $processEvent->getProcess();
51
52 34
        foreach ($process->getTestResults() as $testResult) {
53 34
            $this->printFormattedWithCounter($testResult);
54
        }
55
    }
56
57
    /**
58
     * @param PrintableTestResultInterface $testResult
59
     */
60 34
    private function printFormattedWithCounter(PrintableTestResultInterface $testResult)
61
    {
62 34
        if ($this->counter % 80 === 0 && $this->counter > 1) {
63 6
            $this->output->writeln('');
64
        }
65
66 34
        ++$this->counter;
67
68 34
        $this->output->write(
69 34
            $this->singleResultFormatter->formatSingleResult($testResult)
70
        );
71
    }
72
}
73