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

JSONLogFetcher::createLogEnding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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