Completed
Push — 2.0-dev ( 7b1246...f2dca7 )
by George
02:26
created

Apc::save()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

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