Completed
Push — master ( 288712...0dd6d6 )
by Freek
02:21
created

PartialCache::determineEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 5
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
    public function __construct(View $view, Cache $cache, CacheManager $cacheManager, Config $config)
33
    {
34
        $this->view = $view;
35
        $this->cache = $cache;
36
        $this->cacheManager = $cacheManager;
37
38
        $this->cacheKey = $config->get('partialcache.key');
39
        $this->cacheIsTaggable = is_a($this->cacheManager->driver()->getStore(), TaggableStore::class);
40
        $this->enabled = $this->determineEnabled($config);
41
    }
42
43
    /**
44
     * Cache a view. If minutes are null, the view is cached forever.
45
     *
46
     * @param array        $data
47
     * @param string       $view
48
     * @param array        $mergeData
49
     * @param int          $minutes
50
     * @param string       $key
51
     * @param string|array $tag
52
     *
53
     * @return string
54
     */
55
    public function cache($data, $view, $mergeData = null, $minutes = null, $key = null, $tag = null)
56
    {
57
        if (!$this->enabled) {
58
            return call_user_func($this->renderView($view, $data, $mergeData));
0 ignored issues
show
Bug introduced by
It seems like $mergeData defined by parameter $mergeData on line 55 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...
59
        }
60
61
        $viewKey = $this->getCacheKeyForView($view, $key);
62
63
        $mergeData = $mergeData ?: [];
64
65
        $tags = $this->getTags($tag);
66
67 View Code Duplication
        if ($this->cacheIsTaggable && $minutes === null) {
0 ignored issues
show
Duplication introduced by
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...
68
            return $this->cache
69
                ->tags($tags)
70
                ->rememberForever($viewKey, $this->renderView($view, $data, $mergeData));
71
        }
72
73 View Code Duplication
        if ($this->cacheIsTaggable) {
0 ignored issues
show
Duplication introduced by
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
                ->remember($viewKey, $minutes, $this->renderView($view, $data, $mergeData));
77
        }
78
79
        if ($minutes === null) {
80
            return $this->cache
81
                ->rememberForever($viewKey, $this->renderView($view, $data, $mergeData));
82
        }
83
84
        return $this->cache
85
            ->remember($viewKey, $minutes, $this->renderView($view, $data, $mergeData));
86
    }
87
88
    /**
89
     * Create a key name for the cached view.
90
     *
91
     * @param string $view
92
     * @param string $key
93
     *
94
     * @return string
95
     */
96
    public function getCacheKeyForView($view, $key = null)
97
    {
98
        $parts = [$this->cacheKey, $view];
99
100
        if ($key !== null) {
101
            $parts[] = $key;
102
        }
103
104
        return implode('.', $parts);
105
    }
106
107
    /**
108
     * Forget a rendered view.
109
     *
110
     * @param string $view
111
     * @param string $key
112
     * @param null|string|array $tag
113
     */
114
    public function forget($view, $key = null, $tag = null)
115
    {
116
        $cacheKey = $this->getCacheKeyForView($view, $key);
117
118
        if ($this->cacheIsTaggable) {
119
            $tags = $this->getTags($tag);
120
            $this->cache->tags($tags)->forget($cacheKey);
121
        }
122
123
        $this->cache->forget($cacheKey);
124
    }
125
126
    /**
127
     * Empty all views linked to a tag or the complete partial cache.
128
     * Note: Only supported by Taggable cache drivers.
129
     *
130
     * @param string $tag
131
     *
132
     * @throws \Spatie\PartialCache\Exceptions\MethodNotSupportedException
133
     */
134
    public function flush($tag = null)
135
    {
136
        if (!$this->cacheIsTaggable) {
137
            throw new MethodNotSupportedException('The cache driver ('.
138
                get_class($this->cacheManager->driver()).') doesn\'t support the flush method.');
139
        }
140
141
        $tag = $tag ?: $this->cacheKey;
142
        $this->cache->tags($tag)->flush();
143
    }
144
145
    /**
146
     * Render a view.
147
     *
148
     * @param string $view
149
     * @param array  $data
150
     * @param array  $mergeData
151
     *
152
     * @return string
153
     */
154
    protected function renderView($view, $data, $mergeData)
155
    {
156
        $data = $data ?: [];
157
        $mergeData = $mergeData ?: [];
158
159
        return function () use ($view, $data, $mergeData) {
160
            return $this->view->make($view, $data, $mergeData)->render();
161
        };
162
    }
163
164
    /**
165
     * Constructs tag array
166
     *
167
     * @param null|string|array $tag
168
     *
169
     * @return array
170
     */
171
    protected function getTags($tag = null)
172
    {
173
        $tags = [$this->cacheKey];
174
175
        if ($tag) {
176
            if (!is_array($tag)) {
177
                $tag = [$tag];
178
            }
179
180
            $tags = array_merge($tags, $tag);
181
        }
182
183
        return $tags;
184
    }
185
186
    protected function determineEnabled(Config $config)
187
    {
188
        $configValue = $config->get('partialcache.enabled');
189
190
        /**
191
         * Previous versions of the package mistakenly used a string for the enabled setting.
192
         */
193
        if (is_string($config)) {
194
            return filter_var($configValue,FILTER_VALIDATE_BOOLEAN);
195
        }
196
197
        return $configValue;
198
    }
199
}
200