CacheDecorator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setSetting() 0 13 1
A getSetting() 0 8 3
A allToArray() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of ibrand/setting.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Setting\Repositories;
13
14
/**
15
 * Class CacheDecorator
16
 * @package iBrand\Component\Setting\Repositories
17
 */
18
class CacheDecorator implements SettingInterface
19
{
20
    /**
21
     * @var SettingInterface
22
     */
23
    private $repo;
24
    /**
25
     * @var mixed
26
     */
27
    private $cache;
28
29
    /**
30
     * @var string
31
     */
32
    private $key;
33
34
    /**
35
     * CacheDecorator constructor.
36
     * @param SettingInterface $repo
37
     */
38 5
    public function __construct(SettingInterface $repo)
39
    {
40 5
        $this->repo = $repo;
41 5
        $this->cache = cache();
42 5
        $this->key = md5('ibrand.setting');
43 5
    }
44
45
    /**
46
     * @param array $settings
47
     * @return mixed
48
     */
49 4
    public function setSetting(array $settings)
50
    {
51 4
        $cacheKey = $this->key;
52
53 4
        $this->cache->forget($cacheKey);
54
55 4
        $result = $this->repo->setSetting($settings);
56
57 4
        $this->cache->put($cacheKey, $this->repo->allToArray(), config('ibrand.setting.minute'));
58
59 4
        return $result;
60
61
    }
62
63
64
    /**
65
     * @param $key
66
     * @param null $default
67
     * @return mixed|string
68
     */
69 3
    public function getSetting($key, $default = null)
70
    {
71 3
        $allSettings = $this->allToArray();
72
73 3
        $value = $default ? $default : '';
74
75 3
        return isset($allSettings[$key]) ? $allSettings[$key] : $value;
76
    }
77
78
    /**
79
     * @return mixed
80
     */
81 3
    public function allToArray()
82
    {
83 3
        $cacheKey = $this->key;
84
85 3
        $data = $this->cache->remember($cacheKey, config('ibrand.setting.minute'), function () {
86 1
            return $this->repo->allToArray();
87 3
        });
88
89 3
        return $data;
90
    }
91
}
92