Passed
Push — master ( 48948a...a5130b )
by Christian
02:28
created

ChainBuilder::addMemory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPCacheAdapter\CacheItemPool;
6
7
use Psr\Cache;
8
use RemotelyLiving\PHPCacheAdapter\SimpleCache;
9
10
final class ChainBuilder
11
{
12
    private SimpleCache\Chain $chain;
13
14
    private ?string $namespace;
15
16
    private ?int $defaultTTL;
17
18
    private function __construct(?string $namespace = null, int $defaultTTL = null)
19
    {
20
21
        $this->namespace = $namespace;
22
        $this->defaultTTL = $defaultTTL;
23
        $this->chain = SimpleCache\Chain::create();
24
    }
25
26
    public static function create(?string $namespace = null, int $defaultTTL = null): self
27
    {
28
        return new self($namespace, $defaultTTL);
29
    }
30
31
    public function addMemcached(\Memcached $memcached): self
32
    {
33
        $this->chain->addAdapter(SimpleCache\Memcached::create($memcached));
34
35
        return $this;
36
    }
37
38
    public function addRedis(\Redis $redis): self
39
    {
40
        $this->chain->addAdapter(SimpleCache\Redis::create($redis));
41
42
        return $this;
43
    }
44
45
    public function addMemory(int $maxItems = null): self
46
    {
47
        $this->chain->addAdapter(SimpleCache\Memory::create($maxItems));
48
49
        return $this;
50
    }
51
52
    public function addAPCu(): self
53
    {
54
        $this->chain->addAdapter(SimpleCache\APCu::create());
55
56
        return $this;
57
    }
58
59
    public function build(): Cache\CacheItemPoolInterface
60
    {
61
        return CacheItemPool::createFromSimpleCache($this->chain, $this->namespace, $this->defaultTTL);
62
    }
63
}
64