Completed
Pull Request — master (#94)
by Alessandro
08:53
created

LogParser::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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