Completed
Push — output_parsers_refactor ( 755930...b5c5df )
by Alessandro
07:17
created

ProcessPrinter::onProcessTerminated()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0359

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 27
ccs 18
cts 20
cp 0.9
rs 8.439
cc 6
eloc 17
nc 6
nop 1
crap 6.0359
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Lifecycle\ProcessEvent;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Class ProcessPrinter
10
 * @package Paraunit\Printer
11
 */
12
class ProcessPrinter
13
{
14
    /** @var  OutputInterface */
15
    private $output;
16
    
17
    /** @var int */
18
    private $counter = 0;
19
20
    /**
21
     * @param ProcessEvent $processEvent
22
     */
23 24
    public function onProcessTerminated(ProcessEvent $processEvent)
24
    {
25 24
        $process = $processEvent->getProcess();
26
27 24
        if ( ! $processEvent->has('output_interface')) {
28
            throw new \BadMethodCallException('missing output_interface');
29
        }
30
31 24
        $this->output = $processEvent->get('output_interface');
32
33 24
        if ( ! $this->output instanceof OutputInterface) {
34
            throw new \BadMethodCallException('output_interface, unexpected type: ' . get_class($this->output));
35
        }
36
37 24
        switch (true) {
38 24
            case $process->isToBeRetried():
39 4
                $this->printWithCounter('<ok>A</ok>');
40 4
                break;
41 23
            case $process->hasAbnormalTermination():
42 2
                $this->printWithCounter('<halted>X</halted>');
43 2
                break;
44 21
            default:
45 2
                foreach ($process->getTestResults() as $testResult) {
46 2
                    $this->printSingleTestResult($testResult);
47 19
                }
48 1
        }
49 1
    }
50 18
51 18
    /**
52 18
     * @param string $testResult
53 18
     */
54 18
    private function printSingleTestResult($testResult)
55 24
    {
56
        switch ($testResult) {
57
            case 'E': 
58
                $this->printWithCounter('<error>E</error>');
59
                break;
60 18
            case 'F':
61
                $this->printWithCounter('<fail>F</fail>');
62
                break;
63 18
            case 'W':
64 4
                $this->printWithCounter('<warning>W</warning>');
65 4
                break;
66 14
            case 'I':
67 7
                $this->printWithCounter('<incomplete>I</incomplete>');
68 7
                break;
69 7
            case 'S':
70 2
                $this->printWithCounter('<skipped>S</skipped>');
71 2
                break;
72 5
           case 'R':
73 1
                $this->printWithCounter('<risky>R</risky>');
74 1
                break;
75 4
            case '.':
76 1
                $this->printWithCounter('<ok>.</ok>');
77 1
                break;
78 3
            default:
79 1
                $this->printWithCounter('<warning>?</warning>');
80 1
                break;
81 2
        }
82 1
    }
83 1
84 1
    /**
85 1
     * @param string $string
86 1
     */
87 1
    private function printWithCounter($string)
88 18
    {
89
        if ($this->counter % 80 == 0 && $this->counter > 1) {
90
            $this->output->writeln('');
91
        }
92
93 24
        ++$this->counter;
94
95 24
        $this->output->write($string);
96 4
    }
97
}
98