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

CacheAwareTrait::getCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
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
}