Passed
Push — master ( b682e4...9aa869 )
by Carlos
09:31 queued 11s
created

InteractsWithCache::isSymfony43OrHigher()   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\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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Cache\Simple\FilesystemCache was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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(\sprintf('The cache instance must implements %s or %s interface.', SimpleCacheInterface::class, CacheItemPoolInterface::class));
72
        }
73
74 2
        if ($cache instanceof CacheItemPoolInterface) {
75 2
            if (!$this->isSymfony43OrHigher()) {
76
                throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
77
            }
78 2
            $cache = new Psr16Cache($cache);
79
        }
80
81 2
        $this->cache = $cache;
82
83 2
        return $this;
84
    }
85
86
    /**
87
     * @return \Psr\SimpleCache\CacheInterface
88
     */
89 3
    protected function createDefaultCache()
90
    {
91 3
        if ($this->isSymfony43OrHigher()) {
92 3
            return new Psr16Cache(new FilesystemAdapter('easywechat', 1500));
93
        }
94
95
        return new FilesystemCache();
96
    }
97
98
    /**
99
     * @return bool
100
     */
101 3
    protected function isSymfony43OrHigher(): bool
102
    {
103 3
        return \class_exists('Symfony\Component\Cache\Psr16Cache');
104
    }
105
}
106