Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
4.35% |
1 / 23 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
| AbstractFileExporter | |
4.35% |
1 / 23 |
|
14.29% |
1 / 7 |
160.90 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| export | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| download | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| preparePsrResponse | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| legacyEmitResponse | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| flushOutputBuffer | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| emitHeaders | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| emitBody | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 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; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\export\implementation; |
| 23 | |
| 24 | use oat\tao\model\export\Exporter; |
| 25 | use Psr\Http\Message\ResponseInterface; |
| 26 | |
| 27 | /** |
| 28 | * @package oat\tao\model\export |
| 29 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
| 30 | */ |
| 31 | abstract class AbstractFileExporter implements Exporter |
| 32 | { |
| 33 | /** |
| 34 | * @var string value of `Content-Type` header |
| 35 | */ |
| 36 | protected $contentType = 'text/plain; charset=UTF-8'; |
| 37 | |
| 38 | /** |
| 39 | * @var mixed Data to be exported |
| 40 | */ |
| 41 | protected $data; |
| 42 | |
| 43 | /** |
| 44 | * @param array $data Data to be exported |
| 45 | */ |
| 46 | public function __construct($data) |
| 47 | { |
| 48 | $this->data = $data; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Export data as string |
| 53 | * @return string |
| 54 | */ |
| 55 | abstract public function export(); |
| 56 | |
| 57 | /** |
| 58 | * Send exported data to end user |
| 59 | * @deprecated implement PsrResponseExporter to get Psr Response |
| 60 | * @see CsvExporter |
| 61 | * @param string $data Data to be exported |
| 62 | * @param string|null $fileName |
| 63 | */ |
| 64 | protected function download($data, $fileName = null) |
| 65 | { |
| 66 | $response = $this->preparePsrResponse(new \GuzzleHttp\Psr7\Response(), $data, $fileName); |
| 67 | $this->legacyEmitResponse($response); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param ResponseInterface $response |
| 72 | * @param string $data Data to be exported |
| 73 | * @param string|null $fileName if null timestamp will be used as file name |
| 74 | * @return ResponseInterface |
| 75 | */ |
| 76 | protected function preparePsrResponse(ResponseInterface $response, $data, $fileName = null) |
| 77 | { |
| 78 | if ($fileName === null) { |
| 79 | $fileName = (string) time(); |
| 80 | } |
| 81 | |
| 82 | return $response |
| 83 | ->withHeader('Content-Type', $this->contentType) |
| 84 | ->withHeader('Content-Disposition', 'attachment; fileName="' . $fileName . '"') |
| 85 | ->withHeader('Content-Length', strlen($data)) |
| 86 | ->withBody(\GuzzleHttp\Psr7\stream_for($data)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Implements old logic which writes headers and data directly to output |
| 91 | * @deprecated Responses should be emitted in a centralized way using ResponseEmitter |
| 92 | * @param ResponseInterface $response |
| 93 | */ |
| 94 | private function legacyEmitResponse(ResponseInterface $response) |
| 95 | { |
| 96 | $this->flushOutputBuffer(); |
| 97 | $this->emitHeaders($response); |
| 98 | $this->emitBody($response); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @deprecated |
| 103 | */ |
| 104 | private function flushOutputBuffer() |
| 105 | { |
| 106 | while (ob_get_level() > 0) { |
| 107 | ob_end_flush(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @deprecated |
| 113 | * @param ResponseInterface $response |
| 114 | */ |
| 115 | private function emitHeaders(ResponseInterface $response) |
| 116 | { |
| 117 | foreach ($response->getHeaders() as $name => $values) { |
| 118 | foreach ($values as $value) { |
| 119 | header("$name: $value"); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @deprecated |
| 126 | * @param ResponseInterface $response |
| 127 | */ |
| 128 | private function emitBody(ResponseInterface $response) |
| 129 | { |
| 130 | $stream = $response->getBody(); |
| 131 | if ($stream->isSeekable()) { |
| 132 | $stream->rewind(); |
| 133 | } |
| 134 | while (!$stream->eof()) { |
| 135 | echo $stream->read(1024 * 8); |
| 136 | } |
| 137 | } |
| 138 | } |