Completed
Push — master ( 3013b6...f1fd2e )
by Matthew
18:40
created

DoctrineCacheFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PublishingKit\Cache\Factories;
6
7
use Cache\Adapter\Doctrine\DoctrineCachePool;
8
use Doctrine\Common\Cache\ArrayCache;
9
use Doctrine\Common\Cache\Cache;
10
use Doctrine\Common\Cache\FilesystemCache;
11
use Doctrine\Common\Cache\PhpFileCache;
12
use Psr\Cache\CacheItemPoolInterface;
13
use PublishingKit\Cache\Contracts\Factories\CacheFactory;
14
use PublishingKit\Cache\Exceptions\Factories\PathNotSet;
15
16
final class DoctrineCacheFactory implements CacheFactory
17
{
18
    public function make(array $config): CacheItemPoolInterface
19
    {
20
        return new DoctrineCachePool($this->createAdapter($config));
21
    }
22
23
    private function createAdapter(array $config): Cache
24
    {
25
        if (!isset($config['driver'])) {
26
            $config['driver'] = 'filesystem';
27
        }
28
        switch ($config['driver']) {
29
            case 'array':
30
                $driver = $this->createArrayAdapter();
31
                break;
32
            case 'php-files':
33
                $driver = $this->createPhpFilesAdapter($config);
34
                break;
35
            default:
36
                $driver = $this->createFilesystemAdapter($config);
37
                break;
38
        }
39
        return $driver;
40
    }
41
42
    private function createFilesystemAdapter(array $config): FilesystemCache
43
    {
44
        if (!isset($config['path'])) {
45
            throw new PathNotSet('Path must be set for the filesystem adapter');
46
        }
47
        return new FilesystemCache($config['path']);
48
    }
49
50
    private function createArrayAdapter(): ArrayCache
51
    {
52
        return new ArrayCache();
53
    }
54
55
    private function createPhpFilesAdapter(array $config): PhpFileCache
56
    {
57
        if (!isset($config['path'])) {
58
            throw new PathNotSet('Path must be set for the PHP file adapter');
59
        }
60
        return new PhpFileCache($config['path']);
61
    }
62
}
63