History::getData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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