Passed
Pull Request — master (#46)
by Florian
03:13
created

CacheProvider::createNullCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tebru\Gson\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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Cache\Simple\ArrayCache 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...
15
use Symfony\Component\Cache\Simple\ChainCache;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Cache\Simple\ChainCache 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...
16
use Symfony\Component\Cache\Simple\NullCache;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Cache\Simple\NullCache 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...
17
use Symfony\Component\Cache\Simple\PhpFilesCache;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Cache\Simple\PhpFilesCache 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...
18
19
/**
20
 * @codeCoverageIgnore
21
 */
22
final class CacheProvider
23
{
24
    /**
25
     * Create a "file cache", chained to a "memory cache" depending on symfony/cache version
26
     *
27
     * @param string $cacheDir
28
     * @return CacheInterface
29
     * @throws \Symfony\Component\Cache\Exception\CacheException
30
     */
31
    public static function createFileCache(string $cacheDir): CacheInterface
32
    {
33
        // >= Symfony 4.3
34
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
35
            return new Psr16Cache(new ChainAdapter([
36
                new Psr16Adapter(self::createMemoryCache()),
37
                new PhpFilesAdapter('', 0, $cacheDir),
38
            ]));
39
        }
40
41
        return new ChainCache([
42
            self::createMemoryCache(),
43
            new PhpFilesCache('', 0, $cacheDir)
44
        ]);
45
    }
46
47
    /**
48
     * Create a "memory cache" depending on symfony/cache version
49
     *
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