NormalizingSaver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A save() 0 33 4
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