Driver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 127
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A clear() 0 4 1
A getSlavePool() 0 3 1
A makeOperation() 0 14 3
A commit() 0 4 1
A getItem() 0 7 1
A hasItem() 0 4 1
A save() 0 7 1
A deleteItem() 0 4 1
A getMasterPool() 0 3 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Georges.L (Geolim4) <[email protected]>
12
 *
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Cluster\Drivers\MasterSlaveReplication;
18
19
use Phpfastcache\Cluster\AggregatablePoolInterface;
20
use Phpfastcache\Cluster\ClusterPoolAbstract;
21
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
22
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
23
use Phpfastcache\Event\Event;
24
use Phpfastcache\Event\EventManagerInterface;
25
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
26
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
27
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
28
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
29
use Phpfastcache\Exceptions\PhpfastcacheExceptionInterface;
30
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
31
use Phpfastcache\Exceptions\PhpfastcacheIOException;
32
use Phpfastcache\Exceptions\PhpfastcacheReplicationException;
33
use Psr\Cache\CacheItemInterface;
34
35
class Driver extends ClusterPoolAbstract
36
{
37
    /**
38
     * MasterSlaveReplicationCluster constructor.
39
     * @param string $clusterName
40
     * @param EventManagerInterface $em
41
     * @param AggregatablePoolInterface ...$driverPools
42
     * @throws PhpfastcacheDriverCheckException
43
     * @throws PhpfastcacheDriverConnectException
44
     * @throws PhpfastcacheInvalidArgumentException
45
     * @throws PhpfastcacheCoreException
46
     * @throws PhpfastcacheDriverException
47
     * @throws PhpfastcacheIOException
48
     */
49
    public function __construct(string $clusterName, EventManagerInterface $em, AggregatablePoolInterface ...$driverPools)
50
    {
51
        if (\count($driverPools) !== 2) {
52
            throw new PhpfastcacheInvalidArgumentException('A "master/slave" cluster requires exactly two pools to be working.');
53
        }
54
55
        parent::__construct($clusterName, $em, ...$driverPools);
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function getItem(string $key): ExtendedCacheItemInterface
62
    {
63
        return $this->getStandardizedItem(
64
            $this->makeOperation(static function (ExtendedCacheItemPoolInterface $pool) use ($key) {
65
                return $pool->getItem($key);
66
            }) ?? (new Item($this, $key, $this->getEventManager()))->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())),
67
            $this
68
        );
69
    }
70
71
    /**
72
     * @param callable $operation
73
     * @return mixed
74
     * @throws PhpfastcacheReplicationException
75
     */
76
    protected function makeOperation(callable $operation)
77
    {
78
        try {
79
            return $operation($this->getMasterPool());
80
        } catch (PhpfastcacheExceptionInterface $e) {
81
            try {
82
                $this->eventManager->dispatch(
83
                    Event::CACHE_REPLICATION_SLAVE_FALLBACK,
84
                    $this,
85
                    \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']
86
                );
87
                return $operation($this->getSlavePool());
88
            } catch (PhpfastcacheExceptionInterface $e) {
89
                throw new PhpfastcacheReplicationException('Master and Slave thrown an exception !');
90
            }
91
        }
92
    }
93
94
    /**
95
     * @return AggregatablePoolInterface
96
     */
97
    protected function getMasterPool(): AggregatablePoolInterface
98
    {
99
        return $this->clusterPools[0];
100
    }
101
102
    /**
103
     * @return AggregatablePoolInterface
104
     */
105
    protected function getSlavePool(): AggregatablePoolInterface
106
    {
107
        return $this->clusterPools[1];
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function hasItem(string $key): bool
114
    {
115
        return $this->makeOperation(
116
            static fn (ExtendedCacheItemPoolInterface $pool) => $pool->hasItem($key)
117
        );
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    public function clear(): bool
124
    {
125
        return $this->makeOperation(
126
            static fn (ExtendedCacheItemPoolInterface $pool) => $pool->clear()
127
        );
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function deleteItem(string $key): bool
134
    {
135
        return $this->makeOperation(
136
            static fn (ExtendedCacheItemPoolInterface $pool) => $pool->deleteItem($key)
137
        );
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function save(CacheItemInterface $item): bool
144
    {
145
        return $this->makeOperation(
146
            function (ExtendedCacheItemPoolInterface $pool) use ($item) {
147
                /** @var ExtendedCacheItemInterface $item */
148
                $item->setHit(true);
149
                return $pool->save($this->getStandardizedItem($item, $pool));
150
            }
151
        );
152
    }
153
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function commit(): bool
159
    {
160
        return $this->makeOperation(
161
            static fn (ExtendedCacheItemPoolInterface $pool) => $pool->commit()
162
        );
163
    }
164
}
165