Completed
Push — master ( 1a3a48...6e0e69 )
by Alessandro
09:39 queued 07:07
created

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