Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 28 | class CouchbaseStore extends TaggableStore implements Store |
||
| 29 | { |
||
| 30 | use RetrievesMultipleKeys; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected $prefix; |
||
| 34 | |||
| 35 | /** @var CouchbaseBucket */ |
||
| 36 | protected $bucket; |
||
| 37 | |||
| 38 | /** @var CouchbaseCluster */ |
||
| 39 | protected $cluster; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * CouchbaseStore constructor. |
||
| 43 | * |
||
| 44 | * @param CouchbaseCluster $cluster |
||
| 45 | * @param $bucket |
||
| 46 | * @param string $password |
||
| 47 | * @param null $prefix |
||
| 48 | * @param string $serialize |
||
| 49 | */ |
||
| 50 | public function __construct(CouchbaseCluster $cluster, $bucket, $password = '', $prefix = null, $serialize = 'php') |
||
| 51 | { |
||
| 52 | $this->cluster = $cluster; |
||
| 53 | $this->setBucket($bucket, $password, $serialize); |
||
| 54 | $this->setPrefix($prefix); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function get($key) |
||
| 61 | { |
||
| 62 | try { |
||
| 63 | $result = $this->bucket->get($this->resolveKey($key)); |
||
| 64 | |||
| 65 | return $this->getMetaDoc($result); |
||
| 66 | } catch (CouchbaseException $e) { |
||
|
|
|||
| 67 | return; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Store an item in the cache if the key doesn't exist. |
||
| 73 | * |
||
| 74 | * @param string|array $key |
||
| 75 | * @param mixed $value |
||
| 76 | * @param int $minutes |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function add($key, $value, $minutes = 0) |
||
| 81 | { |
||
| 82 | $options = ($minutes === 0) ? [] : ['expiry' => ($minutes * 60)]; |
||
| 83 | try { |
||
| 84 | $this->bucket->insert($this->resolveKey($key), $value, $options); |
||
| 85 | |||
| 86 | return true; |
||
| 87 | } catch (CouchbaseException $e) { |
||
| 88 | return false; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | public function put($key, $value, $minutes) |
||
| 96 | { |
||
| 97 | $this->bucket->upsert($this->resolveKey($key), $value, ['expiry' => $minutes * 60]); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | public function increment($key, $value = 1) |
||
| 104 | { |
||
| 105 | return $this->bucket |
||
| 106 | ->counter($this->resolveKey($key), $value, ['initial' => abs($value)])->value; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function decrement($key, $value = 1) |
||
| 113 | { |
||
| 114 | return $this->bucket |
||
| 115 | ->counter($this->resolveKey($key), (0 - abs($value)), ['initial' => (0 - abs($value))])->value; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function forever($key, $value) |
||
| 122 | { |
||
| 123 | try { |
||
| 124 | $this->bucket->insert($this->resolveKey($key), $value); |
||
| 125 | } catch (CouchbaseException $e) { |
||
| 126 | // bucket->insert when called from resetTag in TagSet can throw CAS exceptions, ignore.\ |
||
| 127 | $this->bucket->upsert($this->resolveKey($key), $value); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function forget($key) |
||
| 135 | { |
||
| 136 | try { |
||
| 137 | $this->bucket->remove($this->resolveKey($key)); |
||
| 138 | } catch (\Exception $e) { |
||
| 139 | // Ignore exceptions from remove |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * flush bucket. |
||
| 145 | * |
||
| 146 | * @throws FlushException |
||
| 147 | * @codeCoverageIgnore |
||
| 148 | */ |
||
| 149 | public function flush() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function getPrefix() |
||
| 161 | { |
||
| 162 | return $this->prefix; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Set the cache key prefix. |
||
| 167 | * |
||
| 168 | * @param string $prefix |
||
| 169 | */ |
||
| 170 | public function setPrefix($prefix) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param $bucket |
||
| 177 | * @param string $password |
||
| 178 | * @param string $serialize |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function setBucket($bucket, $password = '', $serialize = 'php') |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param $keys |
||
| 194 | * |
||
| 195 | * @return array|string |
||
| 196 | */ |
||
| 197 | private function resolveKey($keys) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param $meta |
||
| 213 | * |
||
| 214 | * @return array|null |
||
| 215 | */ |
||
| 216 | View Code Duplication | protected function getMetaDoc($meta) |
|
| 232 | } |
||
| 233 |