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

LogParser::onProcessTerminated()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paraunit\Parser\JSON;
6
7
use Paraunit\Lifecycle\ProcessEvent;
8
use Paraunit\Process\AbstractParaunitProcess;
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 RetryParser */
30
    private $retryParser;
31
32
    /** @var ParserChainElementInterface[] */
33
    private $parsers;
34
35
    /**
36
     * LogParser constructor.
37
     * @param LogFetcher $logLocator
38
     * @param TestResultHandlerInterface $noTestExecutedResultContainer
39
     * @param EventDispatcherInterface $eventDispatcher
40
     * @param RetryParser $retryParser
41
     */
42 39
    public function __construct(
43
        LogFetcher $logLocator,
44
        TestResultHandlerInterface $noTestExecutedResultContainer,
45
        EventDispatcherInterface $eventDispatcher,
46
        RetryParser $retryParser
47
    ) {
48 39
        $this->logLocator = $logLocator;
49 39
        $this->noTestExecutedResultContainer = $noTestExecutedResultContainer;
50 39
        $this->eventDispatcher = $eventDispatcher;
51 39
        $this->retryParser = $retryParser;
52 39
        $this->parsers = [];
53
    }
54
55 64
    public static function getSubscribedEvents(): array
56
    {
57
        return [
58 64
            ProcessEvent::PROCESS_TERMINATED => 'onProcessTerminated',
59
        ];
60
    }
61
62
    /**
63
     * @param ParserChainElementInterface $container
64
     */
65 39
    public function addParser(ParserChainElementInterface $container)
66
    {
67 39
        $this->parsers[] = $container;
68
    }
69
70
    /**
71
     * @return ParserChainElementInterface[]
72
     */
73
    public function getParsers(): array
74
    {
75
        return $this->parsers;
76
    }
77
78
    /**
79
     * @param ProcessEvent $processEvent
80
     */
81 37
    public function onProcessTerminated(ProcessEvent $processEvent)
82
    {
83 37
        $process = $processEvent->getProcess();
84 37
        $logs = $this->logLocator->fetch($process);
85
86 37
        if ($this->noTestsExecuted($process, $logs)) {
87 4
            $this->noTestExecutedResultContainer->addProcessToFilenames($process);
88
89 4
            return;
90
        }
91
92 33
        if ($this->retryParser->processWillBeRetried($process, $logs)) {
93 7
            $this->eventDispatcher->dispatch(ProcessEvent::PROCESS_TO_BE_RETRIED, new ProcessEvent($process));
94
95 7
            return;
96
        }
97
98 32
        foreach ($logs as $singleLog) {
99 32
            $this->processLog($process, $singleLog);
100
        }
101
102 32
        $this->eventDispatcher->dispatch(ProcessEvent::PROCESS_PARSING_COMPLETED, new ProcessEvent($process));
103
    }
104
105
    /**
106
     * @param AbstractParaunitProcess $process
107
     * @param \stdClass $logItem
108
     */
109 32
    private function processLog(AbstractParaunitProcess $process, \stdClass $logItem)
110
    {
111
        /** @var ParserChainElementInterface $resultContainer */
112 32
        foreach ($this->parsers as $resultContainer) {
113 32
            if ($resultContainer->handleLogItem($process, $logItem) instanceof TestResultInterface) {
114 32
                return;
115
            }
116
        }
117
    }
118
119
    /**
120
     * @param AbstractParaunitProcess $process
121
     * @param array $logs
122
     * @return bool
123
     */
124 37
    private function noTestsExecuted(AbstractParaunitProcess $process, array $logs): bool
125
    {
126 37
        return $process->getExitCode() === 0 && count($logs) === 1;
127
    }
128
}
129