CachePoolAwareTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A cachePool() 0 10 2
A setCachePool() 0 3 1
1
<?php
2
3
namespace Charcoal\Cache;
4
5
use RuntimeException;
6
7
// From PSR-6
8
use Psr\Cache\CacheItemPoolInterface;
9
10
/**
11
 * The Cache Aware Trait provides the methods necessary for an object
12
 * to use a "Cache" service.
13
 */
14
trait CachePoolAwareTrait
15
{
16
    /**
17
     * Store the PSR-6 caching service.
18
     *
19
     * @var CacheItemPoolInterface
20
     */
21
    private $cachePool;
22
23
    /**
24
     * Set the cache pool manager.
25
     *
26
     * @param  CacheItemPoolInterface $cache A PSR-6 compliant cache pool instance.
27
     * @return void
28
     */
29
    protected function setCachePool(CacheItemPoolInterface $cache)
30
    {
31
        $this->cachePool = $cache;
32
    }
33
34
    /**
35
     * Retrieve the cache service.
36
     *
37
     * @throws RuntimeException If the cache service was not previously set.
38
     * @return CacheItemPoolInterface
39
     */
40
    protected function cachePool()
41
    {
42
        if ($this->cachePool === null) {
43
            throw new RuntimeException(sprintf(
44
                'Cache Pool is not defined for "%s"',
45
                get_class($this)
46
            ));
47
        }
48
49
        return $this->cachePool;
50
    }
51
}
52