Completed
Push — update-2019 ( 09caf7 )
by Richan
01:22
created

CacheableService::wrapWithDecorator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Suitmedia\Cacheable;
4
5
use Closure;
6
use Illuminate\Cache\ArrayStore;
7
use Illuminate\Cache\CacheManager;
8
use Illuminate\Cache\TaggableStore;
9
use Illuminate\Cache\TaggedCache;
10
use Suitmedia\Cacheable\Contracts\CacheableRepository;
11
12
class CacheableService
13
{
14
    /**
15
     * Cache manager object.
16
     *
17
     * @var \Illuminate\Cache\TaggableStore
18
     */
19
    protected $cache;
20
21
    /**
22
     * Cacheable configurations.
23
     *
24
     * @var array
25
     */
26
    protected $configurations;
27
28
    /**
29
     * Runtime cache.
30
     *
31
     * @var \Illuminate\Cache\ArrayStore
32
     */
33
    protected $runtimeCache;
34
35
    /**
36
     * Class constructor.
37
     *
38
     * @param \Illuminate\Cache\CacheManager $cache
39
     * @param \Illuminate\Cache\ArrayStore   $runtimeCache
40
     */
41
    public function __construct(CacheManager $cache, ArrayStore $runtimeCache)
42
    {
43
        $this->cache = $cache->store()->getStore();
44
        $this->configurations = \Config::get('cacheable');
45
        $this->runtimeCache = $runtimeCache;
46
    }
47
48
    /**
49
     * Just an alias of wrap() method.
50
     *
51
     * @param mixed $repository
52
     *
53
     * @return \Suitmedia\Cacheable\CacheableDecorator
54
     */
55
    public function build($repository)
56
    {
57
        return $this->wrap($repository);
58
    }
59
60
    /**
61
     * Flush cache.
62
     *
63
     * @param mixed $tags
64
     *
65
     * @return void
66
     */
67
    public function flush($tags = null)
68
    {
69
        $this->taggedCache($this->cache, $tags)->flush();
70
        $this->taggedCache($this->runtimeCache, $tags)->flush();
71
    }
72
73
    /**
74
     * Get configuration value.
75
     *
76
     * @param string $key
77
     *
78
     * @return mixed
79
     */
80
    public function getConfiguration($key)
81
    {
82
        return data_get($this->configurations, $key, null);
83
    }
84
85
    /**
86
     * Get runtime cached object.
87
     *
88
     * @param mixed  $tags
89
     * @param string $key
90
     *
91
     * @return mixed
92
     */
93
    protected function getRuntimeCache($tags, $key)
94
    {
95
        return $this->runtimeCache->tags($tags)->get($key);
96
    }
97
98
    /**
99
     * Retrieve cached items.
100
     *
101
     * @param mixed   $tags
102
     * @param string  $key
103
     * @param int     $duration
104
     * @param Closure $callable
105
     *
106
     * @return mixed
107
     */
108
    public function retrieve($tags, $key, $duration, Closure $callable)
109
    {
110
        if ($data = $this->getRuntimeCache($tags, $key)) {
111
            return $data;
112
        }
113
114
        $cache = $this->taggedCache($this->cache, $tags);
115
116
        $data = ($duration > 0) ?
117
            $cache->remember($key, $duration, $callable) :
118
            $cache->rememberForever($key, $callable);
119
120
        $this->setRuntimeCache($tags, $key, $data);
121
122
        return $data;
123
    }
124
125
    /**
126
     * Set runtime cache object.
127
     *
128
     * @param mixed  $tags
129
     * @param string $key
130
     * @param mixed  $value
131
     *
132
     * @return void
133
     */
134
    protected function setRuntimeCache($tags, $key, $value)
135
    {
136
        $this->runtimeCache->tags($tags)->forever($key, $value);
137
    }
138
139
    /**
140
     * Get tagged cache object.
141
     *
142
     * @param TaggableStore $cache
143
     * @param mixed         $tags
144
     *
145
     * @return \Illuminate\Cache\TaggedCache|TaggableStore
146
     */
147
    protected function taggedCache(TaggableStore $cache, $tags)
148
    {
149
        return !empty($tags) ? $cache->tags($tags) : $cache;
150
    }
151
152
    /**
153
     * Build CacheableDecorator and wrap the given class name
154
     * or repository object.
155
     *
156
     * @param mixed $repository
157
     *
158
     * @return \Suitmedia\Cacheable\CacheableDecorator
159
     */
160
    public function wrap($repository)
161
    {
162
        if (is_string($repository)) {
163
            $repository = \App::make($repository);
164
        }
165
166
        return $this->wrapWithDecorator($repository);
167
    }
168
169
    /**
170
     * Wrap the given CacheableRepository with a new CacheableDecorator.
171
     *
172
     * @param \Suitmedia\Cacheable\Contracts\CacheableRepository $repository
173
     *
174
     * @return \Suitmedia\Cacheable\CacheableDecorator
175
     */
176
    protected function wrapWithDecorator(CacheableRepository $repository)
177
    {
178
        return new CacheableDecorator($this, $repository);
179
    }
180
}
181