Passed
Push — master ( 50b7a6...31c381 )
by Morris
17:10 queued 10s
created
apps/dav/lib/Upload/AssemblyStream.php 1 patch
Indentation   +200 added lines, -200 removed lines patch added patch discarded remove patch
@@ -39,230 +39,230 @@
 block discarded – undo
39 39
  */
40 40
 class AssemblyStream implements \Icewind\Streams\File {
41 41
 
42
-	/** @var resource */
43
-	private $context;
42
+    /** @var resource */
43
+    private $context;
44 44
 
45
-	/** @var IFile[] */
46
-	private $nodes;
45
+    /** @var IFile[] */
46
+    private $nodes;
47 47
 
48
-	/** @var int */
49
-	private $pos = 0;
48
+    /** @var int */
49
+    private $pos = 0;
50 50
 
51
-	/** @var int */
52
-	private $size = 0;
51
+    /** @var int */
52
+    private $size = 0;
53 53
 
54
-	/** @var resource */
55
-	private $currentStream = null;
54
+    /** @var resource */
55
+    private $currentStream = null;
56 56
 
57
-	/** @var int */
58
-	private $currentNode = 0;
57
+    /** @var int */
58
+    private $currentNode = 0;
59 59
 
60
-	/** @var int */
61
-	private $currentNodeRead = 0;
60
+    /** @var int */
61
+    private $currentNodeRead = 0;
62 62
 
63
-	/**
64
-	 * @param string $path
65
-	 * @param string $mode
66
-	 * @param int $options
67
-	 * @param string &$opened_path
68
-	 * @return bool
69
-	 */
70
-	public function stream_open($path, $mode, $options, &$opened_path) {
71
-		$this->loadContext('assembly');
63
+    /**
64
+     * @param string $path
65
+     * @param string $mode
66
+     * @param int $options
67
+     * @param string &$opened_path
68
+     * @return bool
69
+     */
70
+    public function stream_open($path, $mode, $options, &$opened_path) {
71
+        $this->loadContext('assembly');
72 72
 
73
-		$nodes = $this->nodes;
74
-		// http://stackoverflow.com/a/10985500
75
-		@usort($nodes, function (IFile $a, IFile $b) {
76
-			return strnatcmp($a->getName(), $b->getName());
77
-		});
78
-		$this->nodes = array_values($nodes);
79
-		$this->size = array_reduce($this->nodes, function ($size, IFile $file) {
80
-			return $size + $file->getSize();
81
-		}, 0);
82
-		return true;
83
-	}
73
+        $nodes = $this->nodes;
74
+        // http://stackoverflow.com/a/10985500
75
+        @usort($nodes, function (IFile $a, IFile $b) {
76
+            return strnatcmp($a->getName(), $b->getName());
77
+        });
78
+        $this->nodes = array_values($nodes);
79
+        $this->size = array_reduce($this->nodes, function ($size, IFile $file) {
80
+            return $size + $file->getSize();
81
+        }, 0);
82
+        return true;
83
+    }
84 84
 
85
-	/**
86
-	 * @param string $offset
87
-	 * @param int $whence
88
-	 * @return bool
89
-	 */
90
-	public function stream_seek($offset, $whence = SEEK_SET) {
91
-		return false;
92
-	}
85
+    /**
86
+     * @param string $offset
87
+     * @param int $whence
88
+     * @return bool
89
+     */
90
+    public function stream_seek($offset, $whence = SEEK_SET) {
91
+        return false;
92
+    }
93 93
 
94
-	/**
95
-	 * @return int
96
-	 */
97
-	public function stream_tell() {
98
-		return $this->pos;
99
-	}
94
+    /**
95
+     * @return int
96
+     */
97
+    public function stream_tell() {
98
+        return $this->pos;
99
+    }
100 100
 
101
-	/**
102
-	 * @param int $count
103
-	 * @return string
104
-	 */
105
-	public function stream_read($count) {
106
-		if (is_null($this->currentStream)) {
107
-			if ($this->currentNode < count($this->nodes)) {
108
-				$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
109
-			} else {
110
-				return '';
111
-			}
112
-		}
101
+    /**
102
+     * @param int $count
103
+     * @return string
104
+     */
105
+    public function stream_read($count) {
106
+        if (is_null($this->currentStream)) {
107
+            if ($this->currentNode < count($this->nodes)) {
108
+                $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
109
+            } else {
110
+                return '';
111
+            }
112
+        }
113 113
 
114
-		do {
115
-			$data = fread($this->currentStream, $count);
116
-			$read = strlen($data);
117
-			$this->currentNodeRead += $read;
114
+        do {
115
+            $data = fread($this->currentStream, $count);
116
+            $read = strlen($data);
117
+            $this->currentNodeRead += $read;
118 118
 
119
-			if (feof($this->currentStream)) {
120
-				fclose($this->currentStream);
121
-				$currentNodeSize = $this->nodes[$this->currentNode]->getSize();
122
-				if ($this->currentNodeRead < $currentNodeSize) {
123
-					throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
124
-				}
125
-				$this->currentNode++;
126
-				$this->currentNodeRead = 0;
127
-				if ($this->currentNode < count($this->nodes)) {
128
-					$this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
129
-				} else {
130
-					$this->currentStream = null;
131
-				}
132
-			}
133
-			// if no data read, try again with the next node because
134
-			// returning empty data can make the caller think there is no more
135
-			// data left to read
136
-		} while ($read === 0 && !is_null($this->currentStream));
119
+            if (feof($this->currentStream)) {
120
+                fclose($this->currentStream);
121
+                $currentNodeSize = $this->nodes[$this->currentNode]->getSize();
122
+                if ($this->currentNodeRead < $currentNodeSize) {
123
+                    throw new \Exception('Stream from assembly node shorter than expected, got ' . $this->currentNodeRead . ' bytes, expected ' . $currentNodeSize);
124
+                }
125
+                $this->currentNode++;
126
+                $this->currentNodeRead = 0;
127
+                if ($this->currentNode < count($this->nodes)) {
128
+                    $this->currentStream = $this->getStream($this->nodes[$this->currentNode]);
129
+                } else {
130
+                    $this->currentStream = null;
131
+                }
132
+            }
133
+            // if no data read, try again with the next node because
134
+            // returning empty data can make the caller think there is no more
135
+            // data left to read
136
+        } while ($read === 0 && !is_null($this->currentStream));
137 137
 
138
-		// update position
139
-		$this->pos += $read;
140
-		return $data;
141
-	}
138
+        // update position
139
+        $this->pos += $read;
140
+        return $data;
141
+    }
142 142
 
143
-	/**
144
-	 * @param string $data
145
-	 * @return int
146
-	 */
147
-	public function stream_write($data) {
148
-		return false;
149
-	}
143
+    /**
144
+     * @param string $data
145
+     * @return int
146
+     */
147
+    public function stream_write($data) {
148
+        return false;
149
+    }
150 150
 
151
-	/**
152
-	 * @param int $option
153
-	 * @param int $arg1
154
-	 * @param int $arg2
155
-	 * @return bool
156
-	 */
157
-	public function stream_set_option($option, $arg1, $arg2) {
158
-		return false;
159
-	}
151
+    /**
152
+     * @param int $option
153
+     * @param int $arg1
154
+     * @param int $arg2
155
+     * @return bool
156
+     */
157
+    public function stream_set_option($option, $arg1, $arg2) {
158
+        return false;
159
+    }
160 160
 
161
-	/**
162
-	 * @param int $size
163
-	 * @return bool
164
-	 */
165
-	public function stream_truncate($size) {
166
-		return false;
167
-	}
161
+    /**
162
+     * @param int $size
163
+     * @return bool
164
+     */
165
+    public function stream_truncate($size) {
166
+        return false;
167
+    }
168 168
 
169
-	/**
170
-	 * @return array
171
-	 */
172
-	public function stream_stat() {
173
-		return [
174
-			'size' => $this->size,
175
-		];
176
-	}
169
+    /**
170
+     * @return array
171
+     */
172
+    public function stream_stat() {
173
+        return [
174
+            'size' => $this->size,
175
+        ];
176
+    }
177 177
 
178
-	/**
179
-	 * @param int $operation
180
-	 * @return bool
181
-	 */
182
-	public function stream_lock($operation) {
183
-		return false;
184
-	}
178
+    /**
179
+     * @param int $operation
180
+     * @return bool
181
+     */
182
+    public function stream_lock($operation) {
183
+        return false;
184
+    }
185 185
 
186
-	/**
187
-	 * @return bool
188
-	 */
189
-	public function stream_flush() {
190
-		return false;
191
-	}
186
+    /**
187
+     * @return bool
188
+     */
189
+    public function stream_flush() {
190
+        return false;
191
+    }
192 192
 
193
-	/**
194
-	 * @return bool
195
-	 */
196
-	public function stream_eof() {
197
-		return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null);
198
-	}
193
+    /**
194
+     * @return bool
195
+     */
196
+    public function stream_eof() {
197
+        return $this->pos >= $this->size || ($this->currentNode >= count($this->nodes) && $this->currentNode === null);
198
+    }
199 199
 
