Valiable::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Sonar\Valiable;
3
4
use Illuminate\Cache\Repository as Cache;
5
use Symfony\Component\Yaml\Yaml;
6
7
class Valiable
8
{
9
    protected $prefix = "sonar_valiable_";
10
    protected $name_list = "sonar_valiables";
11
    private $cache;
12
13 12
    public function __construct(Cache $cache)
14
    {
15 12
        $this->cache = $cache;
16 12
    }
17 1
    public function get($name)
18
    {
19 1
        $data = $this->cache->get($this->prefix . $name);
20
21 1
        return $data ? $this->decode($data) : null;
22
    }
23 3
    public function getValue($name,$key)
24
    {
25 3
        if ( is_null($key) ) {
26 1
            throw new \Exception('value is null');
27
        }
28 2
        $data = $this->cache->get($this->prefix . $name);
29 2
        if ( ! $data ) return null;
30
31 2
        $data = $this->decode($data);
32 2
        if ( isset($data[$key]) === false  ) {
33 1
            throw new \Exception('value is not found. key=' . $key);
34
        }
35 1
        return $data[$key];
36
    }
37 5
    public function getNames()
38
    {
39 5
        $names = $this->cache->get($this->name_list);
40 5
        if ( $names ) {
41 2
            $names = $this->decode($names);
42
        } else {
43 3
            $names = [];
44
        }
45 5
        return $names;
46
    }
47 2
    public function set($name,$data)
48
    {
49 2
        $this->cache->forever($this->prefix . $name,$this->encode($data));
50 2
        $this->addNames($name);
51 2
    }
52 3
    public function importYaml($yaml_data)
53
    {
54 3
        $data = Yaml::parse($yaml_data);
55 3
        if ( is_array($data) === false) {
56 1
            return;
57
        }
58 2
        foreach ( $data as $name => $rec ) {
59 2
            foreach ( $rec as $key => $values ) {
60 2
                if ( isset($values['value']) === true) {
61 1
                    $this->set($name . '_' . $key,$values['value']);
62
                } else {
63 1
                    throw new \Exception('valueが見つかりません。key=' . $key);
64
                }
65
            }
66
        }
67 1
    }
68 1
    public function clear()
69
    {
70 1
        $names = $this->getNames();
71 1
        if ( count($names) > 0 ) {
72 1
            foreach ( $names as $name ) {
73 1
                $this->cache->forget($this->prefix . $name);
74
            }
75
        }
76 1
        $this->cache->forget($this->name_list);
77 1
    }
78 2
    protected function addNames($name)
79
    {
80 2
        $names = $this->getNames();
81 2
        if ( in_array($name,$names) === false ) {
82 2
            $names[] = $name;
83
        }
84 2
        $this->cache->forever($this->name_list,$this->encode($names));
85 2
    }
86 2
    protected function encode($data)
87
    {
88 2
        return base64_encode(serialize($data));
89
    }
90 5
    protected function decode($data)
91
    {
92 5
        return unserialize(base64_decode($data));
93
    }
94
}
95