Completed
Pull Request — master (#37)
by Alessandro
02:23
created

JSONLogParser::onProcessTerminated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Exception\JSONLogNotFoundException;
6
use Paraunit\Lifecycle\ProcessEvent;
7
use Paraunit\Process\AbstractParaunitProcess;
8
use Paraunit\TestResult\Interfaces\TestResultContainerInterface;
9
use Paraunit\TestResult\Interfaces\TestResultInterface;
10
11
/**
12
 * Class JSONLogParser
13
 * @package Paraunit\Parser
14
 */
15
class JSONLogParser
16
{
17
    /** @var  JSONLogFetcher */
18
    private $logLocator;
19
20
    /** @var  JSONParserChainElementInterface[] */
21
    private $parsers;
22
23
    /**
24
     * JSONLogParser constructor.
25
     * @param JSONLogFetcher $logLocator
26
     */
27 23
    public function __construct(JSONLogFetcher $logLocator)
28
    {
29 23
        $this->logLocator = $logLocator;
30 23
        $this->parsers = array();
31 23
    }
32
33
    /**
34
     * @param JSONParserChainElementInterface $container
35
     */
36 23
    public function addParser(JSONParserChainElementInterface $container)
37
    {
38 23
        $this->parsers[] = $container;
39 23
    }
40
41
    /**
42
     * @return TestResultContainerInterface[]
43
     */
44 9
    public function getParsers()
45
    {
46 9
        return $this->parsers;
47
    }
48
49
    /**
50
     * @return TestResultContainerInterface[]
51
     */
52 9
    public function getParsersForPrinting()
53
    {
54 9
        return array_reverse($this->parsers);
55
    }
56
57
    /**
58
     * @param ProcessEvent $processEvent
59
     */
60 22
    public function onProcessTerminated(ProcessEvent $processEvent)
61
    {
62 22
        $process = $processEvent->getProcess();
63 22
        $logs = $this->logLocator->fetch($process);
64
65 22
        foreach ($logs as $singleLog) {
66 22
            $this->processLog($process, $singleLog);
67 22
        }
68 22
    }
69
70
    /**
71
     * @param AbstractParaunitProcess $process
72
     * @param \stdClass $logItem
73
     */
74 22
    private function processLog(AbstractParaunitProcess $process, \stdClass $logItem)
75
    {
76
        /** @var JSONParserChainElementInterface $resultContainer */
77 22
        foreach ($this->parsers as $resultContainer) {
78 22
            if ($resultContainer->handleLogItem($process, $logItem) instanceof TestResultInterface) {
79 22
                return;
80
            }
81 21
        }
82
    }
83
}
84