Passed
Pull Request — master (#1647)
by
11:06
created

InteractsWithCache::setCache()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
crap 4.0119
rs 9.9332
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\Cache\CacheItemPoolInterface;
17
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
18
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
19
use Symfony\Component\Cache\Psr16Cache;
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
     *
39
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
40
     */
41 3
    public function getCache()
42
    {
43 3
        if ($this->cache) {
44 2
            return $this->cache;
45
        }
46
47 3
        if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) {
48 1
            $this->setCache($this->app['cache']);
49
50
            // Fix PHPStan error
51 1
            assert($this->cache instanceof \Psr\SimpleCache\CacheInterface);
52
53 1
            return $this->cache;
54
        }
55
56 3
        return $this->cache = $this->createDefaultCache();
57
    }
58
59
    /**
60
     * Set cache instance.
61
     *
62
     * @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache
63
     *
64
     * @return $this
65
     *
66
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
67
     */
68 2
    public function setCache($cache)
69
    {
70 2
        if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) {
71 1
            throw new InvalidArgumentException(
72 1
                \sprintf('The cache instance must implements %s or %s interface.',
73 1
                    SimpleCacheInterface::class, CacheItemPoolInterface::class
74
                )
75
            );
76
        }
77
78 2
        if ($cache instanceof CacheItemPoolInterface) {
79 2
            if (!$this->isSymfony43()) {
80
                throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
81
            }
82 2
            $cache = new Psr16Cache($cache);
83
        }
84
85 2
        $this->cache = $cache;
86
87 2
        return $this;
88
    }
89
90
    /**
91
     * @return \Psr\SimpleCache\CacheInterface
92
     */
93 3
    protected function createDefaultCache()
94
    {
95 3
        if ($this->isSymfony43()) {
96 3
            return new Psr16Cache(new FilesystemAdapter('easywechat', 1500));
97
        }
98
99
        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

99
        return /** @scrutinizer ignore-deprecated */ new FilesystemCache();
Loading history...
100
    }
101
102
    /**
103
     * @return bool
104
     */
105 3
    protected function isSymfony43(): bool
106
    {
107 3
        return \class_exists('Symfony\Component\Cache\Psr16Cache');
108
    }
109
}
110