Passed
Pull Request — master (#856)
by
unknown
10:26
created

Driver::getItem()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 32
c 0
b 0
f 0
nc 28
nop 1
dl 0
loc 52
rs 6.9666

How to fix   Long Method    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\Config\ConfigurationOption;
20
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
21
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
22
use Psr\Cache\CacheItemInterface;
23
24
class Driver extends ClusterPoolAbstract
25
{
26
    /**
27
     * @inheritDoc
28
     */
29
    public function getItem(string $key): ExtendedCacheItemInterface
30
    {
31
        /** @var ExtendedCacheItemPoolInterface[] $poolsToResync */
32
        $poolsToResync = [];
33
        /** @var ?ExtendedCacheItemInterface $item */
34
        $item = null;
35
36
        foreach ($this->clusterPools as $driverPool) {
37
            $poolItem = $driverPool->getItem($key);
38
            if ($poolItem->isHit()) {
39
                if (!$item) {
40
                    $item = $poolItem;
41
                    continue;
42
                }
43
44
                $itemData = $item->get();
45
                $poolItemData = $poolItem->get();
46
47
                if (\is_object($itemData)
48
                ) {
49
                    if ($item->get() != $poolItemData) {
50
                        $poolsToResync[] = $driverPool;
51
                    }
52
                } else {
53
                    if ($item->get() !== $poolItemData) {
54
                        $poolsToResync[] = $driverPool;
55
                    }
56
                }
57
            } else {
58
                $poolsToResync[] = $driverPool;
59
            }
60
        }
61
62
        if ($item && $item->isHit() && \count($poolsToResync) < \count($this->clusterPools)) {
63
            foreach ($poolsToResync as $poolToResync) {
64
                $poolItem = $poolToResync->getItem($key);
65
                $poolItem->setEventManager($this->getEventManager())
66
                    ->set($item->get())
67
                    ->setHit($item->isHit())
68
                    ->setTags($item->getTags())
69
                    ->expiresAt($item->getExpirationDate())
70
                    ->setDriver($poolToResync);
71
                $poolToResync->save($poolItem);
72
            }
73
        }
74
75
        if ($item === null) {
76
            $item = new Item($this, $key, $this->getEventManager());
77
            $item->expiresAfter(abs($this->getConfig()->getDefaultTtl()));
0 ignored issues
show
Bug introduced by
It seems like abs($this->getConfig()->getDefaultTtl()) can also be of type double; however, parameter $time of Phpfastcache\Cluster\ItemAbstract::expiresAfter() does only seem to accept DateInterval|integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $item->expiresAfter(/** @scrutinizer ignore-type */ abs($this->getConfig()->getDefaultTtl()));
Loading history...
78
        }
79
80
        return $this->getStandardizedItem($item, $this);
81
    }
82
83
    /**
84
     * @inheritDoc
85
     */
86
    public function hasItem(string $key): bool
87
    {
88
        foreach ($this->clusterPools as $driverPool) {
89
            $poolItem = $driverPool->getItem($key);
90
            if ($poolItem->isHit()) {
91
                return true;
92
            }
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function clear(): bool
102
    {
103
        $hasClearedOnce = false;
104
        foreach ($this->clusterPools as $driverPool) {
105
            if ($result = $driverPool->clear()) {
106
                $hasClearedOnce = $result;
107
            }
108
        }
109
        // Return true only if at least one backend confirmed the "clear" operation
110
        return $hasClearedOnce;
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function deleteItem(string $key): bool
117
    {
118
        $hasDeletedOnce = false;
119
        foreach ($this->clusterPools as $driverPool) {
120
            if ($result = $driverPool->deleteItem($key)) {
121
                $hasDeletedOnce = $result;
122
            }
123
        }
124
        // Return true only if at least one backend confirmed the "clear" operation
125
        return $hasDeletedOnce;
126
    }
127
128
    /**
129
     * @inheritDoc
130
     */
131
    public function save(CacheItemInterface $item): bool
132
    {
133
        /** @var ExtendedCacheItemInterface $item */
134
        $hasSavedOnce = false;
135
        foreach ($this->clusterPools as $driverPool) {
136
            $poolItem = $this->getStandardizedItem($item, $driverPool);
137
            if ($result = $driverPool->save($poolItem)) {
138
                $hasSavedOnce = $result;
139
            }
140
        }
141
        // Return true only if at least one backend confirmed the "commit" operation
142
        return $hasSavedOnce;
143
    }
144
145
    /**
146
     * @inheritDoc
147
     */
148
    public function saveDeferred(CacheItemInterface $item): bool
149
    {
150
        /** @var ExtendedCacheItemInterface $item */
151
        $hasSavedOnce = false;
152
        foreach ($this->clusterPools as $driverPool) {
153
            $poolItem = $this->getStandardizedItem($item, $driverPool);
154
            if ($result = $driverPool->saveDeferred($poolItem)) {
155
                $hasSavedOnce = $result;
156
            }
157
        }
158
        // Return true only if at least one backend confirmed the "commit" operation
159
        return $hasSavedOnce;
160
    }
161
162
    /**
163
     * @inheritDoc
164
     */
165
    public function commit(): bool
166
    {
167
        $hasCommitOnce = false;
168
        foreach ($this->clusterPools as $driverPool) {
169
            if ($result = $driverPool->commit()) {
170
                $hasCommitOnce = $result;
171
            }
172
        }
173
        // Return true only if at least one backend confirmed the "commit" operation
174
        return $hasCommitOnce;
175
    }
176
}
177