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

ProcessPrinter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 90
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A onProcessCompleted() 0 8 2
A printFormattedWithCounter() 0 13 2
A printCounter() 0 5 1
A getSubscribedEvents() 0 8 1
A onEngineEnd() 0 9 2
A isRowFull() 0 4 1
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