Completed
Branch master (919a8c)
by Filippo
05:19 queued 03:14
created
src/EoC/Adapter/SocketAdapter.php 1 patch
Braces   +46 added lines, -32 removed lines patch added patch discarded remove patch
@@ -56,13 +56,15 @@  discard block
 block discarded – undo
56 56
     $this->timeout = static::$defaultSocketTimeout;
57 57
 
58 58
     // Establishes a connection within the server.
59
-    if ($persistent)
60
-      $this->handle = @pfsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout);
61
-    else
62
-      $this->handle = @fsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout);
59
+    if ($persistent) {
60
+          $this->handle = @pfsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout);
61
+    } else {
62
+          $this->handle = @fsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout);
63
+    }
63 64
 
64
-    if (!is_resource($this->handle))
65
-      throw new \ErrorException($errstr, $errno);
65
+    if (!is_resource($this->handle)) {
66
+          throw new \ErrorException($errstr, $errno);
67
+    }
66 68
   }
67 69
 
68 70
 
@@ -123,8 +125,9 @@  discard block
 block discarded – undo
123 125
       $statusCodeAndHeader .= $buffer;
124 126
 
125 127
       // The header is separated from the body by a newline, so we break when we read it.
126
-      if ($buffer == Message::CRLF)
127
-        break;
128
+      if ($buffer == Message::CRLF) {
129
+              break;
130
+      }
128 131
     }
129 132
 
130 133
     return $statusCodeAndHeader;
@@ -144,18 +147,21 @@  discard block
 block discarded – undo
144 147
 
145 148
       // If it's only a newline, this normally means it's read the total amount of data requested minus the newline
146 149
       // continue to next loop to make sure we're done.
147
-      if ($line == Message::CRLF)
148
-        continue;
150
+      if ($line == Message::CRLF) {
151
+              continue;
152
+      }
149 153
 
150 154
       // The length of the block is expressed in hexadecimal.
151 155
       $length = hexdec($line);
152 156
 
153
-      if (!is_int($length))
154
-        throw new \RuntimeException("The response doesn't seem chunk encoded.");
157
+      if (!is_int($length)) {
158
+              throw new \RuntimeException("The response doesn't seem chunk encoded.");
159
+      }
155 160
 
156 161
       // Zero is sent when at the end of the chunks or the end of the stream.
157
-      if ($length < 1)
158
-        break;
162
+      if ($length < 1) {
163
+              break;
164
+      }
159 165
 
160 166
       // Reads the chunk.
161 167
       // When reading from network streams or pipes, such as those returned when reading remote files or from popen()
@@ -168,18 +174,21 @@  discard block
 block discarded – undo
168 174
         $size = min(self::BUFFER_LENGTH, $length);
169 175
         $data = fread($this->handle, $size);
170 176
 
171
-        if (strlen($data) == 0)
172
-          break; // EOF
177
+        if (strlen($data) == 0) {
178
+                  break;
179
+        }
180
+        // EOF
173 181
 
174 182
         $buffer .= $data;
175 183
         $length -= strlen($data);
176 184
       }
177 185
 
178 186
       // If a function has been hooked, calls it, else just adds the buffer to the body.
179
-      if (is_null($chunkHook))
180
-        $body .= $buffer;
181
-      else
182
-        $chunkHook->process($buffer);
187
+      if (is_null($chunkHook)) {
188
+              $body .= $buffer;
189
+      } else {
190
+              $chunkHook->process($buffer);
191
+      }
183 192
     }
184 193
 
185 194
     // A chunk response might have some footer, but CouchDB doesn't use them, so we simply ignore them.
@@ -188,8 +197,9 @@  discard block
 block discarded – undo
188 197
       $buffer = fgets($this->handle, self::BUFFER_LENGTH);
189 198
 
190 199
       // The chunk response ends with a newline, so we break when we read it.
191
-      if ($buffer == Message::CRLF)
192
-        break;
200
+      if ($buffer == Message::CRLF) {
201
+              break;
202
+      }
193 203
     }
194 204
 
195 205
     return $body;
@@ -216,8 +226,9 @@  discard block
 block discarded – undo
216 226
         $body .= $buffer;
217 227
         $bytes += strlen($buffer);
218 228
 
219
-        if ($bytes >= $length)
220
-          break;
229
+        if ($bytes >= $length) {
230
+                  break;
231
+        }
221 232
       }
222 233
     }
223 234
 
@@ -232,10 +243,11 @@  discard block
 block discarded – undo
232 243
    * @return string
233 244
    */
234 245
   protected function readResponseBody(Response $response, $chunkHook) {
235
-    if ($response->getHeaderFieldValue(Response::TRANSFER_ENCODING_HF) == "chunked")
236
-      return $this->readChunkedResponseBody($chunkHook);
237
-    else
238
-      return $this->readStandardResponseBody($response);
246
+    if ($response->getHeaderFieldValue(Response::TRANSFER_ENCODING_HF) == "chunked") {
247
+          return $this->readChunkedResponseBody($chunkHook);
248
+    } else {
249
+          return $this->readStandardResponseBody($response);
250
+    }
239 251
   }
240 252
 
241 253
 
@@ -245,12 +257,14 @@  discard block
 block discarded – undo
245 257
   public function send(Request $request, IChunkHook $chunkHook = NULL) {
246 258
     $request->setHeaderField(Request::HOST_HF, $this->host.":".$this->port);
247 259
 
248
-    if (!empty($this->userName))
249
-      $request->setBasicAuth($this->userName, $this->password);
260
+    if (!empty($this->userName)) {
261
+          $request->setBasicAuth($this->userName, $this->password);
262
+    }
250 263
 
251 264
     // Sets the Content-Length header only when the given request has a message body.
252
-    if ($request->hasBody())
253
-      $request->setHeaderField(Message::CONTENT_LENGTH_HF, $request->getBodyLength());
265
+    if ($request->hasBody()) {
266
+          $request->setHeaderField(Message::CONTENT_LENGTH_HF, $request->getBodyLength());
267
+    }
254 268
 
255 269
     // Writes the request over the socket.
256 270
     $this->writeRequest($request);
Please login to merge, or discard this patch.