| Conditions | 19 |
| Paths | 9 |
| Total Lines | 78 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 165 | protected function setDeviceUserData($key, $data, $devid, $user, $subkey = -1, $doCas = false, $rawdata = false) { |
||
| 166 | if (!$this->ipcProvider) { |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | $compKey = $this->getComposedKey($devid, $user, $subkey); |
||
| 170 | $ok = false; |
||
| 171 | |||
| 172 | // overwrite |
||
| 173 | if (!$doCas) { |
||
| 174 | $ok = ($this->ipcProvider->get()->hset($key, $compKey, json_encode($data)) !== false); |
||
| 175 | } |
||
| 176 | // merge data and do CAS on the $compKey |
||
| 177 | elseif ($doCas == "merge") { |
||
| 178 | $okCount = 0; |
||
| 179 | // TODO: make this configurable (retrycount)? |
||
| 180 | while (!$ok && $okCount < 5) { |
||
| 181 | $newData = $data; |
||
| 182 | // step 1: get current data |
||
| 183 | $_rawdata = $this->ipcProvider->get()->hget($key, $compKey); |
||
| 184 | // step 2: if data exists, merge it with the new data. Keys within |
||
| 185 | // within the new data overwrite possible existing old data (update). |
||
| 186 | if ($_rawdata && $_rawdata !== null) { |
||
| 187 | $_old = json_decode((string) $_rawdata, true); |
||
| 188 | $newData = array_merge($_old, $data); |
||
| 189 | } |
||
| 190 | // step 3: replace old with new data |
||
| 191 | $ok = $this->ipcProvider->CASHash($key, $compKey, $_rawdata, json_encode($newData)); |
||
| 192 | if (!$ok) { |
||
| 193 | ++$okCount; |
||
| 194 | // retry in 0.1s |
||
| 195 | // TODO: make this configurable? |
||
| 196 | usleep(1000000); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | // replace data and do CAS on the $compKey - fail hard if CAS fails |
||
| 201 | elseif ($doCas == "replace") { |
||
| 202 | if (!$rawdata) { |
||
| 203 | $ok = ($this->ipcProvider->get()->hset($key, $compKey, json_encode($data)) !== false); |
||
| 204 | } |
||
| 205 | else { |
||
| 206 | $ok = $this->ipcProvider->CASHash($key, $compKey, $rawdata, json_encode($data)); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | // delete keys of data and do CAS on the $compKey |
||
| 210 | elseif ($doCas == "deletekeys") { |
||
| 211 | $okCount = 0; |
||
| 212 | // TODO: make this configurable (retrycount)? |
||
| 213 | while (!$ok && $okCount < 5) { |
||
| 214 | // step 1: get current data |
||
| 215 | $_rawdata = $this->ipcProvider->get()->hget($key, $compKey); |
||
| 216 | $newData = json_decode((string) $_rawdata, true); |
||
| 217 | # no data to delete from, done |
||
| 218 | if (!is_array($newData) || count($newData) == 0) { |
||
| 219 | break; |
||
| 220 | } |
||
| 221 | // step 2: if data exists, delete the keys of $data from it |
||
| 222 | foreach ($data as $delKey) { |
||
| 223 | unset($newData[$delKey]); |
||
| 224 | } |
||
| 225 | $rawNewData = json_encode($newData); |
||
| 226 | // step 3: check if the data actually changed (not the correctest way to compare these, but still valid for our data) |
||
| 227 | if ($_rawdata == $rawNewData) { |
||
| 228 | break; |
||
| 229 | } |
||
| 230 | // step 4: replace old with new data |
||
| 231 | $ok = $this->ipcProvider->CASHash($key, $compKey, $_rawdata, $rawNewData); |
||
| 232 | if (!$ok) { |
||
| 233 | ++$okCount; |
||
| 234 | // TODO: make this configurable? |
||
| 235 | // retry in 0.1s |
||
| 236 | usleep(100000); |
||
| 237 | SLog::Write(LOGLEVEL_DEBUG, "InterProcessData: setDeviceUserData CAS failed, retrying..."); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | return $ok; |
||
| 243 | } |
||
| 320 |