Passed
Pull Request — master (#37)
by Florian
03:57
created

CacheProvider::provideNullCacheAsSimpleCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tebru\Gson\Internal;
6
7
use Psr\Cache\CacheItemPoolInterface;
8
use Psr\SimpleCache\CacheInterface;
9
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10
use Symfony\Component\Cache\Adapter\ChainAdapter;
11
use Symfony\Component\Cache\Adapter\NullAdapter;
12
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
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
final class CacheProvider
21
{
22
    /**
23
     * @var bool
24
     */
25
    private $symfonyGte43;
26
27 3
    public function __construct()
28
    {
29 3
        $this->symfonyGte43 = class_exists('Symfony\Component\Cache\Psr16Cache');
30 3
    }
31
32
    /**
33
     * @return CacheInterface
34
     */
35 3
    public function provideNullCacheAsSimpleCache()
36
    {
37 3
        if ($this->symfonyGte43) {
38 3
            return new Psr16Cache(new NullAdapter());
39
        }
40
41
        return new NullCache();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\NullCache has been deprecated: since Symfony 4.3, use NullAdapter 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

41
        return /** @scrutinizer ignore-deprecated */ new NullCache();
Loading history...
42
    }
43
44
    /**
45
     * @param int $defaultLifetime
46
     * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
47
     * @return CacheInterface
48
     */
49 3
    public function provideArrayCacheAsSimpleCache(int $defaultLifetime = 0, bool $storeSerialized = true)
50
    {
51 3
        if ($this->symfonyGte43) {
52 3
            return new Psr16Cache(new ArrayAdapter($defaultLifetime, $storeSerialized));
53
        }
54
55
        return new ArrayCache($defaultLifetime, $storeSerialized);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\ArrayCache has been deprecated: since Symfony 4.3, use ArrayAdapter 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

55
        return /** @scrutinizer ignore-deprecated */ new ArrayCache($defaultLifetime, $storeSerialized);
Loading history...
56
    }
57
58
    /**
59
     * @param int $defaultLifetime
60
     * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
61
     * @return CacheItemPoolInterface|CacheInterface
62
     */
63
    public function providerArrayCacheAsCacheItemPool(int $defaultLifetime = 0, bool $storeSerialized = true)
64
    {
65
        if ($this->symfonyGte43) {
66
            return new ArrayAdapter($defaultLifetime, $storeSerialized);
67
        }
68
69
        return new ArrayCache($defaultLifetime, $storeSerialized);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\ArrayCache has been deprecated: since Symfony 4.3, use ArrayAdapter 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

69
        return /** @scrutinizer ignore-deprecated */ new ArrayCache($defaultLifetime, $storeSerialized);
Loading history...
70
    }
71
72
    /**
73
     * @param CacheItemPoolInterface[] $caches The ordered list of caches used to fetch cached items
74
     * @param int $defaultLifetime The lifetime of items propagated from lower caches to upper ones
75
     * @return CacheInterface
76
     */
77
    public function provideChainCacheAsSimpleCache(array $caches, int $defaultLifetime = 0)
78
    {
79
        if ($this->symfonyGte43) {
80
            return new Psr16Cache(new ChainAdapter($caches, $defaultLifetime));
81
        }
82
83
        return new ChainCache($caches, $defaultLifetime);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\ChainCache has been deprecated: since Symfony 4.3, use ChainAdapter 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

83
        return /** @scrutinizer ignore-deprecated */ new ChainCache($caches, $defaultLifetime);
Loading history...
84
    }
85
86
//    /**
87
//     * @param string $namespace
88
//     * @param int $defaultLifetime
89
//     * @param string|null $directory
90
//     * @param bool $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire.
91
//     *                    Doing so is encouraged because it fits perfectly OPcache's memory model.
92
//     *
93
//     * @return CacheInterface
94
//     * @throws CacheException
95
//     */
96
//    public function providePhpFilesCacheAsSimpleCache(string $namespace = '', int $defaultLifetime = 0, string $directory = null, bool $appendOnly = false)
97
//    {
98
//        if ($this->symfonyGte43) {
99
//            return new Psr16Cache(new PhpFilesAdapter($namespace, $defaultLifetime, $directory, $appendOnly));
100
//        }
101
//
102
//        return new PhpFilesCache($namespace, $defaultLifetime, $directory, $appendOnly);
103
//    }
104
105
    /**
106
     * @param string $namespace
107
     * @param int $defaultLifetime
108
     * @param string|null $directory
109
     * @param bool $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire.
110
     *                    Doing so is encouraged because it fits perfectly OPcache's memory model.
111
     *
112
     * @return CacheItemPoolInterface|CacheInterface
113
     * @throws CacheException
114
     */
115
    public function providePhpFilesCacheAsCacheItemPool(string $namespace = '', int $defaultLifetime = 0, string $directory = null, bool $appendOnly = false)
116
    {
117
        if ($this->symfonyGte43) {
118
            return new PhpFilesAdapter($namespace, $defaultLifetime, $directory, $appendOnly);
119
        }
120
121
        return new PhpFilesCache($namespace, $defaultLifetime, $directory, $appendOnly);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\PhpFilesCache has been deprecated: since Symfony 4.3, use PhpFilesAdapter 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

121
        return /** @scrutinizer ignore-deprecated */ new PhpFilesCache($namespace, $defaultLifetime, $directory, $appendOnly);
Loading history...
122
    }
123
}
124