Apcu::hasItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Part of the Joomla Framework Cache Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE
7
 */
8
9
namespace Joomla\Cache\Adapter;
10
11
use Joomla\Cache\AbstractCacheItemPool;
12
use Joomla\Cache\Item\HasExpirationDateInterface;
13
use Joomla\Cache\Item\Item;
14
use Psr\Cache\CacheItemInterface;
15
16
/**
17
 * APCu cache driver for the Joomla Framework.
18
 *
19
 * @since       __DEPLOY_VERSION__
20
 * @deprecated  The joomla/cache package is deprecated
21
 */
22
class Apcu extends AbstractCacheItemPool
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Cache\AbstractCacheItemPool has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
23
{
24
	/**
25
	 * This will wipe out the entire cache's keys
26
	 *
27
	 * @return  boolean  True if the pool was successfully cleared. False if there was an error.
28
	 *
29
	 * @since   __DEPLOY_VERSION__
30
	 */
31
	public function clear()
32
	{
33
		return apcu_clear_cache();
34
	}
35
36
	/**
37
	 * Returns a Cache Item representing the specified key.
38
	 *
39
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
40
	 *
41
	 * @return  CacheItemInterface  The corresponding Cache Item.
42
	 *
43
	 * @since   __DEPLOY_VERSION__
44
	 */
45 View Code Duplication
	public function getItem($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
	{
47
		$success = false;
48
		$value = apcu_fetch($key, $success);
49
		$item = new Item($key);
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Cache\Item\Item has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
50
51
		if ($success)
52
		{
53
			$item->set($value);
54
		}
55
56
		return $item;
57
	}
58
59
	/**
60
	 * Returns a traversable set of cache items.
61
	 *
62
	 * @param   string[]  $keys  An indexed array of keys of items to retrieve.
63
	 *
64
	 * @return  array  A traversable collection of Cache Items keyed by the cache keys of each item.
65
	 *                 A Cache item will be returned for each key, even if that key is not found.
66
	 *
67
	 * @since   __DEPLOY_VERSION__
68
	 */
69
	public function getItems(array $keys = [])
70
	{
71
		$items   = [];
72
		$success = false;
73
		$values  = apcu_fetch($keys, $success);
74
75
		if ($success && is_array($values))
76
		{
77
			foreach ($keys as $key)
78
			{
79
				$items[$key] = new Item($key);
0 ignored issues
show
Deprecated Code introduced by
The class Joomla\Cache\Item\Item has been deprecated with message: The joomla/cache package is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
80
81
				if (isset($values[$key]))
82
				{
83
					$items[$key]->set($values[$key]);
84
				}
85
			}
86
		}
87
88
		return $items;
89
	}
90
91
	/**
92
	 * Removes the item from the pool.
93
	 *
94
	 * @param   string  $key  The key to delete.
95
	 *
96
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
97
	 *
98
	 * @since   __DEPLOY_VERSION__
99
	 */
100
	public function deleteItem($key)
101
	{
102
		if ($this->hasItem($key))
103
		{
104
			return apcu_delete($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_delete($key); of type boolean|string[] adds the type string[] to the return on line 104 which is incompatible with the return type declared by the interface Psr\Cache\CacheItemPoolInterface::deleteItem of type boolean.
Loading history...
105
		}
106
107
		// If the item doesn't exist, no error
108
		return true;
109
	}
110
111
	/**
112
	 * Persists a cache item immediately.
113
	 *
114
	 * @param   CacheItemInterface  $item  The cache item to save.
115
	 *
116
	 * @return  boolean  True if the item was successfully persisted. False if there was an error.
117
	 *
118
	 * @since   __DEPLOY_VERSION__
119
	 */
120 View Code Duplication
	public function save(CacheItemInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
121
	{
122
		// If we are able to find out when the item expires - find out. Else bail.
123
		if ($item instanceof HasExpirationDateInterface)
124
		{
125
			$ttl = $this->convertItemExpiryToSeconds($item);
126
		}
127
		else
128
		{
129
			$ttl = 0;
130
		}
131
132
		return apcu_store($item->getKey(), $item->get(), $ttl);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_store($item->getKey(), $item->get(), $ttl); of type boolean|array adds the type array to the return on line 132 which is incompatible with the return type declared by the interface Psr\Cache\CacheItemPoolInterface::save of type boolean.
Loading history...
133
	}
134
135
	/**
136
	 * Confirms if the cache contains specified cache item.
137
	 *
138
	 * @param   string  $key  The key for which to check existence.
139
	 *
140
	 * @return  boolean  True if item exists in the cache, false otherwise.
141
	 *
142
	 * @since   __DEPLOY_VERSION__
143
	 */
144
	public function hasItem($key)
145
	{
146
		return apcu_exists($key);
0 ignored issues
show
Bug Compatibility introduced by
The expression apcu_exists($key); of type boolean|string[] adds the type string[] to the return on line 146 which is incompatible with the return type declared by the interface Psr\Cache\CacheItemPoolInterface::hasItem of type boolean.
Loading history...
147
	}
148
149
	/**
150
	 * Test to see if the CacheItemPoolInterface is available
151
	 *
152
	 * @return  boolean  True on success, false otherwise
153
	 *
154
	 * @since   __DEPLOY_VERSION__
155
	 */
156
	public static function isSupported(): bool
157
	{
158
		$supported = extension_loaded('apcu') && ini_get('apc.enabled');
159
160
		// If on the CLI interface, the `apc.enable_cli` option must also be enabled
161
		if ($supported && php_sapi_name() === 'cli')
162
		{
163
			$supported = ini_get('apc.enable_cli');
164
		}
165
166
		return (bool) $supported;
167
	}
168
}
169