| @@ 149-164 (lines=16) @@ | ||
| 146 | * |
|
| 147 | * @return integer|false Returns the new incremented value or FALSE on failure |
|
| 148 | */ |
|
| 149 | public function inc($key, $step = 1) |
|
| 150 | { |
|
| 151 | $key = $this->prefix.$key; |
|
| 152 | ||
| 153 | if ($this->driver instanceof IncrementorInterface) { |
|
| 154 | return $this->driver->inc($key, $step); |
|
| 155 | } |
|
| 156 | ||
| 157 | $value = $this->driver->get($key); |
|
| 158 | if (is_null($value) || !is_numeric($value)) { |
|
| 159 | return false; |
|
| 160 | } |
|
| 161 | $newValue = $value + $step; |
|
| 162 | ||
| 163 | return ($this->driver->set($key, $newValue)) ? $newValue : false; |
|
| 164 | } |
|
| 165 | ||
| 166 | /** |
|
| 167 | * Decrements numeric item's value. |
|
| @@ 175-190 (lines=16) @@ | ||
| 172 | * |
|
| 173 | * @return integer|false Returns the new decremented value or FALSE on failure |
|
| 174 | */ |
|
| 175 | public function dec($key, $step = 1) |
|
| 176 | { |
|
| 177 | $key = $this->prefix.$key; |
|
| 178 | ||
| 179 | if ($this->driver instanceof IncrementorInterface) { |
|
| 180 | return $this->driver->dec($key, $step); |
|
| 181 | } |
|
| 182 | ||
| 183 | $value = $this->driver->get($key); |
|
| 184 | if (is_null($value) || !is_numeric($value)) { |
|
| 185 | return false; |
|
| 186 | } |
|
| 187 | $newValue = $value - $step; |
|
| 188 | ||
| 189 | return ($this->driver->set($key, $newValue)) ? $newValue : false; |
|
| 190 | } |
|
| 191 | } |
|
| 192 | ||