Completed
Push — master ( 0dd6d6...e92646 )
by Freek
14s queued 10s
created

PartialCache::resolveCacheDuration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\PartialCache;
4
5
use Illuminate\Cache\TaggableStore;
6
use Illuminate\Contracts\Cache\Factory as CacheManager;
7
use Illuminate\Contracts\Cache\Repository as Cache;
8
use Illuminate\Contracts\Config\Repository as Config;
9
use Illuminate\Contracts\View\Factory as View;
10
use Spatie\PartialCache\Exceptions\MethodNotSupportedException;
11
12
class PartialCache
13
{
14
    /** @var \Illuminate\Contracts\View\Factory */
15
    protected $view;
16
17
    /** @var \Illuminate\Contracts\Cache\Repository */
18
    protected $cache;
19
20
    /** @var \Illuminate\Contracts\Cache\Factory */
21
    protected $cacheManager;
22
23
    /** @var string */
24
    protected $cacheKey;
25
26
    /** @var bool */
27
    protected $cacheIsTaggable;
28
29
    /** @var bool */
30
    protected $enabled;
31
32
    /** @var int|null */
33
    protected $duration;
34
35
    public function __construct(View $view, Cache $cache, CacheManager $cacheManager, Config $config)
36
    {
37
        $this->view = $view;
38
        $this->cache = $cache;
39
        $this->cacheManager = $cacheManager;
40
41
        $this->cacheKey = $config->get('partialcache.key');
42
        $this->cacheIsTaggable = is_a($this->cacheManager->driver()->getStore(), TaggableStore::class);
43
        $this->enabled = $this->determineEnabled($config);
44
        $this->duration = $config->get('partialcache.default_duration');
45
    }
46
47
    /**
48
     * Cache a view. If minutes are null, the view is cached forever.
49
     *
50
     * @param array        $data
51
     * @param string       $view
52
     * @param array        $mergeData
53
     * @param int          $minutes
54
     * @param string       $key
55
     * @param string|array $tag
56
     *
57
     * @return string
58
     */
59
    public function cache($data, $view, $mergeData = null, $minutes = null, $key = null, $tag = null)
60
    {
61
        if (!$this->enabled) {
62
            return call_user_func($this->renderView($view, $data, $mergeData));
0 ignored issues
show
Bug introduced by Sebastian De Deyne
It seems like $mergeData defined by parameter $mergeData on line 59 can also be of type null; however, Spatie\PartialCache\PartialCache::renderView() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
63
        }
64
65
        $viewKey = $this->getCacheKeyForView($view, $key);
66
67
        $mergeData = $mergeData ?: [];
68
69
        $tags = $this->getTags($tag);
70
71
        $minutes = $this->resolveCacheDuration($minutes);
72
73 View Code Duplication
        if ($this->cacheIsTaggable && $minutes === null) {
0 ignored issues
show
Duplication introduced by Sebastian De Deyne
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            return $this->cache
75
                ->tags($tags)
76
                ->rememberForever($viewKey, $this->renderView($view, $data, $mergeData));
77
        }
78
79 View Code Duplication
        if ($this->cacheIsTaggable) {
0 ignored issues
show
Duplication introduced by Sebastian De Deyne
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
            return $this->cache
81
                ->tags($tags)
82
                ->remember($viewKey, $minutes, $this->renderView($view, $data, $mergeData));
83
        }
84
85
        if ($minutes === null) {
86
            return $this->cache
87
                ->rememberForever($viewKey, $this->renderView($view, $data, $mergeData));
88
        }
89
90
        return $this->cache
91
            ->remember($viewKey, $minutes, $this->renderView($view, $data, $mergeData));
92
    }
93
94
    /**
95
     * Create a key name for the cached view.
96
     *
97
     * @param string $view
98
     * @param string $key
99
     *
100
     * @return string
101
     */
102
    public function getCacheKeyForView($view, $key = null)
103
    {
104
        $parts = [$this->cacheKey, $view];
105
106
        if ($key !== null) {
107
            $parts[] = $key;
108
        }
109
110
        return implode('.', $parts);
111
    }
112
113
    /**
114
     * Forget a rendered view.
115
     *
116
     * @param string $view
117
     * @param string $key
118
     * @param null|string|array $tag
119
     */
120
    public function forget($view, $key = null, $tag = null)
121
    {
122
        $cacheKey = $this->getCacheKeyForView($view, $key);
123
124
        if ($this->cacheIsTaggable) {
125
            $tags = $this->getTags($tag);
126
            $this->cache->tags($tags)->forget($cacheKey);
127
        }
128
129
        $this->cache->forget($cacheKey);
130
    }
131
132
    /**
133
     * Empty all views linked to a tag or the complete partial cache.
134
     * Note: Only supported by Taggable cache drivers.
135
     *
136
     * @param string $tag
137
     *
138
     * @throws \Spatie\PartialCache\Exceptions\MethodNotSupportedException
139
     */
140
    public function flush($tag = null)
141
    {
142
        if (!$this->cacheIsTaggable) {
143
            throw new MethodNotSupportedException('The cache driver ('.
144
                get_class($this->cacheManager->driver()).') doesn\'t support the flush method.');
145
        }
146
147
        $tag = $tag ?: $this->cacheKey;
148
        $this->cache->tags($tag)->flush();
149
    }
150
151
    /**
152
     * Render a view.
153
     *
154
     * @param string $view
155
     * @param array  $data
156
     * @param array  $mergeData
157
     *
158
     * @return string
159
     */
160
    protected function renderView($view, $data, $mergeData)
161
    {
162
        $data = $data ?: [];
163
        $mergeData = $mergeData ?: [];
164
165
        return function () use ($view, $data, $mergeData) {
166
            return $this->view->make($view, $data, $mergeData)->render();
167
        };
168
    }
169
170
    /**
171
     * Constructs tag array
172
     *
173
     * @param null|string|array $tag
174
     *
175
     * @return array
176
     */
177
    protected function getTags($tag = null)
178
    {
179
        $tags = [$this->cacheKey];
180
181
        if ($tag) {
182
            if (!is_array($tag)) {
183
                $tag = [$tag];
184
            }
185
186
            $tags = array_merge($tags, $tag);
187
        }
188
189
        return $tags;
190
    }
191
192
    protected function determineEnabled(Config $config)
193
    {
194
        $configValue = $config->get('partialcache.enabled');
195
196
        /**
197
         * Previous versions of the package mistakenly used a string for the enabled setting.
198
         */
199
        if (is_string($config)) {
200
            return filter_var($configValue,FILTER_VALIDATE_BOOLEAN);
201
        }
202
203
        return $configValue;
204
    }
205
206
    /**
207
     * Resolve cache duration, defaults to the config if minutes is null
208
     *
209
     * @param int|null $minutes
210
     * @return int|null
211
     */
212
    protected function resolveCacheDuration($minutes = null)
213
    {
214
        if (is_null($minutes)) {
215
            return $this->duration;
216
        }
217
218
        return $minutes;
219
    }
220
}
221