LogFetcher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 56
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 16 2
A cleanLog() 0 6 1
A createLogEnding() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paraunit\Parser\JSON;
6
7
use Paraunit\Configuration\TempFilenameFactory;
8
use Paraunit\Process\AbstractParaunitProcess;
9
10
/**
11
 * Class LogFetcher
12
 * @package Paraunit\Parser\JSON
13
 */
14
class LogFetcher
15
{
16
    const LOG_ENDING_STATUS = 'paraunitEnd';
17
18
    /** @var TempFilenameFactory */
19
    private $fileName;
20
21
    /**
22
     * LogFetcher constructor.
23
     * @param TempFilenameFactory $fileName
24
     */
25 42
    public function __construct(TempFilenameFactory $fileName)
26
    {
27 42
        $this->fileName = $fileName;
28
    }
29
30
    /**
31
     * @param AbstractParaunitProcess $process
32
     * @return \stdClass[]
33
     */
34 40
    public function fetch(AbstractParaunitProcess $process): array
35
    {
36 40
        $filePath = $this->fileName->getFilenameForLog($process->getUniqueId());
37 40
        $fileContent = '';
38
39 40
        if (file_exists($filePath)) {
40 38
            /** @var string $fileContent */
41 38
            $fileContent = file_get_contents($filePath);
42
            unlink($filePath);
43
        }
44 40
45 40
        $logs = json_decode(self::cleanLog($fileContent));
46
        $logs[] = $this->createLogEnding();
47 40
48
        return $logs;
49
    }
50
51
    /**
52
     * @param string $jsonString The dirty output
53
     * @return string            The normalized log, as an array of JSON objects
54 40
     */
55
    private static function cleanLog(string $jsonString): string
56 40
    {
57
        $splitted = preg_replace('/\}\{/', '},{', $jsonString);
58 40
59
        return '[' . $splitted . ']';
60
    }
61 40
62
    private function createLogEnding(): \stdClass
63 40
    {
64 40
        $logEnding = new \stdClass();
65
        $logEnding->status = self::LOG_ENDING_STATUS;
66 40
67
        return $logEnding;
68
    }
69
}
70