StashCacheFactory::createSqliteAdapter()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 8.8333
cc 7
nc 1
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PublishingKit\Cache\Factories;
6
7
use Stash\Pool;
8
use Stash\Driver\Apc;
9
use Stash\Driver\BlackHole;
10
use Stash\Driver\Composite;
11
use Stash\Driver\Ephemeral;
12
use Stash\Driver\FileSystem;
13
use Stash\Driver\Memcache;
14
use Stash\Driver\Redis;
15
use Stash\Driver\Sqlite;
16
use Stash\Interfaces\DriverInterface;
17
use PublishingKit\Cache\Contracts\Factories\CacheFactory;
18
use Psr\Cache\CacheItemPoolInterface;
19
use PublishingKit\Cache\Exceptions\Factories\PathNotSet;
20
21
final class StashCacheFactory implements CacheFactory
22
{
23 30
    public function make(array $config): CacheItemPoolInterface
24
    {
25 30
        $driver = $this->createAdapter($config);
26 27
        return new Pool($driver);
27
    }
28
29 30
    private function createAdapter(array $config): DriverInterface
30
    {
31 30
        if (!isset($config['driver'])) {
32 3
            $config['driver'] = 'filesystem';
33
        }
34 30
        switch ($config['driver']) {
35 30
            case 'test':
36 3
                $driver = $this->createBlackHoleAdapter();
37 3
                break;
38 27
            case 'ephemeral':
39 6
                $driver = $this->createEphemeralAdapter();
40 6
                break;
41 24
            case 'composite':
42 3
                $driver = $this->createCompositeAdapter($config);
43 3
                break;
44 24
            case 'sqlite':
45 3
                $driver = $this->createSqliteAdapter($config);
46 3
                break;
47 21
            case 'apc':
48 3
                $driver = $this->createApcAdapter($config);
49 3
                break;
50 18
            case 'memcache':
51 3
                $driver = $this->createMemcacheAdapter($config);
52 3
                break;
53 15
            case 'redis':
54 3
                $driver = $this->createRedisAdapter($config);
55 3
                break;
56
            default:
57 12
                $driver = $this->createFilesystemAdapter($config);
58 9
                break;
59
        }
60 27
        return $driver;
61
    }
62
63 12
    private function createFilesystemAdapter(array $config): FileSystem
64
    {
65 12
        if (!isset($config['path'])) {
66 3
            throw new PathNotSet('Path must be set for the filesystem adapter');
67
        }
68
        $adapterConfig = [
69 9
            'path' => $config['path'],
70
        ];
71 9
        if (isset($config['dirSplit'])) {
72 3
            $adapterConfig['dirSplit'] = $config['dirSplit'];
73
        }
74 9
        if (isset($config['filePermissions'])) {
75 3
            $adapterConfig['filePermissions'] = $config['filePermissions'];
76
        }
77 9
        if (isset($config['dirPermissions'])) {
78 3
            $adapterConfig['dirPermissions'] = $config['dirPermissions'];
79
        }
80 9
        return new FileSystem($adapterConfig);
81
    }
82
83 3
    private function createBlackHoleAdapter(): BlackHole
84
    {
85 3
        return new BlackHole();
86
    }
87
88 6
    private function createEphemeralAdapter(): Ephemeral
89
    {
90 6
        return new Ephemeral();
91
    }
92
93 3
    private function createCompositeAdapter(array $config): Composite
94
    {
95 3
        $drivers = [];
96 3
        foreach ($config['subdrivers'] as $driver) {
97 3
            $drivers[] = $this->createAdapter($driver);
98
        }
99 3
        return new Composite([
100 3
            'drivers' => $drivers
101
        ]);
102
    }
103
104 3
    private function createSqliteAdapter(array $config): Sqlite
105
    {
106 3
        return new Sqlite([
107 3
            'extension' => isset($config['extension']) ? $config['extension'] : null,
108 3
            'version' => isset($config['version']) ? $config['version'] : null,
109 3
            'nesting' => isset($config['nesting']) ? $config['nesting'] : null,
110 3
            'path' => isset($config['path']) ? $config['path'] : null,
111 3
            'filePermissions' => isset($config['filePermissions']) ? $config['filePermissions'] : null,
112 3
            'dirPermissions' => isset($config['dirPermissions']) ? $config['dirPermissions'] : null
113
        ]);
114
    }
115
116 3
    private function createApcAdapter(array $config): Apc
117
    {
118 3
        return new Apc([
119 3
            'ttl' => isset($config['ttl']) ? $config['ttl'] : null,
120 3
            'namespace' => isset($config['namespace']) ? $config['namespace'] : null
121
        ]);
122
    }
123
124 3
    private function createMemcacheAdapter(array $config): Memcache
125
    {
126
        $options = [
127 3
            'servers' => isset($config['servers']) ? $config['servers'] : null,
128
        ];
129 3
        if (isset($config['extension'])) {
130 3
            $options['extension'] = $config['extension'];
131
        }
132 3
        if (isset($config['prefix_key'])) {
133 3
            $options['prefix_key'] = $config['prefix_key'];
134
        }
135 3
        return new Memcache($options);
136
    }
137
138 3
    private function createRedisAdapter(array $config): Redis
139
    {
140 3
        return new Redis([
141 3
            'servers' => isset($config['servers']) ? $config['servers'] : null,
142
        ]);
143
    }
144
}
145