Completed
Pull Request — master (#94)
by Alessandro
08:53
created

ProcessPrinter::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1.037
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 22
     * @param SingleResultFormatter $singleResultFormatter
29
     * @param OutputInterface $output
30 22
     */
31 22
    public function __construct(SingleResultFormatter $singleResultFormatter, OutputInterface $output)
32
    {
33
        $this->singleResultFormatter = $singleResultFormatter;
34
        $this->output = $output;
35
    }
36 22
37
    public static function getSubscribedEvents(): array
38 22
    {
39
        return [
40 22
            ProcessEvent::PROCESS_PARSING_COMPLETED => 'onProcessParsingCompleted',
41
        ];
42
    }
43
44 22
    /**
45
     * @param ProcessEvent $processEvent
46 22
     * @throws \BadMethodCallException
47
     */
48
    public function onProcessParsingCompleted(ProcessEvent $processEvent)
49
    {
50 22
        $process = $processEvent->getProcess();
51 21
52
        foreach ($process->getTestResults() as $testResult) {
53 22
            $this->printFormattedWithCounter($testResult);
54
        }
55
    }
56
57
    /**
58 21
     * @param PrintableTestResultInterface $testResult
59
     */
60 21
    private function printFormattedWithCounter(PrintableTestResultInterface $testResult)
61 4
    {
62
        if ($this->counter % 80 === 0 && $this->counter > 1) {
63
            $this->output->writeln('');
64 21
        }
65
66 21
        ++$this->counter;
67 21
68
        $this->output->write(
69 21
            $this->singleResultFormatter->formatSingleResult($testResult)
70
        );
71
    }
72
}
73