Passed
Pull Request — master (#838)
by Georges
04:11 queued 02:01
created

Driver::deleteItem()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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