Passed
Push — master ( 0d0f52...07463e )
by Yuichi
02:21
created

Valiable::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
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 11
    public function __construct(Cache $cache)
14
    {
15 11
        $this->cache = $cache;
16 11
    }
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 2
        } 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 2
    public function importYaml($yaml_data)
53
    {
54 2
        $data = Yaml::parse($yaml_data);
55 2
        if ( is_array($data) ) {
56 2
            foreach ( $data as $name => $rec ) {
57 2
                foreach ( $rec as $key => $values ) {
58 2
                    if ( isset($values['value'] ) ) {
59 1
                        $this->set($name . '_' . $key,$values['value']);
60 1
                    } else {
61 1
                        throw new \Exception('valueが見つかりません。key=' . $key);
62 1
                    }
63 1
                }
64 1
            }
65 1
        }
66 1
    }
67 1
    public function clear()
68
    {
69 1
        $names = $this->getNames();
70 1
        if ( count($names) > 0 ) {
71 1
            foreach ( $names as $name ) {
72 1
                $this->cache->forget($this->prefix . $name);
73 1
            }
74 1
        }
75 1
        $this->cache->forget($this->name_list);
76 1
    }
77 2
    protected function addNames($name)
78
    {
79 2
        $names = $this->getNames();
80 2
        if ( in_array($name,$names) === false ) {
81 2
            $names[] = $name;
82 2
        }
83 2
        $this->cache->forever($this->name_list,$this->encode($names));
84 2
    }
85 2
    protected function encode($data)
86
    {
87 2
        return base64_encode(serialize($data));
88
    }
89 5
    protected function decode($data)
90
    {
91 5
        return unserialize(base64_decode($data));
92
    }
93
}
94