Completed
Push — output_parsers_refactor ( ea70d9...668933 )
by Alessandro
02:32
created

JSONLogFetcher::fetch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Paraunit\Parser;
4
5
use Paraunit\Configuration\JSONLogFilename;
6
use Paraunit\Exception\JSONLogNotFoundException;
7
use Paraunit\Process\ParaunitProcessInterface;
8
9
/**
10
 * Class JSONLogFetcher
11
 * @package Paraunit\Parser
12
 */
13
class JSONLogFetcher
14
{
15
    /** @var  JSONLogFilename */
16
    private $fileName;
17
18
    /**
19
     * JSONLogFetcher constructor.
20
     * @param JSONLogFilename $fileName
21
     */
22 20
    public function __construct(JSONLogFilename $fileName)
23
    {
24 20
        $this->fileName = $fileName;
25 20
    }
26
27
    /**
28
     * @param ParaunitProcessInterface $process
29
     * @return array
30
     * @throws JSONLogNotFoundException
31
     */
32 18
    public function fetch(ParaunitProcessInterface $process)
33
    {
34 18
        $filePath = $this->fileName->generate($process);
35
36 18
        if ( ! file_exists($filePath)) {
37 1
            throw new JSONLogNotFoundException($process);
38
        }
39
40 17
        return json_decode(
41 17
            $this->cleanLog(file_get_contents($filePath))
42 17
        );
43
    }
44
45
    /**
46
     * @param string $jsonString The dirty output
47
     * @return string            The normalized log, as an array of JSON objects
48
     */
49 17
    private static function cleanLog($jsonString)
50
    {
51 17
        $splitted = preg_replace('/\}\{/', '},{', $jsonString);
52
53 17
        return '[' . $splitted . ']';
54
    }
55
}
56