Completed
Push — master ( 3d22df...95226d )
by Georges
22s queued 12s
created

Driver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

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