Passed
Push — master ( dadcd3...0d3197 )
by Aimeos
01:58
created

Typo3::cleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2014-2018
7
 * @package MW
8
 * @subpackage Cache
9
 */
10
11
12
namespace Aimeos\MW\Cache;
13
14
15
/**
16
 * TYPO3 caching implementation.
17
 *
18
 * @package MW
19
 * @subpackage Cache
20
 */
21
class Typo3
22
	extends \Aimeos\MW\Cache\Base
23
	implements \Aimeos\MW\Cache\Iface
24
{
25
	private $object;
26
	private $prefix;
27
28
29
	/**
30
	 * Initializes the object instance.
31
	 *
32
	 * @param array $config List of configuration values
33
	 * @param \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache TYPO3 cache object
34
	 */
35
	public function __construct( array $config, \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache )
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Core\Cache\Frontend\FrontendInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
	{
37
		$this->prefix = ( isset( $config['siteid'] ) ? $config['siteid'] . '-' : '' );
38
		$this->object = $cache;
39
	}
40
41
42
	/**
43
	 * Removes all expired cache entries.
44
	 *
45
	 * @inheritDoc
46
	 *
47
	 * @return bool True on success and false on failure
48
	 */
49
	public function cleanup() : bool
50
	{
51
		$this->object->collectGarbage();
52
		return true;
53
	}
54
55
56
	/**
57
	 * Removes all entries for the current site from the cache.
58
	 *
59
	 * @inheritDoc
60
	 *
61
	 * @return bool True on success and false on failure
62
	 */
63
	public function clear() : bool
64
	{
65
		if( $this->prefix ) {
66
			$this->object->flushByTag( $this->prefix . 'siteid' );
67
		} else {
68
			$this->object->flush();
69
		}
70
71
		return true;
72
	}
73
74
75
	/**
76
	 * Removes the cache entry identified by the given key.
77
	 *
78
	 * @inheritDoc
79
	 *
80
	 * @param string $key Key string that identifies the single cache entry
81
	 * @return bool True if the item was successfully removed. False if there was an error
82
	 * @throws \Psr\SimpleCache\InvalidArgumentException
83
	 */
84
	public function delete( string $key ) : bool
85
	{
86
		$this->object->remove( $this->prefix . $key );
87
		return true;
88
	}
89
90
91
	/**
92
	 * Removes the cache entries identified by the given keys.
93
	 *
94
	 * @inheritDoc
95
	 *
96
	 * @param iterable $keys List of key strings that identify the cache entries that should be removed
97
	 * @return bool True if the items were successfully removed. False if there was an error.
98
	 * @throws \Psr\SimpleCache\InvalidArgumentException
99
	 */
100
	public function deleteMultiple( iterable $keys ) : bool
101
	{
102
		foreach( $keys as $key ) {
103
			$this->object->remove( $this->prefix . $key );
104
		}
105
106
		return true;
107
	}
108
109
110
	/**
111
	 * Removes the cache entries identified by the given tags.
112
	 *
113
	 * @inheritDoc
114
	 *
115
	 * @param iterable $tags List of tag strings that are associated to one or
116
	 *  more cache entries that should be removed
117
	 * @return bool True if the items were successfully removed. False if there was an error.
118
	 * @throws \Psr\SimpleCache\InvalidArgumentException
119
	 */
120
	public function deleteByTags( iterable $tags ) : bool
121
	{
122
		foreach( $tags as $tag ) {
123
			$this->object->flushByTag( $this->prefix . $tag );
124
		}
125
126
		return true;
127
	}
128
129
130
	/**
131
	 * Returns the value of the requested cache key.
132
	 *
133
	 * @inheritDoc
134
	 *
135
	 * @param string $key Path to the requested value like product/id/123
136
	 * @param mixed $default Value returned if requested key isn't found
137
	 * @return mixed Value associated to the requested key. If no value for the
138
	 *	key is found in the cache, the given default value is returned
139
	 * @throws \Psr\SimpleCache\InvalidArgumentException
140
	 */
141
	public function get( string $key, $default = null )
142
	{
143
		if( ( $entry = $this->object->get( $this->prefix . $key ) ) !== false ) {
144
			return $entry;
145
		}
146
147
		return $default;
148
	}
149
150
151
	/**
152
	 * Returns the cached values for the given cache keys.
153
	 *
154
	 * @inheritDoc
155
	 *
156
	 * @param iterable $keys List of key strings for the requested cache entries
157
	 * @param mixed $default Default value to return for keys that do not exist
158
	 * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
159
	 * @throws \Psr\SimpleCache\InvalidArgumentException
160
	 */
161
	public function getMultiple( iterable $keys, $default = null ) : iterable
162
	{
163
		$result = [];
164
165
		foreach( $keys as $key )
166
		{
167
			if( ( $entry = $this->object->get( $this->prefix . $key ) ) !== false ) {
168
				$result[$key] = $entry;
169
			} else {
170
				$result[$key] = $default;
171
			}
172
		}
173
174
		return $result;
175
	}
176
177
178
	/**
179
	 * Determines whether an item is present in the cache.
180
	 *
181
	 * @inheritDoc
182
	 *
183
	 * @param string $key The cache item key
184
	 * @return bool True if cache entry is available, false if not
185
	 * @throws \Psr\SimpleCache\InvalidArgumentException
186
	 */
187
	public function has( string $key ) : bool
188
	{
189
		return $this->object->has( $this->prefix . $key );
190
	}
191
192
193
	/**
194
	 * Sets the value for the given key in the cache.
195
	 *
196
	 * @inheritDoc
197
	 *
198
	 * @param string $key Key string for the given value like product/id/123
199
	 * @param mixed $value Value string that should be stored for the given key
200
	 * @param \DateInterval|int|string|null $expires Date interval object,
201
	 *  date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value
202
	 *  when the cache entry will expiry
203
	 * @param iterable $tags List of tag strings that should be assoicated to the cache entry
204
	 * @return bool True on success and false on failure.
205
	 * @throws \Psr\SimpleCache\InvalidArgumentException
206
	 */
207
	public function set( string $key, $value, $expires = null, iterable $tags = [] ) : bool
208
	{
209
		if( $expires instanceof \DateInterval ) {
210
			$expires = date_create()->add( $expires )->getTimestamp() - time();
211
		} elseif( is_string( $expires ) ) {
212
			$expires = date_create( $expires )->getTimestamp() - time();
213
		}
214
215
		$tagList = ( $this->prefix ? array( $this->prefix . 'siteid' ) : [] );
216
217
		foreach( $tags as $tag ) {
218
			$tagList[] = $this->prefix . $tag;
219
		}
220
221
		$this->object->set( $this->prefix . $key, $value, $tagList, $expires );
222
		return true;
223
	}
224
225
226
	/**
227
	 * Adds or overwrites the given key/value pairs in the cache, which is much
228
	 * more efficient than setting them one by one using the set() method.
229
	 *
230
	 * @inheritDoc
231
	 *
232
	 * @param iterable $pairs Associative list of key/value pairs. Both must be a string
233
	 * @param \DateInterval|int|string|null $expires Date interval object,
234
	 *  date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value
235
	 *  when the cache entry will expiry
236
	 * @param iterable $tags List of tags that should be associated to the cache entries
237
	 * @return bool True on success and false on failure.
238
	 * @throws \Psr\SimpleCache\InvalidArgumentException
239
	 */
240
	public function setMultiple( iterable $pairs, $expires = null, iterable $tags = [] ) : bool
241
	{
242
		foreach( $pairs as $key => $value ) {
243
			$this->set( $key, $value, $expires, $tags );
244
		}
245
246
		return true;
247
	}
248
}
249