Completed
Push — master ( 3b4436...567fe0 )
by Matthew
02:23
created

SymfonyCacheFactory::createRedisAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PublishingKit\Cache\Factories;
6
7
use Psr\Cache\CacheItemPoolInterface;
8
use PublishingKit\Cache\Contracts\Factories\CacheFactory;
9
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
10
use Symfony\Component\Cache\Adapter\ArrayAdapter;
11
use Symfony\Component\Cache\Adapter\ApcuAdapter;
12
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
13
use Symfony\Component\Cache\Adapter\RedisAdapter;
14
15
final class SymfonyCacheFactory implements CacheFactory
16
{
17 15
    public function make(array $config): CacheItemPoolInterface
18
    {
19 15
        return $this->createAdapter($config);
20
    }
21
22 15
    private function createAdapter(array $config): CacheItemPoolInterface
23
    {
24 15
        if (!isset($config['driver'])) {
25 3
            $config['driver'] = 'filesystem';
26
        }
27 15
        switch ($config['driver']) {
28 15
            case 'array':
29 3
                $driver = $this->createArrayAdapter($config);
30 3
                break;
31 12
            case 'apcu':
32 3
                $driver = $this->createApcuAdapter($config);
33 3
                break;
34 9
            case 'memcached':
35
                $driver = $this->createMemcachedAdapter($config);
36
                break;
37 9
            case 'redis':
38 3
                $driver = $this->createRedisAdapter($config);
39 3
                break;
40
            default:
41 6
                $driver = $this->createFilesystemAdapter($config);
42 6
                break;
43
        }
44 15
        return $driver;
45
    }
46
47 6
    private function createFilesystemAdapter(array $config): FilesystemAdapter
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
    private function createFilesystemAdapter(/** @scrutinizer ignore-unused */ array $config): FilesystemAdapter

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49 6
        return new FilesystemAdapter();
50
    }
51
52 3
    private function createArrayAdapter(array $config): ArrayAdapter
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

52
    private function createArrayAdapter(/** @scrutinizer ignore-unused */ array $config): ArrayAdapter

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
    {
54 3
        return new ArrayAdapter();
55
    }
56
57 3
    private function createApcuAdapter(array $config): ApcuAdapter
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    private function createApcuAdapter(/** @scrutinizer ignore-unused */ array $config): ApcuAdapter

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59 3
        return new ApcuAdapter();
60
    }
61
62
    private function createMemcachedAdapter(array $config): MemcachedAdapter
63
    {
64
        $client = MemcachedAdapter::createConnection(
65
            $config['servers']
66
        );
67
        return new MemcachedAdapter($client);
68
    }
69
70 3
    private function createRedisAdapter(array $config): RedisAdapter
71
    {
72
73 3
        $client = RedisAdapter::createConnection(
74 3
            $config['server']
75
        );
76 3
        return new RedisAdapter($client);
77
    }
78
}
79