Completed
Push — develop ( 053c5a...b8c00a )
by Sean
02:16
created

Illuminate::determineTagSupport()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7.1754

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 5
cts 12
cp 0.4167
rs 9.0534
cc 4
eloc 12
nc 4
nop 0
crap 7.1754
1
<?php
2
3
/*
4
 * This file is part of jwt-auth
5
 *
6
 * (c) Sean Tymon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tymon\JWTAuth\Providers\Storage;
13
14
use Tymon\JWTAuth\Contracts\Providers\Storage;
15
use Illuminate\Contracts\Cache\Repository as CacheContract;
16
17
class Illuminate implements Storage
18
{
19
    /**
20
     * @var \Illuminate\Contracts\Cache\Repository
21
     */
22
    protected $cache;
23
24
    /**
25
     * @var string
26
     */
27
    protected $tag = 'tymon.jwt';
28
    
29
    /**
30
     * @var boolean
31
     */
32
    protected $supportsTags;
33
34
    /**
35
     * @param \Illuminate\Contracts\Cache\Repository  $cache
36
     */
37 20
    public function __construct(CacheContract $cache)
38
    {
39 20
        $this->cache = $cache;
40 20
    }
41
42
    /**
43
     * Add a new item into storage
44
     *
45
     * @param  string  $key
46
     * @param  mixed  $value
47
     * @param  int  $minutes
48
     *
49
     * @return void
50
     */
51 4
    public function add($key, $value, $minutes)
52
    {
53 4
        $this->cache()->put($key, $value, $minutes);
54 4
    }
55
56
    /**
57
     * Add a new item into storage forever
58
     *
59
     * @param  string  $key
60
     * @param  mixed  $value
61
     *
62
     * @return void
63
     */
64 4
    public function forever($key, $value)
65
    {
66 4
        $this->cache()->forever($key, $value);
67 4
    }
68
69
    /**
70
     * Get an item from storage
71
     *
72
     * @param  string  $key
73
     *
74
     * @return mixed
75
     */
76 4
    public function get($key)
77
    {
78 4
        return $this->cache()->get($key);
79
    }
80
81
    /**
82
     * Remove an item from storage
83
     *
84
     * @param  string  $key
85
     *
86
     * @return boolean
87
     */
88 4
    public function destroy($key)
89
    {
90 4
        return $this->cache()->forget($key);
91
    }
92
93
    /**
94
     * Remove all items associated with the tag
95
     *
96
     * @return void
97
     */
98 4
    public function flush()
99
    {
100 4
        $this->cache()->flush();
101 4
    }
102
103
    /**
104
     * Return the cache instance with tags attached
105
     *
106
     * @return \Illuminate\Contracts\Cache\Repository
107
     */
108 20
    protected function cache()
109
    {
110 20
        if (is_null($this->supportsTags)) $this->determineTagSupport();
111
112 20
        if ($this->supportsTags) {
113 10
            return $this->cache->tags($this->tag);
114
        } else {
115 10
            return $this->cache;
116
        }
117
    }
118
119
    /**
120
     * Detect as best we can whether tags are supported with this repository & store,
121
     * and save our result on the $supportsTags flag.
122
     *
123
     * @return void
124
     */
125 10
    protected function determineTagSupport()
126
    {
127 10
        if (method_exists($this->cache, 'tags')) { // Laravel >= 5.1.28
128
            try {
129
                // Attempt the repository tags command, which throws exceptions when unsupported
130
                $this->cache->tags($this->tag);
0 ignored issues
show
Bug introduced by
The method tags() does not seem to exist on object<Illuminate\Contracts\Cache\Repository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
                $this->supportsTags = true;
132
            } catch (\BadMethodCallException $ex) {
133
                $this->supportsTags = false;
134
            }
135
        } else {
136 10
            if (method_exists($this->cache, 'getStore')) { // Laravel <= 5.1.27
137
                // Check for the tags function directly on the store
138
                $this->supportsTags = method_exists($this->cache->getStore(), 'tags');
0 ignored issues
show
Bug introduced by
The method getStore() does not seem to exist on object<Illuminate\Contracts\Cache\Repository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
            } else {
140
                // Must be using custom cache repository without getStore(), and all bets are off,
141
                // or we are mocking the cache contract (in testing), which will not create a getStore method
142 10
                $this->supportsTags = false;
143
            }
144
        }
145 10
    }
146
}
147