DoctrineCacheFactory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 26
c 1
b 0
f 0
dl 0
loc 53
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createArrayAdapter() 0 3 1
A createPhpFilesAdapter() 0 6 2
A createAdapter() 0 20 5
A createVoidAdapter() 0 3 1
A make() 0 3 1
A createFilesystemAdapter() 0 6 2
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 Doctrine\Common\Cache\VoidCache;
13
use Psr\Cache\CacheItemPoolInterface;
14
use PublishingKit\Cache\Contracts\Factories\CacheFactory;
15
use PublishingKit\Cache\Exceptions\Factories\PathNotSet;
16
17
final class DoctrineCacheFactory implements CacheFactory
18
{
19
    public function make(array $config): CacheItemPoolInterface
20
    {
21
        return new DoctrineCachePool($this->createAdapter($config));
22
    }
23
24
    private function createAdapter(array $config): Cache
25
    {
26
        if (!isset($config['driver'])) {
27
            $config['driver'] = 'filesystem';
28
        }
29
        switch ($config['driver']) {
30
            case 'array':
31
                $driver = $this->createArrayAdapter();
32
                break;
33
            case 'php-files':
34
                $driver = $this->createPhpFilesAdapter($config);
35
                break;
36
            case 'void':
37
                $driver = $this->createVoidAdapter();
38
                break;
39
            default:
40
                $driver = $this->createFilesystemAdapter($config);
41
                break;
42
        }
43
        return $driver;
44
    }
45
46
    private function createFilesystemAdapter(array $config): FilesystemCache
47
    {
48
        if (!isset($config['path'])) {
49
            throw new PathNotSet('Path must be set for the filesystem adapter');
50
        }
51
        return new FilesystemCache($config['path']);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\FilesystemCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

51
        return /** @scrutinizer ignore-deprecated */ new FilesystemCache($config['path']);
Loading history...
52
    }
53
54
    private function createArrayAdapter(): ArrayCache
55
    {
56
        return new ArrayCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

56
        return /** @scrutinizer ignore-deprecated */ new ArrayCache();
Loading history...
57
    }
58
59
    private function createPhpFilesAdapter(array $config): PhpFileCache
60
    {
61
        if (!isset($config['path'])) {
62
            throw new PathNotSet('Path must be set for the PHP file adapter');
63
        }
64
        return new PhpFileCache($config['path']);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\PhpFileCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

64
        return /** @scrutinizer ignore-deprecated */ new PhpFileCache($config['path']);
Loading history...
65
    }
66
67
    private function createVoidAdapter(): VoidCache
68
    {
69
        return new VoidCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\VoidCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

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

69
        return /** @scrutinizer ignore-deprecated */ new VoidCache();
Loading history...
70
    }
71
}
72