Issues (31)

src/Kernel/Traits/InteractsWithCache.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace EasyIM\Kernel\Traits;
4
5
use EasyIM\Kernel\Exceptions\InvalidArgumentException;
6
use EasyIM\Kernel\ServiceContainer;
7
use Psr\Cache\CacheItemPoolInterface;
8
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
9
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
10
use Symfony\Component\Cache\Psr16Cache;
11
use Symfony\Component\Cache\Simple\FilesystemCache;
0 ignored issues
show
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...
12
13
/**
14
 * Trait InteractsWithCache.
15
 */
16
trait InteractsWithCache
17
{
18
    /**
19
     * @var \Psr\SimpleCache\CacheInterface
20
     */
21
    protected $cache;
22
23
    /**
24
     * Get cache instance.
25
     *
26
     * @return \Psr\SimpleCache\CacheInterface
27
     *
28
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
29
     */
30 2
    public function getCache()
31
    {
32 2
        if ($this->cache) {
33 1
            return $this->cache;
34
        }
35
36 2
        if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) {
37 1
            $this->setCache($this->app['cache']);
38
39
            // Fix PHPStan error
40 1
            assert($this->cache instanceof \Psr\SimpleCache\CacheInterface);
41
42 1
            return $this->cache;
43
        }
44
45 2
        return $this->cache = $this->createDefaultCache();
46
    }
47
48
    /**
49
     * Set cache instance.
50
     *
51
     * @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache
52
     *
53
     * @return $this
54
     *
55
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
56
     */
57 2
    public function setCache($cache)
58
    {
59 2
        if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) {
60 1
            throw new InvalidArgumentException(\sprintf('The cache instance must implements %s or %s interface.', SimpleCacheInterface::class, CacheItemPoolInterface::class));
61
        }
62
63 2
        if ($cache instanceof CacheItemPoolInterface) {
64 2
            if (!$this->isSymfony43OrHigher()) {
65
                throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
66
            }
67 2
            $cache = new Psr16Cache($cache);
68
        }
69
70 2
        $this->cache = $cache;
71
72 2
        return $this;
73
    }
74
75
    /**
76
     * @return \Psr\SimpleCache\CacheInterface
77
     */
78 2
    protected function createDefaultCache()
79
    {
80 2
        if ($this->isSymfony43OrHigher()) {
81 2
            return new Psr16Cache(new FilesystemAdapter('EasyIM', 1500));
82
        }
83
84
        return new FilesystemCache();
85
    }
86
87
    /**
88
     * @return bool
89
     */
90 2
    protected function isSymfony43OrHigher(): bool
91
    {
92 2
        return \class_exists('Symfony\Component\Cache\Psr16Cache');
93
    }
94
}
95