Passed
Pull Request — master (#85)
by Florian
06:14
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\Psr16Cache;
14
use Symfony\Component\Cache\Simple\ArrayCache;
15
use Symfony\Component\Cache\Simple\ChainCache;
16
use Symfony\Component\Cache\Simple\NullCache;
17
use Symfony\Component\Cache\Simple\PhpFilesCache;
18
19
/**
20
 * @codeCoverageIgnore
21
 */
22
final class CacheProvider
23
{
24
    public static function createFileCache(string $cacheDir): CacheInterface
25
    {
26
        // >= Symfony 4.3
27
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
28
            return new Psr16Cache(new ChainAdapter([
29
                new Psr16Adapter(self::createMemoryCache()),
30
                new PhpFilesAdapter('', 0, $cacheDir),
31
            ]));
32
        }
33
34
        return new ChainCache([
35
            self::createMemoryCache(),
36
            new PhpFilesCache('', 0, $cacheDir)
37
        ]);
38
    }
39
40
    /**
41
     * Create a "memory cache" depending on symfony/cache version
42
     * @return CacheInterface
43
     */
44
    public static function createMemoryCache(): CacheInterface
45
    {
46
        // >= Symfony 4.3
47
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
48
            return new Psr16Cache(new ArrayAdapter(0, false));
49
        }
50
51
        return new ArrayCache(0, false);
52
    }
53
54
    /**
55
     * Create a "null" cache (for annotations) depending on symfony/cache version
56
     *
57
     * @return CacheInterface
58
     */
59
    public static function createNullCache(): CacheInterface
60
    {
61
        // >= Symfony 4.3
62
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
63
            return new Psr16Cache(new NullAdapter());
64
        }
65
66
        return new NullCache();
67
    }
68
}
69