Passed
Pull Request — master (#46)
by Florian
02:53
created

CacheProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
eloc 14
c 3
b 1
f 0
dl 0
loc 53
ccs 6
cts 16
cp 0.375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createMemoryCache() 0 8 2
A createNullCache() 0 8 2
A createFileCache() 0 13 2
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
final class CacheProvider
20
{
21
    /**
22
     * Create a "file cache", chained to a "memory cache" depending on symfony/cache version
23
     *
24
     * @param string $cacheDir
25
     * @return CacheInterface
26
     * @throws \Symfony\Component\Cache\Exception\CacheException
27
     */
28
    public static function createFileCache(string $cacheDir): CacheInterface
29
    {
30
        // >= Symfony 4.3
31
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
32
            return new Psr16Cache(new ChainAdapter([
33
                new Psr16Adapter(self::createMemoryCache()),
34
                new PhpFilesAdapter('', 0, $cacheDir),
35
            ]));
36
        }
37
38
        return new ChainCache([
39
            self::createMemoryCache(),
40
            new PhpFilesCache('', 0, $cacheDir)
41
        ]);
42
    }
43
44
    /**
45
     * Create a "memory cache" depending on symfony/cache version
46
     *
47
     * @return CacheInterface
48
     */
49 3
    public static function createMemoryCache(): CacheInterface
50
    {
51
        // >= Symfony 4.3
52 3
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
53 3
            return new Psr16Cache(new ArrayAdapter(0, false));
54
        }
55
56
        return new ArrayCache(0, false);
57
    }
58
59
    /**
60
     * Create a "null" cache (for annotations) depending on symfony/cache version
61
     *
62
     * @return CacheInterface
63
     */
64 8
    public static function createNullCache(): CacheInterface
65
    {
66
        // >= Symfony 4.3
67 8
        if (class_exists('Symfony\Component\Cache\Psr16Cache')) {
68 8
            return new Psr16Cache(new NullAdapter());
69
        }
70
71
        return new NullCache();
72
    }
73
}
74