Skip to content

Commit f06a5a8

Browse files
committed
Moved duplicated code to a trait.
1 parent 3413c21 commit f06a5a8

File tree

3 files changed

+54
-51
lines changed

3 files changed

+54
-51
lines changed

PrefixedCachePool.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@
2121
*/
2222
class PrefixedCachePool implements CacheItemPoolInterface
2323
{
24+
use PrefixedUtilityTrait;
25+
2426
/**
2527
* @type CacheItemPoolInterface
2628
*/
2729
private $cachePool;
2830

29-
/**
30-
* @type string
31-
*/
32-
private $prefix;
33-
3431
/**
3532
* @param CacheItemPoolInterface $cachePool
3633
* @param string $prefix
@@ -41,26 +38,6 @@ public function __construct(CacheItemPoolInterface $cachePool, $prefix)
4138
$this->prefix = $prefix;
4239
}
4340

44-
/**
45-
* Add namespace prefix on the key.
46-
*
47-
* @param array $keys
48-
*/
49-
private function prefixValue(&$key)
50-
{
51-
$key = $this->prefix.$key;
52-
}
53-
54-
/**
55-
* @param array $keys
56-
*/
57-
private function prefixValues(array &$keys)
58-
{
59-
foreach ($keys as &$key) {
60-
$this->prefixValue($key);
61-
}
62-
}
63-
6441
/**
6542
* {@inheritdoc}
6643
*/

PrefixedSimpleCache.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
/*
3+
/**
4+
* @file
45
* This file is part of php-cache organization.
56
*
67
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
@@ -22,16 +23,13 @@
2223
*/
2324
class PrefixedSimpleCache implements CacheInterface
2425
{
26+
use PrefixedUtilityTrait;
27+
2528
/**
2629
* @type CacheInterface
2730
*/
2831
private $cache;
2932

30-
/**
31-
* @type string
32-
*/
33-
private $prefix;
34-
3533
/**
3634
* @param CacheInterface $simpleCache
3735
* @param string $prefix
@@ -42,26 +40,6 @@ public function __construct(CacheInterface $simpleCache, $prefix)
4240
$this->prefix = $prefix;
4341
}
4442

45-
/**
46-
* Add namespace prefix on the key.
47-
*
48-
* @param array $keys
49-
*/
50-
private function prefixValue(&$key)
51-
{
52-
$key = $this->prefix.$key;
53-
}
54-
55-
/**
56-
* @param array $keys
57-
*/
58-
private function prefixValues(array &$keys)
59-
{
60-
foreach ($keys as &$key) {
61-
$this->prefixValue($key);
62-
}
63-
}
64-
6543
/**
6644
* {@inheritdoc}
6745
*/

PrefixedUtilityTrait.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* This file is part of php-cache organization.
6+
*
7+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
8+
*
9+
* This source file is subject to the MIT license that is bundled
10+
* with this source code in the file LICENSE.
11+
*/
12+
13+
namespace Cache\Prefixed;
14+
15+
/**
16+
* Utility to reduce code suplication between the Prefixed* decoratrs.
17+
*
18+
* @author ndobromirov
19+
*/
20+
trait PrefixedUtilityTrait
21+
{
22+
/**
23+
* @type string Prefix to use for key namespaceing.
24+
*/
25+
private $prefix;
26+
27+
/**
28+
* Add namespace prefix on the key.
29+
*
30+
* @param array $keys Reference to the key. It is mutated.
31+
*/
32+
private function prefixValue(&$key)
33+
{
34+
$key = $this->prefix.$key;
35+
}
36+
37+
/**
38+
* Adds a namespace prefix on a list of keys.
39+
*
40+
* @param array $keys Reference to the list of keys. The list is mutated.
41+
*/
42+
private function prefixValues(array &$keys)
43+
{
44+
foreach ($keys as &$key) {
45+
$this->prefixValue($key);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)