Passed
Push — master ( 477212...0ba014 )
by Sergey
02:48
created

Cache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 0
cbo 2
dl 0
loc 68
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFacadeComponentId() 0 4 1
A get() 0 12 3
A getOrSet() 0 17 3
1
<?php
2
/**
3
 * Facades for Yii 2
4
 *
5
 * @see       https://github.com/sergeymakinen/yii2-facades
6
 * @copyright Copyright (c) 2016 Sergey Makinen (https://makinen.ru)
7
 * @license   https://github.com/sergeymakinen/yii2-facades/blob/master/LICENSE The MIT License
8
 */
9
10
namespace sergeymakinen\facades;
11
12
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
13
/**
14
 * Cache facade.
15
 *
16
 * Facades Yii::$app->get('cache') component.
17
 *
18
 * @see \yii\caching\Cache
19
 * @method static bool add(mixed $key, mixed $value, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores a value identified by a key into cache if the cache does not contain this key.
20
 * @method static \yii\base\Behavior attachBehavior(string $name, string|array|\yii\base\Behavior $behavior) Attaches a behavior to this component.
21
 * @method static attachBehaviors(array $behaviors) Attaches a list of behaviors to the component.
22
 * @method static array behaviors() Returns a list of behaviors that this component should behave as.
23
 * @method static string buildKey(mixed $key) Builds a normalized cache key from a given key.
24
 * @method static bool delete(mixed $key) Deletes a value with the specified key from cache.
25
 * @method static null|\yii\base\Behavior detachBehavior(string $name) Detaches a behavior from the component.
26
 * @method static detachBehaviors() Detaches all behaviors from the component.
27
 * @method static ensureBehaviors() Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
28
 * @method static bool exists(mixed $key) Checks whether a specified key exists in the cache.
29
 * @method static bool flush() Deletes all values from cache.
30
 * @method static null|\yii\base\Behavior getBehavior(string $name) Returns the named behavior object.
31
 * @method static \yii\base\Behavior[] getBehaviors() Returns all behaviors attached to this component.
32
 * @method static string getKeyPrefix() Returns a string prefixed to every cache key so that it is unique globally in the whole cache storage.
33
 * @method static array|bool getSerializer() Returns the functions used to serialize and unserialize cached data.
34
 * @method static bool hasEventHandlers(string $name) Returns a value indicating whether there is any handler attached to the named event.
35
 * @method static bool madd(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
36
 * @method static array mget(string[] $keys) Retrieves multiple values from cache with the specified keys.
37
 * @method static bool mset(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
38
 * @method static bool multiAdd(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
39
 * @method static array multiGet(string[] $keys) Retrieves multiple values from cache with the specified keys.
40
 * @method static bool multiSet(array $items, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores multiple items in cache.
41
 * @method static bool off(string $name, callable $handler = null) Detaches an existing event handler from this component.
42
 * @method static on(string $name, callable $handler, mixed $data = null, bool $append = true) Attaches an event handler to an event.
43
 * @method static bool set(mixed $key, mixed $value, int $duration = 0, \yii\caching\Dependency $dependency = null) Stores a value identified by a key into cache.
44
 * @method static setKeyPrefix(string $value) Sets a string prefixed to every cache key so that it is unique globally in the whole cache storage.
45
 * @method static setSerializer(array|bool $value) Sets the functions used to serialize and unserialize cached data.
46
 * @method static trigger(string $name, \yii\base\Event $event = null) Triggers an event.
47
 */
48
class Cache extends Facade
49
{
50
    /**
51
     * @inheritdoc
52
     */
53
    public static function getFacadeComponentId()
54
    {
55
        return 'cache';
56
    }
57
58
    /**
59
     * Retrieves a value from cache with a specified key.
60
     *
61
     * @param mixed $key a key identifying the cached value. This can be a simple string or
62
     * a complex data structure consisting of factors representing the key.
63
     * @param callable|mixed $default a default value. This can also be any callable function that
64
     * will recieve the key and should return a value.
65
     *
66
     * @return mixed the value stored in cache or the default value if the value is not in the cache, expired,
67
     * or the dependency associated with the cached data has changed.
68
     */
69
    public static function get($key, $default = false)
70
    {
71
        $value = static::getFacadeComponent()->get($key);
72
        if ($value === false) {
73
            if ($default instanceof \Closure) {
74
                $value = call_user_func($default, $key);
75
            } else {
76
                $value = $default;
77
            }
78
        }
79
        return $value;
80
    }
81
82
    /**
83
     * Retrieves a value from cache (or stores it if it's not cached) with a specified key.
84
     * If the cache already contains such a key, it will be returned, else the default value
85
     * will be stored in the cache and returned.
86
     *
87
     * @param mixed $key a key identifying the cached value. This can be a simple string or
88
     * a complex data structure consisting of factors representing the key.
89
     * @param callable|mixed $default the value to be cached and retrieved if some value is not already in the cache. This can also be
90
     * any callable function that will recieve the key and should return a value.
91
     * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
92
     * @param \yii\caching\Dependency $dependency dependency of the cached item. If the dependency changes,
93
     * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
94
     * This parameter is ignored if [[serializer]] is false.
95
     *
96
     * @return bool whether the value is successfully stored into cache
97
     */
98
    public static function getOrSet($key, $default, $duration = 0, $dependency = null)
99
    {
100
        /**
101
         * @var \yii\caching\Cache $cache
102
         */
103
        $cache = static::getFacadeComponent();
104
        $value = $cache->get($key);
105
        if ($value === false) {
106
            if ($default instanceof \Closure) {
107
                $value = call_user_func($default, $key);
108
            } else {
109
                $value = $default;
110
            }
111
            $cache->set($key, $value, $duration, $dependency);
112
        }
113
        return $value;
114
    }
115
}
116