Passed
Pull Request — master (#85)
by Florian
01:59
created

CacheProvider::createMemoryCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tebru\Retrofit\Internal;
6
7
use Psr\SimpleCache\CacheInterface;
8
use Symfony\Component\Cache\Adapter\ArrayAdapter;
9
use Symfony\Component\Cache\Adapter\ChainAdapter;
10
use Symfony\Component\Cache\Adapter\NullAdapter;
11
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
12
use Symfony\Component\Cache\Adapter\Psr16Adapter;
13
use Symfony\Component\Cache\Exception\CacheException;
14
use Symfony\Component\Cache\Psr16Cache;
15
use Symfony\Component\Cache\Simple\ArrayCache;
16
use Symfony\Component\Cache\Simple\ChainCache;
17
use Symfony\Component\Cache\Simple\NullCache;
18
use Symfony\Component\Cache\Simple\PhpFilesCache;
19
20
/**
21
 * @codeCoverageIgnore
22
 */
23
final class CacheProvider
24
{
25
    /**
26
     * Create a "file cache", chained to a "memory cache" depending on symfony/cache version
27
     *
28
     * @param string $cacheDir
29
     * @return CacheInterface
30
     * @throws CacheException
31
     */
32
    public static function createFileCache(string $cacheDir): CacheInterface
33
    {
34
        // >= Symfony 4.3
35
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
36
            return new Psr16Cache(new ChainAdapter([
37
                new Psr16Adapter(self::createMemoryCache()),
38
                new PhpFilesAdapter('', 0, $cacheDir),
39
            ]));
40
        }
41
42
        return new ChainCache([
43
            self::createMemoryCache(),
44
            new PhpFilesCache('', 0, $cacheDir)
45
        ]);
46
    }
47
48
    /**
49
     * Create a "memory cache" depending on symfony/cache version
50
     * @return CacheInterface
51
     */
52
    public static function createMemoryCache(): CacheInterface
53
    {
54
        // >= Symfony 4.3
55
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
56
            return new Psr16Cache(new ArrayAdapter(0, false));
57
        }
58
59
        return new ArrayCache(0, false);
60
    }
61
62
    /**
63
     * Create a "null" cache (for annotations) depending on symfony/cache version
64
     *
65
     * @return CacheInterface
66
     */
67
    public static function createNullCache(): CacheInterface
68
    {
69
        // >= Symfony 4.3
70
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
71
            return new Psr16Cache(new NullAdapter());
72
        }
73
74
        return new NullCache();
75
    }
76
}
77