Completed
Push — master ( c8cdf3...1aa220 )
by Aimeos
09:24
created

Laravel5   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 183
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 2
A clear() 0 4 1
A delete() 0 4 1
A deleteMultiple() 0 6 2
A deleteByTags() 0 5 1
A getMultiple() 0 15 3
A getMultipleByTags() 0 4 1
A set() 0 8 3
A setMultiple() 0 10 4
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package MW
7
 * @subpackage Cache
8
 */
9
10
11
namespace Aimeos\MW\Cache;
12
13
14
/**
15
 * Laravel 5 caching implementation.
16
 *
17
 * @package MW
18
 * @subpackage Cache
19
 */
20
class Laravel5
21
	extends \Aimeos\MW\Cache\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\MW\Cache\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	private $object;
25
26
27
	/**
28
	 * Initializes the object instance.
29
	 *
30
	 * @param \Illuminate\Contracts\Cache\Store $cache Laravel cache object
31
	 */
32
	public function __construct( \Illuminate\Contracts\Cache\Store $cache )
33
	{
34
		$this->object = $cache;
35
	}
36
37
38
	/**
39
	 * Removes all entries for the current site from the cache.
40
	 *
41
	 * @inheritDoc
42
	 */
43
	public function clear()
44
	{
45
		$this->object->flush();
46
	}
47
48
49
	/**
50
	 * Removes the cache entry identified by the given key.
51
	 *
52
	 * @inheritDoc
53
	 *
54
	 * @param string $key Key string that identifies the single cache entry
55
	 */
56
	public function delete( $key )
57
	{
58
		$this->object->forget( $key );
59
	}
60
61
62
	/**
63
	 * Removes the cache entries identified by the given keys.
64
	 *
65
	 * @inheritDoc
66
	 *
67
	 * @param string[] $keys List of key strings that identify the cache entries
68
	 * 	that should be removed
69
	 */
70
	public function deleteMultiple( $keys )
71
	{
72
		foreach( $keys as $key ) {
73
			$this->object->forget( $key );
74
		}
75
	}
76
77
78
	/**
79
	 * Removes the cache entries identified by the given tags.
80
	 *
81
	 * @inheritDoc
82
	 *
83
	 * @param string[] $tags List of tag strings that are associated to one or more
84
	 * 	cache entries that should be removed
85
	 */
86
	public function deleteByTags( array $tags )
87
	{
88
		// $this->object->tags( $tag )->flush();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
		$this->object->flush();
90
	}
91
92
93
	/**
94
	 * Returns the value of the requested cache key.
95
	 *
96
	 * @inheritDoc
97
	 *
98
	 * @param string $name Path to the requested value like tree/node/classname
99
	 * @param string $default Value returned if requested key isn't found
100
	 * @return mixed Value associated to the requested key
101
	 */
102
	public function get( $name, $default = null )
103
	{
104
		if( ( $entry = $this->object->get( $name ) ) !== null ) {
105
			return $entry;
106
		}
107
108
		return $default;
109
	}
110
111
112
	/**
113
	 * Returns the cached values for the given cache keys.
114
	 *
115
	 * @inheritDoc
116
	 *
117
	 * @param iterable $keys List of key strings for the requested cache entries
118
	 * @param mixed $default Default value to return for keys that do not exist
119
	 * @return array Associative list of key/value pairs for the requested cache
120
	 * 	entries. If a cache entry doesn't exist, neither its key nor a value
121
	 * 	will be in the result list
122
	 */
123
	public function getMultiple( $keys, $default = null )
124
	{
125
		$result = array();
126
127
		foreach( $keys as $key )
128
		{
129
			if( ( $entry = $this->object->get( $key ) ) !== false ) {
130
				$result[$key] = $entry;
131
			} else {
132
				$result[$key] = $default;
133
			}
134
		}
135
136
		return $result;
137
	}
138
139
140
	/**
141
	 * Returns the cached keys and values associated to the given tags.
142
	 *
143
	 * @inheritDoc
144
	 *
145
	 * @param string[] $tags List of tag strings associated to the requested cache entries
146
	 * @return array Associative list of key/value pairs for the requested cache
147
	 * 	entries. If a tag isn't associated to any cache entry, nothing is returned
148
	 * 	for that tag
149
	 */
150
	public function getMultipleByTags( array $tags )
151
	{
152
		return array();
153
	}
154
155
156
	/**
157
	 * Sets the value for the given key in the cache.
158
	 *
159
	 * @inheritDoc
160
	 *
161
	 * @param string $key Key string for the given value like product/id/123
162
	 * @param mixed $value Value string that should be stored for the given key
163
	 * @param int|string|null $expires Date/time string in "YYYY-MM-DD HH:mm:ss"
164
	 * 	format or as TTL value when the cache entry expires
165
	 * @param array $tags List of tag strings that should be assoicated to the
166
	 * 	given value in the cache
167
	 */
168
	public function set( $key, $value, $expires = null, array $tags = array() )
169
	{
170
		if( $expires !== null && ( $timestamp = strtotime( $expires ) ) !== false ) {
171
			$this->object->put( $key, $value, ($timestamp - time())/60 );
172
		} else {
173
			$this->object->forever( $key, $value );
174
		}
175
	}
176
177
178
	/**
179
	 * Adds or overwrites the given key/value pairs in the cache, which is much
180
	 * more efficient than setting them one by one using the set() method.
181
	 *
182
	 * @inheritDoc
183
	 *
184
	 * @param iterable $pairs Associative list of key/value pairs. Both must be
185
	 * 	a string
186
	 * @param int|string|array $expires Associative list of keys and datetime
187
	 *  string or integer TTL pairs.
188
	 * @param array $tags Associative list of key/tag or key/tags pairs that
189
	 *  should be associated to the values identified by their key. The value
190
	 *  associated to the key can either be a tag string or an array of tag strings
191
	 */
192
	public function setMultiple( $pairs, $expires = null, array $tags = array() )
193
	{
194
		foreach( $pairs as $key => $value )
195
		{
196
			$tagList = ( isset( $tags[$key] ) ? (array) $tags[$key] : array() );
197
			$keyExpire = ( isset( $expires[$key] ) ? $expires[$key] : null );
198
199
			$this->set( $key, $value, $keyExpire, $tagList );
200
		}
201
	}
202
}
203