History::getHistoryFileContent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Finder\Logic\Output\Html;
3
4
use Finder\Logic\AnalysisResult;
5
use SplFileObject;
6
7
/**
8
 * Stores information about multiple builds.
9
 */
10
class History
11
{
12
    /**
13
     * Cached history data (not persisted yet).
14
     *
15
     * @var array history data.
16
     */
17
    protected $cachedData;
18
19
    /**
20
     * Directory where history information will be stored.
21
     *
22
     * @var string directory path.
23
     */
24
    protected $outputDirectory;
25
26
    /**
27
     * Set dependencies.
28
     *
29
     * @param string $outputDirectory target directory path.
30
     */
31
    public function __construct($outputDirectory)
32
    {
33
        $this->outputDirectory = $outputDirectory;
34
    }
35
36
    /**
37
     * Finderend analysis result data to the history.
38
     *
39
     * @param  AnalysisResult $result analysis result.
40
     * @return void
41
     */
42
    public function append(AnalysisResult $result)
43
    {
44
        $data = $this->getData();
45
46
        $data['executions'][] = date('M d H:i');
47
48
        $toolsIssues = [];
49
50
        foreach ($result->toArray() as $lines) {
51
            foreach ($lines as $issues) {
52
                foreach ($issues as $issue) {
53
                    if (!isset($toolsIssues[$issue['tool']])) {
54
                        $toolsIssues[$issue['tool']] = 0;
55
                    }
56
                    $toolsIssues[$issue['tool']]++;
57
                }
58
            }
59
        }
60
61
        foreach ($toolsIssues as $tool => $issues) {
62
            if (!isset($data['historyData'][$tool])) {
63
                $builds = count($data['executions']);
64
                $data['historyData'][$tool] = [
65
                    'name' => $tool,
66
                    'data' => $builds > 1 ? array_fill(0, $builds - 1, 0) : [],
67
                ];
68
            }
69
            $data['historyData'][$tool]['data'][] = $issues;
70
        }
71
72
        $this->setData($data);
73
    }
74
75
    /**
76
     * Stores history data.
77
     *
78
     * @return boolean true if successfully wrote to the JSON file.
79
     */
80
    public function save()
81
    {
82
        $file = new SplFileObject($this->getHistoryFilePath(), 'w');
83
        return $file->fwrite(json_encode($this->getData()));
84
    }
85
86
    /**
87
     * Loads history data from the JSON file (or the cache if already did).
88
     *
89
     * @return array
90
     */
91
    public function getData()
92
    {
93
        if (null === $this->cachedData) {
94
            $data = $this->getHistoryFileContent();
95
            if (null === $data) {
96
                $data = [
97
                    'executed_at' => time(),
98
                    'data' => [],
99
                ];
100
            }
101
            $this->cachedData = $data;
102
        }
103
        return $this->cachedData;
104
    }
105
106
    /**
107
     * Overwrite current data.
108
     *
109
     * @param array $data history data.
110
     */
111
    protected function setData(array $data)
112
    {
113
        $this->cachedData = $data;
114
    }
115
116
    /**
117
     * History data
118
     *
119
     * @return array
120
     */
121
    protected function getHistoryFileContent()
122
    {
123
        if (!file_exists($this->getHistoryFilePath())) {
124
            return null;
125
        }
126
        $content = file_get_contents($this->getHistoryFilePath());
127
        return json_decode($content, true);
128
    }
129
130
    /**
131
     * JSON data file path.
132
     *
133
     * @return string file path.
134
     */
135
    protected function getHistoryFilePath()
136
    {
137
        return $this->outputDirectory . '/history.json';
138
    }
139
}
140