Completed
Push — master ( 3a3b0a...6e2562 )
by Alessandro
04:22 queued 01:03
created

ProcessPrinter::isRowFull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Printer;
5
6
use Paraunit\Lifecycle\EngineEvent;
7
use Paraunit\Lifecycle\ProcessEvent;
8
use Paraunit\TestResult\Interfaces\PrintableTestResultInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * Class ProcessPrinter
14
 * @package Paraunit\Printer
15
 */
16
class ProcessPrinter implements EventSubscriberInterface
17
{
18
    const MAX_CHAR_LENGTH = 80;
19
    const COUNTER_CHAR_LENGTH = 5;
20
21
    /** @var  SingleResultFormatter */
22
    private $singleResultFormatter;
23
24
    /** @var  OutputInterface */
25
    private $output;
26
27
    /** @var int */
28
    private $counter;
29
30
    /** @var int */
31
    private $singleRowCounter;
32
33
    /**
34
     * ProcessPrinter constructor.
35
     * @param SingleResultFormatter $singleResultFormatter
36
     * @param OutputInterface $output
37
     */
38 40
    public function __construct(SingleResultFormatter $singleResultFormatter, OutputInterface $output)
39
    {
40 40
        $this->singleResultFormatter = $singleResultFormatter;
41 40
        $this->output = $output;
42 40
        $this->counter = 0;
43 40
        $this->singleRowCounter = 0;
44
    }
45
46 58
    public static function getSubscribedEvents(): array
47
    {
48
        return [
49 58
            ProcessEvent::PROCESS_PARSING_COMPLETED => 'onProcessParsingCompleted',
50 58
            EngineEvent::END => ['onEngineEnd', 400],
51
        ];
52
    }
53
54
    /**
55
     * @param ProcessEvent $processEvent
56
     * @throws \BadMethodCallException
57
     */
58 36
    public function onProcessParsingCompleted(ProcessEvent $processEvent)
59
    {
60 36
        $process = $processEvent->getProcess();
61
62 36
        foreach ($process->getTestResults() as $testResult) {
63 36
            $this->printFormattedWithCounter($testResult);
64
        }
65
    }
66
67 19
    public function onEngineEnd()
68
    {
69 19
        while (! $this->isRowFull()) {
70 19
            $this->output->write(' ');
71 19
            ++$this->singleRowCounter;
72
        }
73
74 19
        $this->printCounter();
75
    }
76
77
    /**
78
     * @param PrintableTestResultInterface $testResult
79
     */
80 36
    private function printFormattedWithCounter(PrintableTestResultInterface $testResult)
81
    {
82 36
        if ($this->isRowFull()) {
83 7
            $this->printCounter();
84
        }
85
86 36
        ++$this->counter;
87 36
        ++$this->singleRowCounter;
88
89 36
        $this->output->write(
90 36
            $this->singleResultFormatter->formatSingleResult($testResult)
91
        );
92
    }
93
94 25
    private function printCounter()
95
    {
96 25
        $this->output->writeln(sprintf('%6d', $this->counter));
97 25
        $this->singleRowCounter = 0;
98
    }
99
100 40
    private function isRowFull(): bool
101
    {
102 40
        return $this->singleRowCounter === self::MAX_CHAR_LENGTH - (self::COUNTER_CHAR_LENGTH + 1);
103
    }
104
}
105