Completed
Push — master ( cd46ad...a80418 )
by
unknown
37:43
created

CacheStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 1
A set() 0 6 1
A getOrSet() 0 8 1
A delete() 0 6 1
A buildKey() 0 6 1
A getStorage() 0 4 1
1
<?php
2
3
namespace yiicod\geo\storages;
4
5
use Yii;
6
use yii\caching\CacheInterface;
7
8
/**
9
 * Class CacheStorage
10
 * Storage that uses Yii cache functionality
11
 *
12
 * @package yiicod\geo\storages
13
 *
14
 * @author Dmitry Turchanin
15
 */
16
class CacheStorage implements StorageInterface
17
{
18
    /**
19
     * Cache component name
20
     *
21
     * @var string
22
     */
23
    public $component = 'cache';
24
25
    /**
26
     * Gets data for $key from storage
27
     *
28
     * @param $key
29
     *
30
     * @return mixed
31
     */
32
    public function get($key)
33
    {
34
        $key = self::buildKey($key);
35
36
        $value = $this->getStorage()->get($key);
37
        return $value;
38
    }
39
40
    /**
41
     * Stores data for $key into storage
42
     *
43
     * @param $key
44
     * @param $value
45
     * @param int $duration
46
     */
47
    public function set($key, $value, $duration = 0): void
48
    {
49
        $key = self::buildKey($key);
50
51
        $this->getStorage()->set($key, $value, $duration);
52
    }
53
54
    /**
55
     * Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key,
56
     * or to store the result of $callable execution if there is no cache available for the $ip.
57
     *
58
     * @param $key
59
     * @param $callable
60
     * @param int $duration
61
     *
62
     * @return mixed
63
     */
64
    public function getOrSet($key, $callable, $duration = 0)
65
    {
66
        $key = self::buildKey($key);
67
68
        $value = $this->getStorage()->getOrSet($key, $callable, $duration);
69
70
        return $value;
71
    }
72
73
    /**
74
     * Deletes record from storage for $key
75
     *
76
     * @param $key
77
     *
78
     * @return mixed
79
     */
80
    public function delete($key): void
81
    {
82
        $key = self::buildKey($key);
83
84
        $this->getStorage()->delete($key);
85
    }
86
87
    /**
88
     * Builds a normalized storage key from a given $key.
89
     *
90
     * @param mixed $key the key to be normalized
91
     *
92
     * @return string the generated cache key
93
     */
94
    public function buildKey($key): string
95
    {
96
        $key = sprintf('geo-getter-data-%s', $key);
97
98
        return $key;
99
    }
100
101
    /**
102
     * Returns storage component
103
     *
104
     * @return mixed|CacheInterface
105
     */
106
    private function getStorage()
107
    {
108
        return Yii::$app->{$this->component};
109
    }
110
}