Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
62.50% |
20 / 32 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| StreamRange | |
62.50% |
20 / 32 |
|
50.00% |
2 / 4 |
32.24 | |
0.00% |
0 / 1 |
| __construct | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
9.01 | |||
| createFromRequest | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
| getFirstPos | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLastPos | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\stream; |
| 23 | |
| 24 | use Psr\Http\Message\StreamInterface; |
| 25 | use Psr\Http\Message\ServerRequestInterface; |
| 26 | |
| 27 | /** |
| 28 | * Class StreamRange |
| 29 | * @package oat\tao\model\stream |
| 30 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
| 31 | */ |
| 32 | class StreamRange |
| 33 | { |
| 34 | /** |
| 35 | * @var integer |
| 36 | */ |
| 37 | private $firstPos; |
| 38 | |
| 39 | /** |
| 40 | * @var integer |
| 41 | */ |
| 42 | private $lastPos; |
| 43 | |
| 44 | /** |
| 45 | * StreamRange constructor. |
| 46 | * @param StreamInterface $stream |
| 47 | * @param string $range |
| 48 | * @throws |
| 49 | */ |
| 50 | public function __construct(StreamInterface $stream, $range) |
| 51 | { |
| 52 | $range = trim($range); |
| 53 | $length = $stream->getSize(); |
| 54 | if (preg_match('/^(\d+)\-$/', $range, $match)) { |
| 55 | $this->firstPos = intval($match[1]); |
| 56 | |
| 57 | if ($this->firstPos > ($length - 1)) { |
| 58 | throw new StreamRangeException('HTTP/1.1 416 Requested Range Not Satisfiable'); |
| 59 | } |
| 60 | $this->lastPos = $length - 1; |
| 61 | } elseif (preg_match('/^(\d+)\-(\d+)$/', $range, $match)) { |
| 62 | $this->firstPos = intval($match[1]); |
| 63 | $this->lastPos = intval($match[2]); |
| 64 | |
| 65 | if ($this->lastPos < $this->firstPos || $this->lastPos > ($length - 1)) { |
| 66 | throw new StreamRangeException('HTTP/1.1 416 Requested Range Not Satisfiable'); |
| 67 | } |
| 68 | } elseif (preg_match('/^\-(\d+)$/', $range, $match)) { |
| 69 | $suffixLength = intval($match[1]); |
| 70 | |
| 71 | if ($suffixLength === 0 || $suffixLength > $length) { |
| 72 | throw new StreamRangeException('HTTP/1.1 416 Requested Range Not Satisfiable'); |
| 73 | } |
| 74 | |
| 75 | $this->firstPos = $length - $suffixLength; |
| 76 | $this->lastPos = $length - 1; |
| 77 | } else { |
| 78 | throw new StreamRangeException('HTTP/1.1 416 Requested Range Not Satisfiable'); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Create array of StreamRange instances based on current request range headers |
| 84 | * @param StreamInterface $stream |
| 85 | * @param ServerRequestInterface $request |
| 86 | * @throws StreamRangeException |
| 87 | * @return StreamRange[] |
| 88 | */ |
| 89 | public static function createFromRequest(StreamInterface $stream, ServerRequestInterface $request = null) |
| 90 | { |
| 91 | $result = []; |
| 92 | if ($request === null) { |
| 93 | $headers = \tao_helpers_Http::getHeaders(); |
| 94 | $rangeHeader = isset($headers['Range']) ? [$headers['Range']] : null; |
| 95 | } else { |
| 96 | $rangeHeader = $request->hasHeader('Range') ? $request->getHeader('Range') : null; |
| 97 | } |
| 98 | if ($rangeHeader) { |
| 99 | $ranges = explode(',', $rangeHeader[0]); |
| 100 | foreach ($ranges as $range) { |
| 101 | $range = str_replace('bytes=', '', $range); |
| 102 | $result[] = new StreamRange($stream, $range); |
| 103 | } |
| 104 | } |
| 105 | return $result; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return int |
| 110 | */ |
| 111 | public function getFirstPos() |
| 112 | { |
| 113 | return $this->firstPos; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return int |
| 118 | */ |
| 119 | public function getLastPos() |
| 120 | { |
| 121 | return $this->lastPos; |
| 122 | } |
| 123 | } |