Completed
Pull Request — master (#94)
by Alessandro
04:59
created

LogParser::onProcessTerminated()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Parser\JSON;
5
6
use Paraunit\Lifecycle\ProcessEvent;
7
use Paraunit\Process\AbstractParaunitProcess;
8
use Paraunit\TestResult\Interfaces\TestResultBearerInterface;
9
use Paraunit\TestResult\Interfaces\TestResultHandlerInterface;
10
use Paraunit\TestResult\Interfaces\TestResultInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
/**
14
 * Class LogParser
15
 * @package Paraunit\Parser\JSON
16
 */
17
class LogParser
18
{
19
    /** @var LogFetcher */
20
    private $logLocator;
21
22
    /** @var TestResultHandlerInterface */
23
    private $noTestExecutedResultContainer;
24
25
    /** @var EventDispatcherInterface */
26
    private $eventDispatcher;
27
28
    /** @var  ParserChainElementInterface[] */
29
    private $parsers;
30
31
    /**
32
     * LogParser constructor.
33
     * @param LogFetcher $logLocator
34
     * @param TestResultHandlerInterface $noTestExecutedResultContainer
35
     * @param EventDispatcherInterface $eventDispatcher
36
     */
37 31
    public function __construct(
38
        LogFetcher $logLocator,
39
        TestResultHandlerInterface $noTestExecutedResultContainer,
40
        EventDispatcherInterface $eventDispatcher
41
    ) {
42 31
        $this->logLocator = $logLocator;
43 31
        $this->noTestExecutedResultContainer = $noTestExecutedResultContainer;
44 31
        $this->eventDispatcher = $eventDispatcher;
45 31
        $this->parsers = [];
46
    }
47
48
    /**
49
     * @param ParserChainElementInterface $container
50
     */
51 31
    public function addParser(ParserChainElementInterface $container)
52
    {
53 31
        $this->parsers[] = $container;
54
    }
55
56
    /**
57
     * @return TestResultBearerInterface[]
58
     */
59
    public function getParsers(): array
60
    {
61
        return $this->parsers;
62
    }
63
64
    /**
65
     * @param ProcessEvent $processEvent
66
     */
67 31
    public function onProcessTerminated(ProcessEvent $processEvent)
68
    {
69 31
        $process = $processEvent->getProcess();
70 31
        $logs = $this->logLocator->fetch($process);
71
72 31
        if ($this->noTestsExecuted($process, $logs)) {
73 3
            $this->noTestExecutedResultContainer->addProcessToFilenames($process);
74
75 3
            return;
76
        }
77
78 28
        foreach ($logs as $singleLog) {
79 28
            $this->processLog($process, $singleLog);
80
        }
81
82 28
        $this->eventDispatcher->dispatch(ProcessEvent::PROCESS_PARSING_COMPLETED, new ProcessEvent($process));
83
    }
84
85
    /**
86
     * @param AbstractParaunitProcess $process
87
     * @param \stdClass $logItem
88
     */
89 28
    private function processLog(AbstractParaunitProcess $process, \stdClass $logItem)
90
    {
91
        /** @var ParserChainElementInterface $resultContainer */
92 28
        foreach ($this->parsers as $resultContainer) {
93 28
            if ($resultContainer->handleLogItem($process, $logItem) instanceof TestResultInterface) {
94 28
                return;
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param AbstractParaunitProcess $process
101
     * @param array $logs
102
     * @return bool
103
     */
104 31
    private function noTestsExecuted(AbstractParaunitProcess $process, array $logs): bool
105
    {
106 31
        return $process->getExitCode() === 0 && count($logs) === 1;
107
    }
108
}
109