NormalizingSaver::save()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 4
nc 6
nop 2
1
<?php
2
3
namespace XHGui\Saver;
4
5
use RuntimeException;
6
7
class NormalizingSaver implements SaverInterface
8
{
9
    private $saver;
10
11
    public function __construct(SaverInterface $saver)
12
    {
13
        $this->saver = $saver;
14
    }
15
16
    public function save(array $data, string $id = null): string
17
    {
18
        // extract "get" from "url"
19
        // profiler no longer needs to send "get" over the wire.
20
        // the individual savers may choose not to store this down separately
21
        $query = parse_url($data['meta']['url'], PHP_URL_QUERY);
22
        parse_str($query, $get);
23
        $data['meta']['get'] = $get;
24
25
        foreach ($data['profile'] as $index => &$profile) {
26
            // skip empty profilings
27
            if (!$profile) {
28
                unset($data['profile'][$index]);
29
                continue;
30
            }
31
32
            // normalize, fill all missing keys
33
            $profile += [
34
                'ct' => 0,
35
                'wt' => 0,
36
                'cpu' => 0,
37
                'mu' => 0,
38
                'pmu' => 0,
39
            ];
40
        }
41
        unset($profile);
42
43
        if (!$data['profile']) {
44
            throw new RuntimeException('Skipping to save empty profiling');
45
        }
46
47
        return $this->saver->save($data, $id);
48
    }
49
}
50