Completed
Push — master ( be9660...707803 )
by Georges
16s queued 13s
created

FullReplicationCluster::getItem()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 29
c 0
b 0
f 0
nc 14
nop 1
dl 0
loc 47
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\FullReplication;
17
18
use Phpfastcache\Cluster\ClusterPoolAbstract;
19
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
21
use Psr\Cache\CacheItemInterface;
22
23
/**
24
 * Class FullReplicationCluster
25
 * @package Phpfastcache\Cluster\Drivers\FullReplication
26
 */
27
class FullReplicationCluster extends ClusterPoolAbstract
28
{
29
    /**
30
     * @inheritDoc
31
     */
32
    public function getItem($key)
33
    {
34
        /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */
35
        $poolsToResync = [];
36
        /** @var ExtendedCacheItemInterface $item */
37
        $item = null;
38
39
        foreach ($this->clusterPools as $driverPool) {
40
            $poolItem = $driverPool->getItem($key);
41
            if ($poolItem->isHit()) {
42
                if (!$item) {
43
                    $item = $poolItem;
44
                    continue;
45
                }
46
47
                $itemData = $item->get();
48
                $poolItemData = $poolItem->get();
49
50
                if (\is_object($itemData)
51
                ) {
52
                    if ($item->get() != $poolItemData) {
53
                        $poolsToResync[] = $driverPool;
54
                    }
55
                } else {
56
                    if ($item->get() !== $poolItemData) {
57
                        $poolsToResync[] = $driverPool;
58
                    }
59
                }
60
            } else {
61
                $poolsToResync[] = $driverPool;
62
            }
63
        }
64
65
        if ($item && $item->isHit() && \count($poolsToResync) < \count($this->clusterPools)) {
66
            foreach ($poolsToResync as $poolToResync) {
67
                $poolItem = $poolToResync->getItem($key);
68
                $poolItem->setEventManager($this->getEventManager())
69
                    ->set($item->get())
70
                    ->setHit($item->isHit())
71
                    ->setTags($item->getTags())
72
                    ->expiresAt($item->getExpirationDate())
73
                    ->setDriver($poolToResync);
74
                $poolToResync->save($poolItem);
75
            }
76
        }
77
78
        return $this->getStandardizedItem($item ?? new Item($this, $key), $this);
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function hasItem($key)
85
    {
86
        foreach ($this->clusterPools as $driverPool) {
87
            $poolItem = $driverPool->getItem($key);
88
            if ($poolItem->isHit()) {
89
                return true;
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function clear()
100
    {
101
        $hasClearedOnce = false;
102
        foreach ($this->clusterPools as $driverPool) {
103
            if ($result = $driverPool->clear()) {
104
                $hasClearedOnce = $result;
105
            }
106
        }
107
        // Return true only if at least one backend confirmed the "clear" operation
108
        return $hasClearedOnce;
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    public function deleteItem($key)
115
    {
116
        $hasDeletedOnce = false;
117
        foreach ($this->clusterPools as $driverPool) {
118
            if ($result = $driverPool->deleteItem($key)) {
119
                $hasDeletedOnce = $result;
120
            }
121
        }
122
        // Return true only if at least one backend confirmed the "clear" operation
123
        return $hasDeletedOnce;
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function save(CacheItemInterface $item)
130
    {
131
        /** @var ExtendedCacheItemInterface $item */
132
        $hasSavedOnce = false;
133
        foreach ($this->clusterPools as $driverPool) {
134
            $poolItem = $this->getStandardizedItem($item, $driverPool);
135
            if ($result = $driverPool->save($poolItem)) {
136
                $hasSavedOnce = $result;
137
            }
138
        }
139
        // Return true only if at least one backend confirmed the "commit" operation
140
        return $hasSavedOnce;
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146
    public function saveDeferred(CacheItemInterface $item)
147
    {
148
        /** @var ExtendedCacheItemInterface $item */
149
        $hasSavedOnce = false;
150
        foreach ($this->clusterPools as $driverPool) {
151
            $poolItem = $this->getStandardizedItem($item, $driverPool);
152
            if ($result = $driverPool->saveDeferred($poolItem)) {
153
                $hasSavedOnce = $result;
154
            }
155
        }
156
        // Return true only if at least one backend confirmed the "commit" operation
157
        return $hasSavedOnce;
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function commit()
164
    {
165
        $hasCommitOnce = false;
166
        foreach ($this->clusterPools as $driverPool) {
167
            if ($result = $driverPool->commit()) {
168
                $hasCommitOnce = $result;
169
            }
170
        }
171
        // Return true only if at least one backend confirmed the "commit" operation
172
        return $hasCommitOnce;
173
    }
174
175
176
}
177