Completed
Pull Request — master (#94)
by Alessandro
04:33
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
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
 * Class LogParser
16
 * @package Paraunit\Parser\JSON
17
 */
18
class LogParser implements EventSubscriberInterface
19
{
20
    /** @var LogFetcher */
21
    private $logLocator;
22
23
    /** @var TestResultHandlerInterface */
24
    private $noTestExecutedResultContainer;
25
26
    /** @var EventDispatcherInterface */
27
    private $eventDispatcher;
28
29
    /** @var  ParserChainElementInterface[] */
30
    private $parsers;
31
32
    /**
33
     * LogParser constructor.
34
     * @param LogFetcher $logLocator
35
     * @param TestResultHandlerInterface $noTestExecutedResultContainer
36
     * @param EventDispatcherInterface $eventDispatcher
37
     */
38 32
    public function __construct(
39
        LogFetcher $logLocator,
40
        TestResultHandlerInterface $noTestExecutedResultContainer,
41
        EventDispatcherInterface $eventDispatcher
42
    ) {
43 32
        $this->logLocator = $logLocator;
44 32
        $this->noTestExecutedResultContainer = $noTestExecutedResultContainer;
45 32
        $this->eventDispatcher = $eventDispatcher;
46 32
        $this->parsers = [];
47
    }
48
49 56
    public static function getSubscribedEvents(): array
50
    {
51
        return [
52 56
            ProcessEvent::PROCESS_TERMINATED => 'onProcessTerminated',
53
        ];
54
    }
55
56
    /**
57
     * @param ParserChainElementInterface $container
58
     */
59 32
    public function addParser(ParserChainElementInterface $container)
60
    {
61 32
        $this->parsers[] = $container;
62
    }
63
64
    /**
65
     * @return TestResultBearerInterface[]
66
     */
67
    public function getParsers(): array
68
    {
69
        return $this->parsers;
70
    }
71
72
    /**
73
     * @param ProcessEvent $processEvent
74
     */
75 32
    public function onProcessTerminated(ProcessEvent $processEvent)
76
    {
77 32
        $process = $processEvent->getProcess();
78 32
        $logs = $this->logLocator->fetch($process);
79
80 32
        if ($this->noTestsExecuted($process, $logs)) {
81 3
            $this->noTestExecutedResultContainer->addProcessToFilenames($process);
82
83 3
            return;
84
        }
85
86 29
        foreach ($logs as $singleLog) {
87 29
            $this->processLog($process, $singleLog);
88
        }
89
90 29
        $this->eventDispatcher->dispatch(ProcessEvent::PROCESS_PARSING_COMPLETED, new ProcessEvent($process));
91
    }
92
93
    /**
94
     * @param AbstractParaunitProcess $process
95
     * @param \stdClass $logItem
96
     */
97 29
    private function processLog(AbstractParaunitProcess $process, \stdClass $logItem)
98
    {
99
        /** @var ParserChainElementInterface $resultContainer */
100 29
        foreach ($this->parsers as $resultContainer) {
101 29
            if ($resultContainer->handleLogItem($process, $logItem) instanceof TestResultInterface) {
102 29
                return;
103
            }
104
        }
105
    }
106
107
    /**
108
     * @param AbstractParaunitProcess $process
109
     * @param array $logs
110
     * @return bool
111
     */
112 32
    private function noTestsExecuted(AbstractParaunitProcess $process, array $logs): bool
113
    {
114 32
        return $process->getExitCode() === 0 && count($logs) === 1;
115
    }
116
}
117