Passed
Push — master ( fdf14f...ac9e25 )
by Carlos
04:33
created

InteractsWithCache::isSymfony43()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Traits;
13
14
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
15
use EasyWeChat\Kernel\ServiceContainer;
16
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
17
use Symfony\Component\Cache\Adapter\AbstractAdapter;
18
use Symfony\Component\Cache\Psr16Cache;
19
use \Psr\Cache\CacheItemPoolInterface;
20
use Symfony\Component\Cache\Simple\FilesystemCache;
21
22
/**
23
 * Trait InteractsWithCache.
24
 *
25
 * @author overtrue <[email protected]>
26
 */
27
trait InteractsWithCache
28
{
29
    /**
30
     * @var \Psr\SimpleCache\CacheInterface
31
     */
32
    protected $cache;
33
34
    /**
35
     * Get cache instance.
36
     *
37
     * @return \Psr\SimpleCache\CacheInterface
38
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
39
     */
40 3
    public function getCache()
41
    {
42 3
        if ($this->cache) {
43 2
            return $this->cache;
44
        }
45
46 3
        if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) {
47 1
            $this->setCache($this->app['cache']);
48 1
            return $this->cache;
49
        }
50
51 3
        return $this->cache = $this->createDefaultCache();
52
    }
53
54
    /**
55
     * Set cache instance.
56
     *
57
     * @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache
58
     *
59
     * @return $this
60
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
61
     */
62 2
    public function setCache($cache)
63
    {
64 2
        if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) {
65
            throw new InvalidArgumentException(
66
                \sprintf('The cache instance must implements %s or %s interface.',
67
                    SimpleCacheInterface::class, CacheItemPoolInterface::class
68
                )
69
            );
70
        }
71
72 2
        if ($cache instanceof CacheItemPoolInterface) {
73
            if (!$this->isSymfony43()) {
74
                throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
75
            }
76
            $cache = new Psr16Cache($cache);
77
        }
78
79 2
        $this->cache = $cache;
80
81 2
        return $this;
82
    }
83
84
    /**
85
     * @return \Psr\SimpleCache\CacheInterface
86
     */
87 3
    protected function createDefaultCache()
88
    {
89 3
        if ($this->isSymfony43()) {
90
            return new Psr16Cache(AbstractAdapter::createSystemCache('easywechat', 2700, '1.0', \sys_get_temp_dir()));
91
        }
92
93 3
        return new FilesystemCache();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\FilesystemCache has been deprecated: since Symfony 4.3, use FilesystemAdapter and type-hint for CacheInterface instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

93
        return /** @scrutinizer ignore-deprecated */ new FilesystemCache();
Loading history...
94
    }
95
96
    /**
97
     * @return bool
98
     */
99 3
    protected function isSymfony43(): bool
100
    {
101 3
        return \class_exists('Symfony\Component\Cache\Psr16Cache');
102
    }
103
}
104