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

Xhgui_Saver_File::getFilename()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.0195
c 0
b 0
f 0
cc 8
nc 13
nop 1
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 bool
15
     */
16
    private $separateMeta;
17
18
    /**
19
     * Xhgui_Saver_File constructor.
20
     * @param $file
21
     * @param bool $separateMeta
22
     */
23
    public function __construct($file, $separateMeta = false)
24
    {
25
        $this->file             = $file;
26
        $this->separateMeta     = $separateMeta;
27
    }
28
29
    /**
30
     * @param array $data
31
     * @return bool|int
32
     */
33
    public function save(array $data)
34
    {
35
        if ($this->separateMeta) {
36
            $profiles           = Xhgui_Util::getDataForStorage($data['profile'], true);
37
38
            $meta               = $data['meta'];
39
40
            // store summary in separate meta file to speed up aggregation
41
            $meta['summary']    = $data['profile']['main()'];
42
            $meta               = Xhgui_Util::getDataForStorage($meta, false);
43
44
            file_put_contents($this->file.'.meta',$meta.PHP_EOL, FILE_APPEND);
45
46
            return file_put_contents($this->file,$profiles.PHP_EOL, FILE_APPEND);
47
        }
48
49
        $json = Xhgui_Util::getDataForStorage($data);
50
        return file_put_contents($this->file, $json.PHP_EOL, FILE_APPEND);
51
    }
52
53
    /**
54
     * Get filename to use to store data
55
     * @param string $dir
56
     * @return string
57
     */
58
    public static function getFilename($dir = '.') {
59
60
        $fileNamePattern = '';
61
62
        if (empty($_SERVER['REQUEST_URI'])) {
63
            if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
64
                try {
65
                    $fileNamePattern = dirname($dir) .
66
                        '/cache/xhgui.data.' .
67
                        microtime(true) .
68
                        bin2hex(random_bytes(5));
69
                } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
                }
71
            }
72
73
            if (empty($fileNamePattern) &&
74
                function_exists('openssl_random_pseudo_bytes') &&
75
                $b = openssl_random_pseudo_bytes(5, $strong)
76
            ) {
77
                $fileNamePattern = dirname($dir) .
78
                    '/cache/xhgui.data.' .
79
                    microtime(true).
80
                    bin2hex($b);
81
            }
82
83
            if (empty($fileNamePattern)) {
84
                $fileNamePattern = dirname($dir) .
85
                    '/cache/xhgui.data.' .
86
                    microtime(true).
87
                    getmypid().
88
                    uniqid('last_resort_unique_string', true);
89
            }
90
        } else {
91
            $fileNamePattern = dirname($dir) .
92
                '/cache/xhgui.data.' .
93
                microtime(true).
94
                substr(md5($_SERVER['REQUEST_URI']), 0, 10);
95
        }
96
97
        return $fileNamePattern;
98
    }
99
}
100