Completed
Push — 2.0-dev ( 490317...2dd5a8 )
by Michael
02:21
created

Apc   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 146
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 78.26%

Importance

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

7 Methods

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

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