Passed
Push — master ( 881b2a...05d288 )
by Hong
02:47
created

CacheAwareTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setCache() 0 4 1
A getCache() 0 6 2
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Cache
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Cache;
13
14
use Psr\SimpleCache\CacheInterface;
15
use Phoole\Cache\Exception\LogicException;
16
17
/**
18
 * CacheAwareTrait
19
 *
20
 * @package Phoole\Cache
21
 */
22
trait CacheAwareTrait
23
{
24
    /**
25
     * @var CacheInterface
26
     */
27
    protected $cache;
28
29
    /**
30
     * @param  CacheInterface $cache
31
     * @return $this
32
     */
33
    public function setCache(CacheInterface $cache)
34
    {
35
        $this->cache = $cache;
36
        return $this;
37
    }
38
39
    /**
40
     * @return CacheInterface
41
     * @throw  LogicException
42
     */
43
    public function getCache(): CacheInterface
44
    {
45
        if (\is_null($this->cache)) {
46
            throw new LogicException("cache not initialized");
47
        }
48
        return $this->cache;
49
    }
50
}