Completed
Push — master ( 4f1b0c...6664b8 )
by Elan
15s queued 11s
created

NormalizingSaver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
        foreach ($data['profile'] as $index => &$profile) {
19
            // skip empty profilings
20
            if (!$profile) {
21
                unset($data['profile'][$index]);
22
                continue;
23
            }
24
25
            // normalize, fill all missing keys
26
            $profile += [
27
                'ct' => 0,
28
                'wt' => 0,
29
                'cpu' => 0,
30
                'mu' => 0,
31
                'pmu' => 0,
32
            ];
33
        }
34
        unset($profile);
35
36
        if (!$data['profile']) {
37
            throw new RuntimeException("Skipping to save empty profiling");
38
        }
39
40
        return $this->saver->save($data, $id);
41
    }
42
}
43