Driver::getItem()   B
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
c 0
b 0
f 0
nc 15
nop 1
dl 0
loc 28
rs 8.8333
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\SemiReplication;
18
19
use Phpfastcache\Cluster\ClusterPoolAbstract;
20
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
21
use Phpfastcache\Exceptions\PhpfastcacheExceptionInterface;
22
use Phpfastcache\Exceptions\PhpfastcacheReplicationException;
23
use Psr\Cache\CacheItemInterface;
24
use Psr\Cache\InvalidArgumentException;
25
26
class Driver extends ClusterPoolAbstract
27
{
28
    /**
29
     * @inheritDoc
30
     * @throws InvalidArgumentException
31
     * @throws PhpfastcacheReplicationException
32
     */
33
    public function getItem(string $key): ExtendedCacheItemInterface
34
    {
35
        /** @var ?ExtendedCacheItemInterface $item */
36
        $item = null;
37
        $eCount = 0;
38
39
        foreach ($this->clusterPools as $driverPool) {
40
            try {
41
                $poolItem = $driverPool->getItem($key);
42
                if (!$item && $poolItem->isHit()) {
43
                    $item = $poolItem;
44
                    break;
45
                }
46
            } catch (PhpfastcacheExceptionInterface) {
47
                $eCount++;
48
            }
49
        }
50
51
        if (\count($this->clusterPools) <= $eCount) {
52
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
53
        }
54
55
        if ($item === null) {
56
            $item = new Item($this, $key, $this->getEventManager());
57
            $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl()));
58
        }
59
60
        return $this->getStandardizedItem($item, $this);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     * @throws PhpfastcacheReplicationException
66
     */
67
    public function hasItem(string $key): bool
68
    {
69
        $eCount = 0;
70
        foreach ($this->clusterPools as $driverPool) {
71
            try {
72
                $poolItem = $driverPool->getItem($key);
73
                if ($poolItem->isHit()) {
74
                    return true;
75
                }
76
            } catch (PhpfastcacheExceptionInterface) {
77
                $eCount++;
78
            }
79
        }
80
81
        if (\count($this->clusterPools) <= $eCount) {
82
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * @inheritDoc
90
     * @throws PhpfastcacheReplicationException
91
     */
92
    public function clear(): bool
93
    {
94
        $hasClearedOnce = false;
95
        $eCount = 0;
96
97
        foreach ($this->clusterPools as $driverPool) {
98
            try {
99
                if ($result = $driverPool->clear()) {
100
                    $hasClearedOnce = $result;
101
                }
102
            } catch (PhpfastcacheExceptionInterface) {
103
                $eCount++;
104
            }
105
        }
106
107
        if (\count($this->clusterPools) <= $eCount) {
108
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
109
        }
110
111
        // Return true only if at least one backend confirmed the "clear" operation
112
        return $hasClearedOnce;
113
    }
114
115
    /**
116
     * @inheritDoc
117
     * @throws PhpfastcacheReplicationException
118
     * @throws InvalidArgumentException
119
     */
120
    public function deleteItem(string $key): bool
121
    {
122
        $hasDeletedOnce = false;
123
        $eCount = 0;
124
125
        foreach ($this->clusterPools as $driverPool) {
126
            try {
127
                if ($result = $driverPool->deleteItem($key)) {
128
                    $hasDeletedOnce = $result;
129
                }
130
            } catch (PhpfastcacheExceptionInterface) {
131
                $eCount++;
132
            }
133
        }
134
135
        if (\count($this->clusterPools) <= $eCount) {
136
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
137
        }
138
        // Return true only if at least one backend confirmed the "clear" operation
139
        return $hasDeletedOnce;
140
    }
141
142
    /**
143
     * @param CacheItemInterface $item
144
     * @return bool
145
     * @throws InvalidArgumentException
146
     * @throws PhpfastcacheReplicationException
147
     */
148
    public function save(CacheItemInterface $item): bool
149
    {
150
        /** @var ExtendedCacheItemInterface $item */
151
        $hasSavedOnce = false;
152
        $eCount = 0;
153
154
        foreach ($this->clusterPools as $driverPool) {
155
            try {
156
                $poolItem = $this->getStandardizedItem($item, $driverPool);
157
                if ($result = $driverPool->save($poolItem)) {
158
                    $hasSavedOnce = $result;
159
                }
160
            } catch (PhpfastcacheExceptionInterface) {
161
                $eCount++;
162
            }
163
        }
164
165
        if (\count($this->clusterPools) <= $eCount) {
166
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
167
        }
168
        // Return true only if at least one backend confirmed the "commit" operation
169
        return $hasSavedOnce;
170
    }
171
172
    /**
173
     * @inheritDoc
174
     * @throws PhpfastcacheReplicationException
175
     */
176
    public function commit(): bool
177
    {
178
        $hasCommitOnce = false;
179
        $eCount = 0;
180
181
        foreach ($this->clusterPools as $driverPool) {
182
            try {
183
                if ($result = $driverPool->commit()) {
184
                    $hasCommitOnce = $result;
185
                }
186
            } catch (PhpfastcacheExceptionInterface) {
187
                $eCount++;
188
            }
189
        }
190
191
        if (\count($this->clusterPools) <= $eCount) {
192
            throw new PhpfastcacheReplicationException('Every pools thrown an exception');
193
        }
194
        // Return true only if at least one backend confirmed the "commit" operation
195
        return $hasCommitOnce;
196
    }
197
}
198