@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | public function generate() |
| 117 | 117 | { |
| 118 | - $t = (int) floor($this->timer->getUnixTimestamp() - $this->epoch); |
|
| 118 | + $t = (int) floor($this->timer->getUnixTimestamp()-$this->epoch); |
|
| 119 | 119 | if ($t !== $this->lastTime) { |
| 120 | 120 | $this->generateWithDifferentTime($t); |
| 121 | 121 | } else { |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | if ($t < $this->lastTime) { |
| 153 | 153 | throw new UnexpectedValueException( |
| 154 | 154 | 'Time moved backwards. We cannot generate IDs for ' |
| 155 | - .($this->lastTime - $t).' milliseconds' |
|
| 155 | + .($this->lastTime-$t).' milliseconds' |
|
| 156 | 156 | ); |
| 157 | 157 | } elseif ($t < 0) { |
| 158 | 158 | throw new UnexpectedValueException( |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | private function hexdec($hex) |
| 222 | 222 | { |
| 223 | 223 | $dec = 0; |
| 224 | - for ($i = strlen($hex) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) { |
|
| 224 | + for ($i = strlen($hex)-1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) { |
|
| 225 | 225 | $factor = self::$hexdec[$hex[$i]]; |
| 226 | 226 | $dec = bcadd($dec, bcmul($factor, $e)); |
| 227 | 227 | } |
@@ -25,6 +25,9 @@ discard block |
||
| 25 | 25 | $this->apiPrefix = $apiPrefix; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | + /** |
|
| 29 | + * @param string $url |
|
| 30 | + */ |
|
| 28 | 31 | public function performGetRequest($url) |
| 29 | 32 | { |
| 30 | 33 | $curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url; |
@@ -35,6 +38,9 @@ discard block |
||
| 35 | 38 | return $returnData; |
| 36 | 39 | } |
| 37 | 40 | |
| 41 | + /** |
|
| 42 | + * @param string $url |
|
| 43 | + */ |
|
| 38 | 44 | public function performPutRequest($url, $payload = null) |
| 39 | 45 | { |
| 40 | 46 | $curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url; |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | public function performGetRequest($url) |
| 29 | 29 | { |
| 30 | - $curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url; |
|
| 30 | + $curlUrl = $this->consulBaseUrl.$this->apiPrefix.$url; |
|
| 31 | 31 | $ch = curl_init($curlUrl); |
| 32 | 32 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 33 | 33 | $result = curl_exec($ch); |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | public function performPutRequest($url, $payload = null) |
| 39 | 39 | { |
| 40 | - $curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url; |
|
| 40 | + $curlUrl = $this->consulBaseUrl.$this->apiPrefix.$url; |
|
| 41 | 41 | $ch = curl_init($curlUrl); |
| 42 | 42 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
| 43 | 43 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | curl_setopt($ch, CURLOPT_HTTPHEADER, |
| 50 | 50 | array( |
| 51 | 51 | 'Content-Type: application/json', |
| 52 | - 'Content-Length: ' . strlen($payload)) |
|
| 52 | + 'Content-Length: '.strlen($payload)) |
|
| 53 | 53 | ); |
| 54 | 54 | $result = curl_exec($ch); |
| 55 | 55 | $returnData = json_decode($result, true); |
@@ -147,8 +147,7 @@ |
||
| 147 | 147 | continue; |
| 148 | 148 | } elseif ($currentValue['Key'] == $this->sessionId) { |
| 149 | 149 | return base64_decode($currentValue['Value']); |
| 150 | - } |
|
| 151 | - else { |
|
| 150 | + } else { |
|
| 152 | 151 | $usedIds[] = base64_decode($currentValue['Value']); |
| 153 | 152 | } |
| 154 | 153 | } |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | { |
| 63 | 63 | $this->curl = $curl; |
| 64 | 64 | $this->kvPrefix = $kvPrefix; |
| 65 | - $this->sessionTTL = (int)$sessionTTL; |
|
| 65 | + $this->sessionTTL = (int) $sessionTTL; |
|
| 66 | 66 | //If we cannot connect to Consul on start, we have a problem. |
| 67 | 67 | $this->createSession(); |
| 68 | 68 | $this->lastSuccessfullCheck = time(); |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | public function heartbeat() |
| 100 | 100 | { |
| 101 | 101 | //If we have last successfull check recently new, we don't have to do anything |
| 102 | - if ($this->lastSuccessfullCheck !== null && time() - $this->lastSuccessfullCheck < $this->sessionTTL / 2 ) { |
|
| 102 | + if ($this->lastSuccessfullCheck !== null && time()-$this->lastSuccessfullCheck < $this->sessionTTL / 2) { |
|
| 103 | 103 | return false; |
| 104 | 104 | } |
| 105 | 105 | //If session reneval succeedes, update last successfull check. |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | } catch (RuntimeException $e) { |
| 117 | 117 | //We could not create new session. We can work for some time in 'detached' mode, |
| 118 | 118 | //but if our TTL time runs out, we have to throw an exception. |
| 119 | - if ($this->lastSuccessfullCheck === null || time() - $this->lastSuccessfullCheck >= $this->sessionTTL) { |
|
| 119 | + if ($this->lastSuccessfullCheck === null || time()-$this->lastSuccessfullCheck >= $this->sessionTTL) { |
|
| 120 | 120 | throw $e; |
| 121 | 121 | } |
| 122 | 122 | return false; |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | //Check, if we don't have existing value for the session |
| 135 | 135 | $currentValue = $this->curl->performGetRequest('/kv/'.$this->kvPrefix.$this->sessionId); |
| 136 | 136 | if (!empty($currentValue['Value'])) { |
| 137 | - return (int)base64_decode($currentValue['Value']); |
|
| 137 | + return (int) base64_decode($currentValue['Value']); |
|
| 138 | 138 | } |
| 139 | 139 | //Lock main key to block concurrent checks |
| 140 | 140 | $this->lockKey(); |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | } |
| 150 | 150 | //Release the lock on the main key and return machine ID. |
| 151 | 151 | $this->releaseKey(); |
| 152 | - return (int)$machineId; |
|
| 152 | + return (int) $machineId; |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
@@ -198,12 +198,12 @@ discard block |
||
| 198 | 198 | |
| 199 | 199 | private function createSession() |
| 200 | 200 | { |
| 201 | - $url ='/session/create'; |
|
| 201 | + $url = '/session/create'; |
|
| 202 | 202 | //We create new session with given TTL and with lock delay equal to half of TTL. |
| 203 | 203 | $payload = array( |
| 204 | 204 | 'TTL' => $this->sessionTTL.'s', |
| 205 | 205 | "Behavior" => "delete", |
| 206 | - 'LockDelay' => floor($this->sessionTTL/2).'s', |
|
| 206 | + 'LockDelay' => floor($this->sessionTTL / 2).'s', |
|
| 207 | 207 | ); |
| 208 | 208 | $returnData = $this->curl->performPutRequest($url, json_encode($payload)); |
| 209 | 209 | if (empty($returnData['ID'])) { |