Test Setup Failed
Push — master ( 4f1524...9006a2 )
by Php Easy Api
04:31
created

CacheManager::get()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 48
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 10
nop 1
dl 0
loc 48
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Cache;
4
5
use Resta\Contracts\ApplicationContracts;
6
7
class CacheManager extends CacheAdapter
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $cache;
13
14
    /**
15
     * @var int $expire
16
     */
17
    protected $expire;
18
19
    /**
20
     * @var string $file
21
     */
22
    protected $adapter;
23
24
    /**
25
     * @var null $path
26
     */
27
    protected $path;
28
29
    /**
30
     * @var string $name
31
     */
32
    protected $name;
33
34
    /**
35
     * CacheManager constructor.
36
     * @param ApplicationContracts $app
37
     */
38
    public function __construct(ApplicationContracts $app)
39
    {
40
        parent::__construct($app);
41
42
        //get configuration variables from application
43
        $config = $this->app->resolve(CacheConfigDetector::class)->getConfig();
44
45
        $this->adapter  = $config['adapter'];
46
        $this->path     = $config['path'];
47
        $this->expire   = $config['expire'];
48
    }
49
50
    /**
51
     * change cache adapter
52
     *
53
     * @param null|string $adapter
54
     * @return $this
55
     */
56
    public function adapter($adapter)
57
    {
58
        if(!is_null($adapter)){
59
            $this->adapter = $adapter;
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * cache name
67
     *
68
     * @param null|string $name
69
     * @return $this
70
     */
71
    public function name($name)
72
    {
73
        //name variable is
74
        //the name of the cache data set to be created.
75
        if(!is_null($name)){
76
            $this->name = $name;
77
        }
78
79
        return $this;
80
    }
81
82
    /**
83
     * cache expire
84
     *
85
     * @param int|mixed $expire
86
     * @return $this
87
     */
88
    public function expire($expire)
89
    {
90
        //Cache data is set at the time.
91
        //Data will be valid in this time.
92
        if(is_numeric($expire)){
93
            $this->expire = $expire;
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * get cache
101
     *
102
     * @param callable $callback
103
     * @return mixed
104
     *
105
     * @throws \Psr\Cache\InvalidArgumentException
106
     */
107
    public function get(callable $callback)
108
    {
109
        // this class has a macro that can be managed by the user.
110
        // macros work as an extensible version of the classes.
111
        $macro = $this->app['macro']->with(config('kernel.macros.cache'),$this,$this->adapter);
112
113
        //set cache macroable object
114
        $this->cache = $macro->{$this->adapter}($callback);
115
116
        //With backtrace, we can specify an automatic name.
117
        //This will automatically detect which service is running in the service.
118
        $backtrace = debug_backtrace()[1];
119
120
        //If name is null, we name it with backtrace.
121
        if($this->name===null) {
122
            $this->name = md5($backtrace['function'].'_'.$backtrace['class']);
123
        }
124
125
        //this method may show continuity depending on the macro.
126
        if(false === $this instanceof $macro) return ;
127
128
        // retrieve the cache item
129
        $cacheItem = $this->cache->getItem($this->name);
130
131
        if (!$cacheItem->isHit()) {
132
133
            $data=call_user_func($callback);
134
135
            if($this->app->has('cache')
136
                && is_callable($cacheProvider = $this->app->get('cache'))){
137
138
                if($cacheProvider($data)){
139
                    $cacheItem->set($data);
140
                    $this->cache->save($cacheItem);
141
                }
142
            }
143
            else{
144
                $cacheItem->set($data);
145
                $this->cache->save($cacheItem);
146
            }
147
148
            return $data;
149
        }
150
151
        $this->app->register('illuminator','cache',['name'=>$this->name]);
152
153
        // retrieve the value stored by the item
154
        return $cacheItem->get();
155
    }
156
157
    /**
158
     * check macro availability for adapter method
159
     *
160
     * @param $class
161
     * @return mixed
162
     */
163
    private function macro($class)
164
    {
165
        return app()['macro'](Cache::class)->isMacro($class)->get(function() use($class){
166
            return $class;
167
        });
168
    }
169
}