Completed
Push — master ( d69358...27ffc6 )
by Dmitry
04:53 queued 53s
created

Cache   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 58
ccs 0
cts 15
cp 0
rs 10
wmc 3
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTimeCached() 0 11 2
A getAuthTimeCached() 0 5 1
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\components;
13
14
use Closure;
15
use ReflectionFunction;
16
use Yii;
17
18
class Cache extends \yii\caching\FileCache
19
{
20
    /**
21
     * Caches a [[\Closure]] call, invalidates on timeout.
22
     *
23
     * @param int $timeout seconds
24
     * @param array $params an array of params that will be passed to the $func
25
     * @param Closure $function the function that will be called
26
     * @return mixed
27
     */
28
    public function getTimeCached($timeout, array $params, Closure $function)
29
    {
30
        $key = array_merge([__CLASS__, ReflectionFunction::export($function, true)], $params);
31
        $res = $this->get($key);
32
        if ($res === false) {
33
            $res = call_user_func_array($function, $params);
34
            $this->set($key, $res, $timeout);
35
        }
36
37
        return $res;
38
    }
39
40
    /**
41
     * Caches a [[\Closure]] call, invalidates on timeout.
42
     * Forcefully adds current logged in user ID to the params to be a part of the key.
43
     *
44
     * @param int $timeout seconds
45
     * @param array $params an array of params that will be passed to the $func
46
     * @param Closure $function the function that will be called
47
     * @return mixed
48
     */
49
    public function getAuthTimeCached($timeout, array $params, Closure $function)
50
    {
51
        $params[] = Yii::$app->user->id;
52
        return $this->getTimeCached($timeout, $params, $function);
53
    }
54
55
/* TRY to realize later
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
56
    public function init()
57
    {
58
        parent::init();
59
        if (!is_object($this->cache)) {
60
            $this->cache = Instance::ensure($this->cache, yiiCache::className());
61
        }
62
    }
63
64
    public function getTimeCached ($timeout, array $params, $func) {
65
        $key = array_merge([__CLASS__], $params);
66
        $res = $this->cache->get($key);
67
        if ($res === false) {
68
            $res = call_user_func_array($func, $params);
69
            $this->getCache()->set($key, $res, $timeout);
70
        }
71
72
        return $res;
73
    }
74
*/
75
}
76