Completed
Pull Request — master (#28)
by Elan
01:16
created

Xhgui_Saver_File::getFilename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * File saving handler
5
 */
6
class Xhgui_Saver_File implements Xhgui_Saver_Interface
7
{
8
    /**
9
     * @var string
10
     */
11
    private $file;
12
13
    /**
14
     * @var string
15
     */
16
    private $path;
17
18
    /**
19
     * @var bool
20
     */
21
    private $separateMeta;
22
23
    /**
24
     * Xhgui_Saver_File constructor.
25
     * @param string $path
26
     * @param string $file or null for default
27
     * @param bool $separateMeta
28
     */
29
    public function __construct($path, $file, $separateMeta = false)
30
    {
31
        $this->path = rtrim($path, '/\\').DIRECTORY_SEPARATOR;
32
        $this->file = !empty($file) ? $file : self::getFilename();
33
        $this->separateMeta = $separateMeta;
34
    }
35
36
    /**
37
     * @param array $data
38
     * @return bool|int
39
     */
40
    public function save(array $data)
41
    {
42
        if ($this->separateMeta) {
43
            $profiles = Xhgui_Util::getDataForStorage($data['profile'], true);
44
45
            $meta = $data['meta'];
46
47
            // store summary in separate meta file to speed up aggregation
48
            $meta['summary'] = $data['profile']['main()'];
49
            $meta = Xhgui_Util::getDataForStorage($meta, false);
50
51
            file_put_contents($this->path.$this->file.'.meta',$meta.PHP_EOL, FILE_APPEND);
52
53
            return file_put_contents($this->path.$this->file,$profiles.PHP_EOL, FILE_APPEND);
54
        }
55
56
        $json = Xhgui_Util::getDataForStorage($data);
57
        return file_put_contents($this->path.$this->file, $json.PHP_EOL, FILE_APPEND);
58
    }
59
60
    /**
61
     * Get filename to use to store data
62
     * @param string $dir
0 ignored issues
show
Bug introduced by
There is no parameter named $dir. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
63
     * @return string
64
     * @throws Exception
65
     */
66
    public static function getFilename() {
67
        $fileNamePattern = 'xhgui.data.'.microtime(true);
68
        try {
69
            $fileNamePattern .= bin2hex(random_bytes(12));
70
        } catch (Exception $e) {
71
            // Should we add logging here? random_bytes from paragonie/random_compat will throw exception if
72
            // it is unable to get safe enough data. It will not fall back to insecure random data.
73
        }
74
75
        return $fileNamePattern;
76
    }
77
}
78