200
-	/**
201
-	 * @return bool
202
-	 */
203
-	public function stream_close() {
204
-		return true;
205
-	}
200
+    /**
201
+     * @return bool
202
+     */
203
+    public function stream_close() {
204
+        return true;
205
+    }
206 206
 
207 207
 
208
-	/**
209
-	 * Load the source from the stream context and return the context options
210
-	 *
211
-	 * @param string $name
212
-	 * @return array
213
-	 * @throws \Exception
214
-	 */
215
-	protected function loadContext($name) {
216
-		$context = stream_context_get_options($this->context);
217
-		if (isset($context[$name])) {
218
-			$context = $context[$name];
219
-		} else {
220
-			throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
221
-		}
222
-		if (isset($context['nodes']) and is_array($context['nodes'])) {
223
-			$this->nodes = $context['nodes'];
224
-		} else {
225
-			throw new \BadMethodCallException('Invalid context, nodes not set');
226
-		}
227
-		return $context;
228
-	}
208
+    /**
209
+     * Load the source from the stream context and return the context options
210
+     *
211
+     * @param string $name
212
+     * @return array
213
+     * @throws \Exception
214
+     */
215
+    protected function loadContext($name) {
216
+        $context = stream_context_get_options($this->context);
217
+        if (isset($context[$name])) {
218
+            $context = $context[$name];
219
+        } else {
220
+            throw new \BadMethodCallException('Invalid context, "' . $name . '" options not set');
221
+        }
222
+        if (isset($context['nodes']) and is_array($context['nodes'])) {
223
+            $this->nodes = $context['nodes'];
224
+        } else {
225
+            throw new \BadMethodCallException('Invalid context, nodes not set');
226
+        }
227
+        return $context;
228
+    }
229 229
 
