Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| LogEntryCsvStdOutExporter | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| export | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| sendHeaders | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| echoContent | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| 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) 2019 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoEventLog\model\export\implementation; |
| 23 | |
| 24 | use oat\taoEventLog\model\export\LogEntryRepositoryInterface; |
| 25 | |
| 26 | class LogEntryCsvStdOutExporter |
| 27 | { |
| 28 | /** |
| 29 | * @var LogEntryRepositoryInterface |
| 30 | */ |
| 31 | private $generator; |
| 32 | /** |
| 33 | * @var string |
| 34 | */ |
| 35 | private $delimiter; |
| 36 | /** |
| 37 | * @var string |
| 38 | */ |
| 39 | private $enclosure; |
| 40 | |
| 41 | /** |
| 42 | * @param LogEntryRepositoryInterface $generator |
| 43 | * @param string $delimiter |
| 44 | * @param string $enclosure |
| 45 | */ |
| 46 | public function __construct(LogEntryRepositoryInterface $generator, $delimiter = ',', $enclosure = '"') |
| 47 | { |
| 48 | $this->generator = $generator; |
| 49 | $this->delimiter = $delimiter; |
| 50 | $this->enclosure = $enclosure; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @return string |
| 55 | */ |
| 56 | public function export() |
| 57 | { |
| 58 | $this->sendHeaders('export.csv'); |
| 59 | $this->echoContent(); |
| 60 | } |
| 61 | |
| 62 | protected function sendHeaders($fileName = null) |
| 63 | { |
| 64 | if ($fileName === null) { |
| 65 | $fileName = (string)time(); |
| 66 | } |
| 67 | |
| 68 | while (ob_get_level() > 0) { |
| 69 | ob_end_flush(); |
| 70 | } |
| 71 | |
| 72 | header('Content-Type: text/plain; charset=UTF-8'); |
| 73 | header('Content-Disposition: attachment; fileName="' . $fileName . '"'); |
| 74 | } |
| 75 | |
| 76 | private function echoContent() |
| 77 | { |
| 78 | $out = fopen('php://output', 'wb'); |
| 79 | if (false === $out) { |
| 80 | throw new \RuntimeException('Can not open stdout for writing'); |
| 81 | } |
| 82 | |
| 83 | foreach ($this->generator->fetch() as $row) { |
| 84 | if (!empty($row)) { |
| 85 | $result = fputcsv($out, $row, $this->delimiter, $this->enclosure); |
| 86 | if (false === $result) { |
| 87 | throw new \RuntimeException('Can not write to stdout'); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | fclose($out); |
| 93 | } |
| 94 | } |