Completed
Push — master ( caf222...deba87 )
by Jeroen
72:32 queued 44:47
created

StashWrapper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
Duplicated Lines 21.88 %

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
dl 14
loc 64
rs 10
c 0
b 0
f 0
ccs 26
cts 27
cp 0.963
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 22 5
A createEphemeral() 0 2 1
A invalidate() 6 6 3
A __construct() 0 2 1
A put() 6 6 3

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
namespace Elgg\Cache\Pool;
3
4
use Elgg\Cache\Pool;
5
use Stash;
6
use InvalidArgumentException;
7
8
/**
9
 * Defers to Stash for the meat of the caching logic.
10
 *
11
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
12
 *
13
 * @since 1.10.0
14
 *
15
 * @access private
16
 */
17
final class StashWrapper implements Pool {
18
19
	/** @var Stash\Pool */
20
	private $stash;
21
22
	/**
23
	 * Constructor
24
	 *
25
	 * @param Stash\Pool $stash The Stash instance to be decorated.
26
	 */
27 16
	public function __construct(Stash\Pool $stash) {
28 16
		$this->stash = $stash;
29 16
	}
30
31
	/** @inheritDoc */
32 8
	public function get($key, callable $callback = null, $default = null) {
33 8
		if (!is_string($key) && !is_int($key)) {
34 4
			throw new InvalidArgumentException('key must be string or integer');
35
		}
36
37 4
		$item = $this->stash->getItem((string) $key);
38
39 4
		$result = $item->get();
40
41 4
		if ($item->isMiss()) {
42 4
			if (!$callback) {
43
				return $default;
44
			}
45
46 4
			$item->lock();
47
48 4
			$result = call_user_func($callback);
49
50 4
			$this->stash->save($item->set($result));
51
		}
52
53 4
		return $result;
54
	}
55
56
	/** @inheritDoc */
57 6 View Code Duplication
	public function invalidate($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...
58 6
		if (!is_string($key) && !is_int($key)) {
59 4
			throw new InvalidArgumentException('key must be string or integer');
60
		}
61
		
62 2
		$this->stash->getItem((string) $key)->clear();
63 2
	}
64
	
65
	/** @inheritDoc */
66 5 View Code Duplication
	public function put($key, $value) {
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...
67 5
		if (!is_string($key) && !is_int($key)) {
68 4
			throw new InvalidArgumentException('key must be string or integer');
69
		}
70
71 1
		$this->stash->getItem((string) $key)->set($value);
72 1
	}
73
	
74
	/**
75
	 * Create an in-memory implementation of the pool.
76
	 *
77
	 * @return StashWrapper
78
	 */
79 16
	public static function createEphemeral() {
80 16
		return new self(new Stash\Pool(new Stash\Driver\Ephemeral()));
81
	}
82
}
83