Completed
Push — 2.0-dev ( 574eeb...e9e84c )
by George
12s
created

Apc   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 147
Duplicated Lines 99.32 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 3
dl 146
loc 147
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 4 4 1
B getItems() 21 21 5
A deleteItem() 10 10 2
A getItem() 13 13 2
A hasItem() 4 4 1
A isSupported() 12 12 4
A save() 14 14 2

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\Item\HasExpirationDateInterface;
13
use Joomla\Cache\Item\Item;
14
use Psr\Cache\CacheItemInterface;
15
16
/**
17
 * APC cache driver for the Joomla Framework.
18
 *
19
 * @since  1.0
20
 */
21 View Code Duplication
class Apc extends AbstractCacheItemPool
0 ignored issues
show
Duplication introduced by
This class 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...
22
{
23
	/**
24
	 * This will wipe out the entire cache's keys
25
	 *
26
	 * @return  boolean  True if the pool was successfully cleared. False if there was an error.
27
	 *
28
	 * @since   1.0
29
	 */
30 18
	public function clear()
31
	{
32 18
		return apc_clear_cache('user');
33
	}
34
35
	/**
36
	 * Returns a Cache Item representing the specified key.
37
	 *
38
	 * @param   string  $key  The key for which to return the corresponding Cache Item.
39
	 *
40
	 * @return  CacheItemInterface  The corresponding Cache Item.
41
	 *
42
	 * @since   __DEPLOY_VERSION__
43
	 */
44 9
	public function getItem($key)
45
	{
46 9
		$success = false;
47 9
		$value = apc_fetch($key, $success);
48 9
		$item = new Item($key);
49
50
		if ($success)
51 9
		{
52 7
			$item->set($value);
53 7
		}
54
55 9
		return $item;
56
	}
57
58
	/**
59
	 * Returns a traversable set of cache items.
60
	 *
61
	 * @param   string[]  $keys  An indexed array of keys of items to retrieve.
62
	 *
63
	 * @return  array  A traversable collection of Cache Items keyed by the cache keys of each item.
64
	 *                 A Cache item will be returned for each key, even if that key is not found.
65
	 *
66
	 * @since   __DEPLOY_VERSION__
67
	 */
68 4
	public function getItems(array $keys = [])
69
	{
70 4
		$items   = [];
71 4
		$success = false;
72 4
		$values  = apc_fetch($keys, $success);
73
74 4
		if ($success && is_array($values))
75 4
		{
76 4
			foreach ($keys as $key)
77
			{
78 4
				$items[$key] = new Item($key);
79
80 4
				if (isset($values[$key]))
81 4
				{
82 2
					$items[$key]->set($values[$key]);
83 2
				}
84 4
			}
85 4
		}
86
87 4
		return $items;
88
	}
89
90
	/**
91
	 * Removes the item from the pool.
92
	 *
93
	 * @param   string  $key  The key to delete.
94
	 *
95
	 * @return  boolean  True if the item was successfully removed. False if there was an error.
96
	 *
97
	 * @since   __DEPLOY_VERSION__
98
	 */
99 4
	public function deleteItem($key)
100
	{
101 4
		if ($this->hasItem($key))
102 4
		{
103 4
			return apc_delete($key);
104
		}
105
106
		// If the item doesn't exist, no error
107 4
		return true;
108
	}
109
110
	/**
111
	 * Persists a cache item immediately.
112
	 *
113
	 * @param   CacheItemInterface  $item  The cache item to save.
114
	 *
115
	 * @return  boolean  True if the item was successfully persisted. False if there was an error.
116
	 *
117
	 * @since   __DEPLOY_VERSION__
118
	 */
119 15
	public function save(CacheItemInterface $item)
120
	{
121
		// If we are able to find out when the item expires - find out. Else bail.
122 15
		if ($item instanceof HasExpirationDateInterface)
123 15
		{
124 7
			$ttl = $this->convertItemExpiryToSeconds($item);
125 7
		}
126
		else
127
		{
128 8
			$ttl = 0;
129
		}
130
131 15
		return apc_store($item->getKey(), $item->get(), $ttl);
132
	}
133
134
	/**
135
	 * Confirms if the cache contains specified cache item.
136
	 *
137
	 * @param   string  $key  The key for which to check existence.
138
	 *
139
	 * @return  boolean  True if item exists in the cache, false otherwise.
140
	 *
141
	 * @since   1.0
142
	 */
143 8
	public function hasItem($key)
144
	{
145 8
		return apc_exists($key);
146
	}
147
148
	/**
149
	 * Test to see if the CacheItemPoolInterface is available
150
	 *
151
	 * @return  boolean  True on success, false otherwise
152
	 *
153
	 * @since   __DEPLOY_VERSION__
154
	 */
155 18
	public static function isSupported()
156
	{
157 18
		$supported = extension_loaded('apc') && ini_get('apc.enabled');
158
159
		// If on the CLI interface, the `apc.enable_cli` option must also be enabled
160 18
		if ($supported && php_sapi_name() === 'cli')
161 18
		{
162 18
			$supported = ini_get('apc.enable_cli');
163 18
		}
164
165 18
		return (bool) $supported;
166
	}
167
}
168