Redis::deleteItem()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
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
 * Redis cache driver for the Joomla Framework.
18
 *
19
 * @since       1.0
20
 * @deprecated  The joomla/cache package is deprecated
21
 */
22
class Redis 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
	 * The redis driver.
26
	 *
27
	 * @var    \Redis
28
	 * @since  1.0
29
	 */
30
	protected $driver;
31
32
	/**
33
	 * Constructor.
34
	 *
35
	 * @param   \Redis              $redis    The Redis driver being used for this pool
36
	 * @param   array|\ArrayAccess  $options  An options array, or an object that implements \ArrayAccess
37
	 *
38
	 * @since   __DEPLOY_VERSION__
39
	 */
40 19
	public function __construct(\Redis $redis, $options = [])
41
	{
42
		// Parent sets up the caching options and checks their type
43 19
		parent::__construct($options);
44
45 19
		$this->driver = $redis;
46 19
	}
47
48
	/**
49
	 * This will wipe out the entire cache's keys
50
	 *
51
	 * @return  boolean  True if the pool was successfully cleared. False if there was an error.
52
	 *
53
	 * @since   1.0
54
	 */
55 19
	public function clear()
56
	{
57 19
		return $this->driver->flushDB();
58
	}
59
60
	/**
61
	 * Returns a Cache Item representing the specified key.
62
	 *
63
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
64
	 *
65
	 * @return  CacheItemInterface  The corresponding Cache Item.
66
	 *
67
	 * @since   __DEPLOY_VERSION__
68
	 */
69 12 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...
70
	{
71 12
		$value = $this->driver->get($key);
72 12
		$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...
73
74 12
		if ($value !== false)
75
		{
76 10
			$item->set($value);
77
		}
78
79 12
		return $item;
80
	}
81
82
	/**
83
	 * Removes the item from the pool.
84
	 *
85
	 * @param   string  $key  The key to delete.
86
	 *
87
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
88
	 *
89
	 * @since   __DEPLOY_VERSION__
90
	 */
91 4
	public function deleteItem($key)
92
	{
93 4
		if ($this->hasItem($key))
94
		{
95 4
			return (bool) $this->driver->del($key);
96
		}
97
98
		// If the item doesn't exist, no error
99 4
		return true;
100
	}
101
102
	/**
103
	 * Persists a cache item immediately.
104
	 *
105
	 * @param   CacheItemInterface  $item  The cache item to save.
106
	 *
107
	 * @return  boolean  True if the item was successfully persisted. False if there was an error.
108
	 *
109
	 * @since   __DEPLOY_VERSION__
110
	 */
111 16
	public function save(CacheItemInterface $item)
112
	{
113 16
		if ($item instanceof HasExpirationDateInterface)
114
		{
115 8
			$ttl = $this->convertItemExpiryToSeconds($item);
116
117 8
			if ($ttl > 0)
118
			{
119 7
				return $this->driver->setex($item->getKey(), $ttl, $item->get());
120
			}
121
		}
122
123 9
		return $this->driver->set($item->getKey(), $item->get());
124
	}
125
126
	/**
127
	 * Confirms if the cache contains specified cache item.
128
	 *
129
	 * @param   string  $key  The key for which to check existence.
130
	 *
131
	 * @return  boolean  True if item exists in the cache, false otherwise.
132
	 *
133
	 * @since   1.0
134
	 */
135 8
	public function hasItem($key)
136
	{
137 8
		return $this->driver->exists($key);
138
	}
139
140
	/**
141
	 * Test to see if the CacheItemPoolInterface is available
142
	 *
143
	 * @return  boolean  True on success, false otherwise
144
	 *
145
	 * @since   __DEPLOY_VERSION__
146
	 */
147 19
	public static function isSupported(): bool
148
	{
149 19
		return extension_loaded('redis') && class_exists('Redis');
150
	}
151
}
152