Completed
Pull Request — master (#28)
by
unknown
01:21
created

Xhgui_Saver_File::getFilename()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.1954
c 0
b 0
f 0
cc 8
nc 13
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
     */
65
    public static function getFilename() {
66
67
        $fileNamePattern = '';
68
69
        $prefix = 'xhgui.data.'.microtime(true);
70
        
71
        if (empty($_SERVER['REQUEST_URI'])) {
72
            if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
73
                try {
74
                    $fileNamePattern = $prefix.bin2hex(random_bytes(5));
75
                } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
76
                }
77
            }
78
79
            if (empty($fileNamePattern) &&
80
                function_exists('openssl_random_pseudo_bytes') &&
81
                $b = openssl_random_pseudo_bytes(5, $strong)
82
            ) {
83
                $fileNamePattern = $prefix.bin2hex($b);
84
            }
85
86
            if (empty($fileNamePattern)) {
87
                $fileNamePattern = $prefix.getmypid().uniqid('last_resort_unique_string', true);
88
            }
89
        } else {
90
            $fileNamePattern = $prefix.substr(md5($_SERVER['REQUEST_URI']), 0, 10);
91
        }
92
93
        return $fileNamePattern;
94
    }
95
}
96