Completed
Push — 2.0-dev ( 8297cc...96e0ca )
by George
9s
created

Memcached   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.37%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 13
loc 143
ccs 37
cts 38
cp 0.9737
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A clear() 0 4 1
A getItem() 13 13 2
A deleteItem() 0 16 3
A save() 0 15 2
A hasItem() 0 6 1
A isSupported() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Exception\RuntimeException;
13
use Joomla\Cache\Item\HasExpirationDateInterface;
14
use Joomla\Cache\Item\Item;
15
use Psr\Cache\CacheItemInterface;
16
17
/**
18
 * Memcached cache driver for the Joomla Framework.
19
 *
20
 * @since  1.0
21
 */
22
class Memcached extends AbstractCacheItemPool
23
{
24
	/**
25
	 * The Memcached driver
26
	 *
27
	 * @var    \Memcached
28
	 * @since  1.0
29
	 */
30
	private $driver;
31
32
	/**
33
	 * Constructor.
34
	 *
35
	 * @param   \Memcached          $memcached  The Memcached driver being used for this pool
36
	 * @param   array|\ArrayAccess  $options    An options array, or an object that implements \ArrayAccess
37
	 *
38
	 * @since   1.0
39
	 * @throws  \RuntimeException
40
	 */
41 12
	public function __construct(\Memcached $memcached, $options = [])
42
	{
43
		// Parent sets up the caching options and checks their type
44 12
		parent::__construct($options);
45
46 12
		$this->driver = $memcached;
47 12
	}
48
49
	/**
50
	 * This will wipe out the entire cache's keys
51
	 *
52
	 * @return  boolean  The result of the clear operation.
53
	 *
54
	 * @since   1.0
55
	 */
56 12
	public function clear()
57
	{
58 12
		return $this->driver->flush();
59
	}
60
61
	/**
62
	 * Method to get a storage entry value from a key.
63
	 *
64
	 * @param   string  $key  The storage entry identifier.
65
	 *
66
	 * @return  CacheItemInterface
67
	 *
68
	 * @since   1.0
69
	 */
70 5 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...
71
	{
72 5
		$value = $this->driver->get($key);
73 5
		$code = $this->driver->getResultCode();
74 5
		$item = new Item($key);
75
76 5
		if ($code === \Memcached::RES_SUCCESS)
77 5
		{
78 4
			$item->set($value);
79 4
		}
80
81 5
		return $item;
82
	}
83
84
	/**
85
	 * Method to remove a storage entry for a key.
86
	 *
87
	 * @param   string  $key  The storage entry identifier.
88
	 *
89
	 * @return  boolean
90
	 *
91
	 * @since   1.0
92
	 */
93 2
	public function deleteItem($key)
94
	{
95 2
		if ($this->hasItem($key))
96 2
		{
97 2
			$this->driver->delete($key);
98
99 2
			$rc = $this->driver->getResultCode();
100
101 2
			if ( ($rc != \Memcached::RES_SUCCESS))
102 2
			{
103
				throw new RuntimeException(sprintf('Unable to remove cache entry for %s. Error message `%s`.', $key, $this->driver->getResultMessage()));
104
			}
105 2
		}
106
107 2
		return true;
108
	}
109
110
	/**
111
	 * Persists a cache item immediately.
112
	 *
113
	 * @param   CacheItemInterface  $item  The cache item to save.
114
	 *
115
	 * @return  static  The invoked object.
116
	 */
117 9
	public function save(CacheItemInterface $item)
118
	{
119 9
		if ($item instanceof HasExpirationDateInterface)
120 9
		{
121 1
			$ttl = $this->convertItemExpiryToSeconds($item);
122 1
		}
123
		else
124
		{
125 8
			$ttl = 0;
126
		}
127
128 9
		$this->driver->set($item->getKey(), $item->get(), $ttl);
129
130 9
		return (bool) ($this->driver->getResultCode() == \Memcached::RES_SUCCESS);
131
	}
132
133
	/**
134
	 * Method to determine whether a storage entry has been set for a key.
135
	 *
136
	 * @param   string  $key  The storage entry identifier.
137
	 *
138
	 * @return  boolean
139
	 *
140
	 * @since   1.0
141
	 */
142 5
	public function hasItem($key)
143
	{
144 5
		$this->driver->get($key);
145
146 5
		return ($this->driver->getResultCode() != \Memcached::RES_NOTFOUND);
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 12
	public static function isSupported()
157
	{
158
		/*
159
		 * GAE and HHVM have both had instances where Memcached the class was defined but no extension was loaded.
160
		 * If the class is there, we can assume it works.
161
		 */
162 12
		return (class_exists('Memcached'));
163
	}
164
}
165