Completed
Pull Request — master (#114)
by Alessandro
08:10
created

LogParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 118
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getSubscribedEvents() 0 6 1
A addParser() 0 4 1
A getParsers() 0 4 1
A onProcessTerminated() 0 15 2
A processLogs() 0 10 3
A processLog() 0 9 3
A noTestsExecuted() 0 4 2
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 37
     * @param TestResultHandlerInterface $noTestExecutedResultContainer
39
     * @param EventDispatcherInterface $eventDispatcher
40
     * @param RetryParser $retryParser
41
     */
42
    public function __construct(
43 37
        LogFetcher $logLocator,
44 37
        TestResultHandlerInterface $noTestExecutedResultContainer,
45 37
        EventDispatcherInterface $eventDispatcher,
46 37
        RetryParser $retryParser
47
    ) {
48
        $this->logLocator = $logLocator;
49 63
        $this->noTestExecutedResultContainer = $noTestExecutedResultContainer;
50
        $this->eventDispatcher = $eventDispatcher;
51
        $this->retryParser = $retryParser;
52 63
        $this->parsers = [];
53
    }
54
55
    public static function getSubscribedEvents(): array
56
    {
57
        return [
58
            ProcessEvent::PROCESS_TERMINATED => 'onProcessTerminated',
59 37
        ];
60
    }
61 37
62
    /**
63
     * @param ParserChainElementInterface $container
64
     */
65
    public function addParser(ParserChainElementInterface $container)
66
    {
67
        $this->parsers[] = $container;
68
    }
69
70
    /**
71
     * @return ParserChainElementInterface[]
72
     */
73
    public function getParsers(): array
74
    {
75 35
        return $this->parsers;
76
    }
77 35
78 35
    /**
79
     * @param ProcessEvent $processEvent
80 35
     */
81 4
    public function onProcessTerminated(ProcessEvent $processEvent)
82
    {
83 4
        $process = $processEvent->getProcess();
84
        $logs = $this->logLocator->fetch($process);
85
86 31
        if ($this->noTestsExecuted($process, $logs)) {
87 31
            $this->noTestExecutedResultContainer->addProcessToFilenames($process);
88
89
            return;
90 31
        }
91
92
        $this->processLogs($process, $logs);
93
94
        $this->eventDispatcher->dispatch(ProcessEvent::PROCESS_PARSING_COMPLETED, new ProcessEvent($process));
95
    }
96
97 31
    /**
98
     * @param AbstractParaunitProcess $process
99
     * @param \stdClass[] $logs
100 31
     */
101 31
    private function processLogs(AbstractParaunitProcess $process, array $logs)
102 31
    {
103
        if ($this->retryParser->processWillBeRetried($process, $logs)) {
104
            return;
105
        }
106
107
        foreach ($logs as $singleLog) {
108
            $this->processLog($process, $singleLog);
109
        }
110
    }
111
112 35
    /**
113
     * @param AbstractParaunitProcess $process
114 35
     * @param \stdClass $logItem
115
     */
116
    private function processLog(AbstractParaunitProcess $process, \stdClass $logItem)
117
    {
118
        /** @var ParserChainElementInterface $resultContainer */
119
        foreach ($this->parsers as $resultContainer) {
120
            if ($resultContainer->handleLogItem($process, $logItem) instanceof TestResultInterface) {
121
                return;
122
            }
123
        }
124
    }
125
126
    /**
127
     * @param AbstractParaunitProcess $process
128
     * @param array $logs
129
     * @return bool
130
     */
131
    private function noTestsExecuted(AbstractParaunitProcess $process, array $logs): bool
132
    {
133
        return $process->getExitCode() === 0 && count($logs) === 1;
134
    }
135
}
136