230
-	/**
231
-	 * @param IFile[] $nodes
232
-	 * @return resource
233
-	 *
234
-	 * @throws \BadMethodCallException
235
-	 */
236
-	public static function wrap(array $nodes) {
237
-		$context = stream_context_create([
238
-			'assembly' => [
239
-				'nodes' => $nodes
240
-			]
241
-		]);
242
-		stream_wrapper_register('assembly', self::class);
243
-		try {
244
-			$wrapped = fopen('assembly://', 'r', null, $context);
245
-		} catch (\BadMethodCallException $e) {
246
-			stream_wrapper_unregister('assembly');
247
-			throw $e;
248
-		}
249
-		stream_wrapper_unregister('assembly');
250
-		return $wrapped;
251
-	}
230
+    /**
231
+     * @param IFile[] $nodes
232
+     * @return resource
233
+     *
234
+     * @throws \BadMethodCallException
235
+     */
236
+    public static function wrap(array $nodes) {
237
+        $context = stream_context_create([
238
+            'assembly' => [
239
+                'nodes' => $nodes
240
+            ]
241
+        ]);
242
+        stream_wrapper_register('assembly', self::class);
243
+        try {
244
+            $wrapped = fopen('assembly://', 'r', null, $context);
245
+        } catch (\BadMethodCallException $e) {
246
+            stream_wrapper_unregister('assembly');
247
+            throw $e;
248
+        }
249
+        stream_wrapper_unregister('assembly');
250
+        return $wrapped;
251
+    }
252 252
 
253
-	/**
254
-	 * @param IFile $node
255
-	 * @return resource
256
-	 */
257
-	private function getStream(IFile $node) {
258
-		$data = $node->get();
259
-		if (is_resource($data)) {
260
-			return $data;
261
-		} else {
262
-			$tmp = fopen('php://temp', 'w+');
263
-			fwrite($tmp, $data);
264
-			rewind($tmp);
265
-			return $tmp;
266
-		}
267
-	}
253
+    /**
254
+     * @param IFile $node
255
+     * @return resource
256
+     */
257
+    private function getStream(IFile $node) {
258
+        $data = $node->get();
259
+        if (is_resource($data)) {
260
+            return $data;
261
+        } else {
262
+            $tmp = fopen('php://temp', 'w+');
263
+            fwrite($tmp, $data);
264
+            rewind($tmp);
265
+            return $tmp;
266
+        }
267
+    }
268 268
 }
Please login to merge, or discard this patch.