Completed
Push — master ( 400051...4d5855 )
by Alessandro
07:06
created

ProcessPrinter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 58
ccs 18
cts 20
cp 0.9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onProcessTerminated() 0 18 4
A printFormattedWithCounter() 0 12 3
1
<?php
2
3
namespace Paraunit\Printer;
4
5
use Paraunit\Lifecycle\ProcessEvent;
6
use Paraunit\TestResult\Interfaces\PrintableTestResultInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class ProcessPrinter
11
 * @package Paraunit\Printer
12
 */
13
class ProcessPrinter
14
{
15
    /** @var  SingleResultFormatter */
16
    private $singleResultFormatter;
17
18
    /** @var  OutputInterface */
19
    private $output;
20
21
    /** @var int */
22
    private $counter = 0;
23
24
    /**
25
     * ProcessPrinter constructor.
26
     * @param SingleResultFormatter $singleResultFormatter
27
     */
28 18
    public function __construct(SingleResultFormatter $singleResultFormatter)
29
    {
30 18
        $this->singleResultFormatter = $singleResultFormatter;
31 18
    }
32
33
    /**
34
     * @param ProcessEvent $processEvent
35
     */
36 18
    public function onProcessTerminated(ProcessEvent $processEvent)
37
    {
38 18
        $process = $processEvent->getProcess();
39
40 18
        if (! $processEvent->has('output_interface')) {
41
            throw new \BadMethodCallException('missing output_interface');
42
        }
43
44 18
        $this->output = $processEvent->get('output_interface');
45
46 18
        if (! $this->output instanceof OutputInterface) {
47
            throw new \BadMethodCallException('output_interface, unexpected type: ' . get_class($this->output));
48
        }
49
50 18
        foreach ($process->getTestResults() as $testResult) {
51 17
            $this->printFormattedWithCounter($testResult);
52
        }
53 18
    }
54
55
    /**
56
     * @param PrintableTestResultInterface $testResult
57
     */
58 17
    private function printFormattedWithCounter(PrintableTestResultInterface $testResult)
59
    {
60 17
        if ($this->counter % 80 == 0 && $this->counter > 1) {
61 4
            $this->output->writeln('');
62
        }
63
64 17
        ++$this->counter;
65
66 17
        $this->output->write(
67 17
            $this->singleResultFormatter->formatSingleResult($testResult)
68
        );
69 17
    }
70
}
71