Completed
Pull Request — master (#268)
by
unknown
01:20
created

Xhgui_Storage_File::importFile()   C

Complexity

Conditions 12
Paths 20

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 6.9666
c 0
b 0
f 0
cc 12
nc 20
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class Xhgui_Storage_File implements Xhgui_StorageInterface, Xhgui_WatchedFunctionsStorageInterface
4
{
5
6
    /**
7
     * @var string
8
     */
9
    protected $path     = '../data/';
10
11
    /**
12
     * @var string
13
     */
14
    protected $prefix   = 'xhgui.data';
15
16
    /**
17
     * @var bool|mixed
18
     */
19
    protected $separateMeta = true;
20
21
    /**
22
     * @var mixed
23
     */
24
    protected $dataSerializer;
25
26
    /**
27
     * @var mixed
28
     */
29
    protected $metaSerializer;
30
31
    /**
32
     * @var string
33
     */
34
    protected $watchedFunctionsPathPrefix = '../watched_functions/';
35
36
    /**
37
     * @var int[]
38
     */
39
    protected $countCache;
40
41
    /**
42
     * @var Xhgui_Storage_Filter
43
     */
44
    private $filter;
45
46
    /**
47
     * Xhgui_Storage_File constructor.
48
     * @param $config
49
     */
50
    public function __construct($config)
51
    {
52
53
        // @todo config!
54
        $this->path         = '../data/';
55
56
        // @todo config!
57
        $this->prefix       = 'xhgui.data';
58
59
        $this->separateMeta     = $config['save.handler.separate_meta'];
60
        $this->dataSerializer   = $config['save.handler.serializer'];
61
        $this->metaSerializer   = $config['save.handler.meta_serializer'];
62
    }
63
64
    /**
65
     * @inheritDoc
66
     * @param Xhgui_Storage_Filter $filter
67
     * @param bool $projections
68
     * @return Xhgui_Storage_ResultSet
69
     */
70
    public function find(Xhgui_Storage_Filter $filter, $projections = false)
71
    {
72
        $result       = glob($this->path. $this->prefix . '*');
73
        sort($result);
74
75
        $ret = [];
76
        foreach ($result as $i => $file) {
77
            // skip meta files.
78
            if (strpos($file, '.meta') !== false) {
79
                continue;
80
            }
81
82
            // try to detect timestamp in filename.
83
            $requestTimeFromFilename = $this->getRequestTimeFromFilename($file);
84
            if (!empty($requestTimeFromFilename)) {
85
                if (null !== $filter->getStartDate() &&
86
                    $this->getDateTimeFromStringOrTimestamp($filter->getStartDate()) >= $requestTimeFromFilename) {
87
                    continue;
88
                }
89
90
                if (null !== $filter->getEndDate() &&
91
                    $this->getDateTimeFromStringOrTimestamp($filter->getEndDate()) <= $requestTimeFromFilename ) {
92
                    continue;
93
                }
94
            }
95
96
            $metaFile   = $this->getMetafileNameFromProfileName($file);
97
98
            $meta       = $this->importFile($metaFile, true);
99
            if ($meta === false) {
100
                continue;
101
            }
102
103
            $profile    = $this->importFile($file, false);
104
            if ($profile === false) {
105
                continue;
106
            }
107
108
            if (!empty($profile['meta'])) {
109
                $meta = array_merge($meta, $profile['meta']);
110
            }
111
112
            if (!empty($profile['profile'])) {
113
                $profile = $profile['profile'];
114
            }
115
116
            if (!empty($profile['_id'])) {
117
                $id = $profile['_id'];
118
            } else {
119
                $id = basename($file);
120
            }
121
            if (!empty($profile)) {
122
                $ret[$id] = [
123
                    'profile'   => $profile,
124
                    '_id'       => $id,
125
                    'meta'      => $meta,
126
                ];
127
            } else {
128
                $ret[$id] = $profile;
129
            }
130
        }
131
132
        try {
133
            if (!empty($filter->getSort()) && !empty($ret)) {
134
                $this->filter = $filter;
135
                usort($ret, array($this, 'sortByColumn'));
136
                unset($this->filter);
137
            }
138
        } catch (InvalidArgumentException $e) {
139
            // ignore for now.
140
        }
141
        
142
        $cacheId = md5(serialize($filter->toArray()));
143
144
        $this->countCache[$cacheId] = count($ret);
145
        $ret = array_slice($ret, $filter->getPerPage()*($filter->getPage()-1), $filter->getPerPage());
146
        $ret = array_column($ret, null, '_id');
147
148
        return new Xhgui_Storage_ResultSet($ret, $this->countCache[$cacheId]);
149
    }
150
151
    /**
152
     * @inheritDoc
153
     * @param Xhgui_Storage_Filter $filter
154
     * @return int
155
     */
156
    public function count(Xhgui_Storage_Filter $filter)
157
    {
158
        $cacheId = md5(serialize($filter->toArray()));
159
        if (empty($this->countCache[$cacheId])) {
160
            $this->find($filter);
161
        }
162
        return $this->countCache[$cacheId];
163
    }
164
165
    /**
166
     * @inheritDoc
167
     * @param $id
168
     * @return mixed
169
     */
170
    public function findOne($id)
171
    {
172
        $filter = new Xhgui_Storage_Filter();
173
        $filter->setId($id);
174
        $resultSet = $this->find($id);
175
        return $resultSet->current();
176
    }
177
178
    /**
179
     * @inheritDoc
180
     * @param $id
181
     * @return bool
182
     */
183
    public function remove($id)
184
    {
185
        if (file_exists($this->path.$id)) {
186
            $metaFileName = $this->getMetafileNameFromProfileName($id);
187
            if (file_exists($this->path.$metaFileName)) {
188
                unlink($this->path.$metaFileName);
189
            }
190
            unlink($this->path.$id);
191
            return true;
192
        }
193
        return false;
194
    }
195
196
    /**
197
     * @inheritDoc
198
     */
199
    public function drop()
200
    {
201
        array_map('unlink', glob($this->path.'*.xhprof'));
202
        array_map('unlink', glob($this->path.'*.meta'));
203
    }
204
205
    /**
206
     * @inheritDoc
207
     * @param $match
208
     * @param $col
209
     * @param int $percentile
210
     * @return array
211
     */
212
    public function aggregate(Xhgui_Storage_Filter $filter, $col, $percentile = 1)
213
    {
214
        $ret = $this->find($filter);
215
216
        $result = [
217
            'ok'        => 1,
218
            'result'    => [],
219
        ];
220
221
        foreach ($ret as $row) {
222
            $request_date = $row['meta']['request_date'];
223
            $result['result'][$request_date]['wall_times'][]    = $row['profile']['main()']['wt'];
224
            $result['result'][$request_date]['cpu_times'][]     = $row['profile']['main()']['cpu'];
225
            $result['result'][$request_date]['mu_times'][]      = $row['profile']['main()']['mu'];
226
            $result['result'][$request_date]['pmu_times'][]     = $row['profile']['main()']['pmu'];
227
228
            if (empty($result['result'][$request_date]['row_count'])) {
229
                $result['result'][$request_date]['row_count'] = 0;
230
            }
231
            $result['result'][$request_date]['row_count']++;
232
233
            $result['result'][$request_date]['raw_index'] =
234
                $result['result'][$request_date]['row_count']*($percentile/100);
235
236
            $result['result'][$request_date]['_id']= $request_date;
237
        }
238
239
        return $result;
240
    }
241
242
243
    /**
244
     * Column sorter
245
     *
246
     * @param $a
247
     * @param $b
248
     * @return int
249
     */
250
    public function sortByColumn($a, $b)
251
    {
252
        $sort = $this->filter->getSort();
253
        switch ($sort) {
254
            case 'ct':
255
            case 'wt':
256
            case 'cpu':
257
            case 'mu':
258 View Code Duplication
            case 'pmu':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
                $aValue = $a['profile']['main()'][$sort];
260
                $bValue = $b['profile']['main()'][$sort];
261
                break;
262
263 View Code Duplication
            case 'time':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
264
                $aValue = $a['meta']['request_ts']['sec'];
265
                $bValue = $b['meta']['request_ts']['sec'];
266
                break;
267
268
            case 'controller':
269
            case 'action':
270
            case 'application':
271
            case 'branch':
272 View Code Duplication
            case 'version':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
                $aValue = $a['meta'][$sort];
274
                $bValue = $b['meta'][$sort];
275
                break;
276
277
            default:
278
                throw new InvalidArgumentException('Invalid sort mode');
279
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
280
        }
281
282
        if ($aValue == $bValue) {
283
            return 0;
284
        }
285
286
        if (is_numeric($aValue) || is_numeric($bValue)) {
287
            if ($this->filter->getDirection() === 'desc') {
288
                if ($aValue < $bValue) {
289
                    return 1;
290
                }
291
                return -1;
292
            }
293
294
            if ($aValue > $bValue) {
295
                return 1;
296
            }
297
            return -1;
298
        }
299
300
        if ($this->filter->getDirection() === 'desc') {
301
            return strnatcmp($aValue, $bValue);
302
        }
303
        return strnatcmp($bValue, $aValue);
304
    }
305
306
    /**
307
     * Generate meta profile name from profile file name.
308
     *
309
     * In most cases just add .meta extension
310
     *
311
     * @param $file
312
     * @return mixed
313
     */
314
    protected function getMetafileNameFromProfileName($file)
315
    {
316
        $metaFile = $file.'.meta';
317
        return $metaFile;
318
    }
319
320
321
    /**
322
     * Try to parse string or unix timestamp ane return \DateTime
323
     *
324
     * @param $timestamp
325
     * @return bool|DateTime
326
     */
327
    protected function getDateTimeFromStringOrTimestamp($timestamp)
328
    {
329
330
        try {
331
            $date = new DateTime($timestamp);
332
            return $date;
333
        } catch (Exception $e) {
334
            // leave empty to try parse different format below
335
        }
336
337
        try {
338
            $date = DateTime::createFromFormat('U', $timestamp);
339
            return $date;
340
        } catch (Exception $e) {
341
            // leave empty to try parse different format below
342
        }
343
344
        try {
345
            $date = DateTime::createFromFormat('Y-m-d H:i:s', $timestamp);
346
            return $date;
347
        } catch (Exception $e) {
348
            // last attempt failed. Throw generic exception.
349
            throw new RuntimeException('Unable to parse date from string: '.$timestamp, null, $e);
350
        }
351
    }
352
353
    /**
354
     * @inheritDoc
355
     * @param $data
356
     */
357
    public function insert(array $data)
358
    {
359
360
        if (empty($data['_id'])) {
361
            $data['_id'] = md5($data['name']);
362
        }
363
364
        file_put_contents($this->path.''.$this->prefix.$data['_id'].'.json', json_encode($data));
365
    }
366
367
    /**
368
     * @inheritDoc
369
     * @param $id
370
     * @param $data
371
     */
372
    public function update($id, array $data)
373
    {
374
        file_put_contents($this->path.''.$this->prefix.$id.'.json', json_encode($data));
375
    }
376
377
    /**
378
     * Load profile file from disk, prepare it and return parsed array
379
     *
380
     * @param $path
381
     * @param bool $meta
382
     * @return mixed
383
     */
384
    protected function importFile($path, $meta = false)
385
    {
386
        if ($meta) {
387
            $serializer = $this->metaSerializer;
388
        } else {
389
            $serializer = $this->dataSerializer;
390
        }
391
392
        if (!file_exists($path) || !is_readable($path)) {
393
            return false;
394
        }
395
        
396
        switch ($serializer) {
397
            default:
398
            case 'json':
399
                return json_decode(file_get_contents($path), true);
400
401
            case 'serialize':
402
                if (PHP_MAJOR_VERSION > 7) {
403
                    return unserialize(file_get_contents($path), false);
404
                }
405
                /** @noinspection UnserializeExploitsInspection */
406
                return unserialize(file_get_contents($path));
407
408
            case 'igbinary_serialize':
409
            case 'igbinary_unserialize':
410
            case 'igbinary':
411
                /** @noinspection PhpComposerExtensionStubsInspection */
412
                return igbinary_unserialize(file_get_contents($path));
413
414
            // this is a path to a file on disk
415
            case 'php':
416
            case 'var_export':
417
                /** @noinspection PhpIncludeInspection */
418
                return include $path;
419
        }
420
    }
421
422
    /**
423
     * @inheritDoc
424
     * @return array
425
     */
426
    public function getWatchedFunctions()
427
    {
428
        $ret = [];
429
        $files = glob($this->watchedFunctionsPathPrefix.'*.json');
430
        foreach ($files as $file) {
431
            $ret[] = json_decode(file_get_contents($file));
432
        }
433
        return $ret;
434
    }
435
436
    /**
437
     * @inheritDoc
438
     * @param $name
439
     * @return bool
440
     */
441 View Code Duplication
    public function addWatchedFunction($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
442
    {
443
        $name = trim($name);
444
        if (empty($name)) {
445
            return false;
446
        }
447
        $id = md5($name);
448
        $i = file_put_contents(
449
            $this->watchedFunctionsPathPrefix.$id.'.json',
450
            json_encode(['id'=>$id, 'name'=>$name])
451
        );
452
        return $i > 0;
453
    }
454
455
    /**
456
     * @inheritDoc
457
     * @param $id
458
     * @param $name
459
     * @return bool
460
     */
461 View Code Duplication
    public function updateWatchedFunction($id, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
462
    {
463
        $name = trim($name);
464
        if (empty($name)) {
465
            return false;
466
        }
467
468
        $i = file_put_contents(
469
            $this->watchedFunctionsPathPrefix.$id.'.json',
470
            json_encode(['id'=>$id, 'name'=>trim($name)])
471
        );
472
        return $i > 0;
473
    }
474
475
    /**
476
     * @inheritDoc
477
     * @param $id
478
     */
479
    public function removeWatchedFunction($id)
480
    {
481
        if (file_exists($this->watchedFunctionsPathPrefix.$id.'.json')) {
482
            unlink($this->watchedFunctionsPathPrefix.$id.'.json');
483
        }
484
    }
485
486
    /**
487
     * Parse filename and try to get request time from filename
488
     *
489
     * @param $fileName
490
     * @return bool|DateTime
491
     */
492
    public function getRequestTimeFromFilename($fileName)
493
    {
494
        $matches = [];
495
        // default pattern is: xhgui.data.<timestamp>.<microseconds>_a68888
496
        //  xhgui.data.15 55 31 04 66 .6606_a68888
497
        preg_match('/(?<t>[\d]{10})(\.(?<m>[\d]{1,6}))?.+/i', $fileName, $matches);
498
        try {
499
            return DateTime::createFromFormat('U u', $matches['t'].' '. $matches['m']);
500
        } catch (Exception $e) {
501
            return null;
502
        }
503
    }
504
}
505