InteractsWithCache::setCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the dingtalk.
4
 * User: Ilham Tahir <[email protected]>
5
 * This source file is subject to the MIT license that is bundled
6
 * with this source code in the file LICENSE.
7
 */
8
9
namespace Aplisin\DingTalk\Kernel\Traits;
10
11
use Aplisin\DingTalk\Kernel\ServiceContainer;
12
use Psr\SimpleCache\CacheInterface;
13
use Symfony\Component\Cache\Simple\FilesystemCache;
14
15
trait InteractsWithCache
16
{
17
    /**
18
     * @var CacheInterface
19
     */
20
    protected $cache;
21
22
    /**
23
     * @return CacheInterface|FilesystemCache
24
     */
25
    public function getCache()
26
    {
27
        if ($this->cache) {
28
            return $this->cache;
29
        }
30
31
        if (property_exists($this, 'app') && $this->app instanceof ServiceContainer
32
            && isset($this->app['cache']) && $this->app['cache'] instanceof CacheInterface) {
33
            return $this->cache = $this->app['cache'];
34
        }
35
36
        return $this->cache = $this->createDefaultCache();
37
    }
38
39
    /**
40
     * @param CacheInterface $cache
41
     * @return $this
42
     */
43
    public function setCache(CacheInterface $cache)
44
    {
45
        $this->cache = $cache;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return FilesystemCache
52
     */
53
    protected function createDefaultCache()
54
    {
55
        return new FilesystemCache();
56
    }
57
}
58