Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| SampleTemplateDownload | |
0.00% |
0 / 36 |
|
0.00% |
0 / 8 |
132 | |
0.00% |
0 / 1 |
| download | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| getCsvContent | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getFileName | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getClassName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMetadataRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplateHeaderParser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplateRepository | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplateSampleLines | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2021 (original work) Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | namespace oat\taoQtiItem\model\import; |
| 25 | |
| 26 | use oat\taoQtiItem\model\import\Repository\CsvTemplateRepository; |
| 27 | use oat\taoQtiItem\model\import\Repository\MetadataRepository; |
| 28 | use oat\taoQtiItem\model\import\Repository\TemplateRepositoryInterface; |
| 29 | use oat\taoQtiItem\model\import\Parser\TemplateHeaderParser; |
| 30 | use oat\taoQtiItem\model\import\Factory\CsvTemplateSampleLineFactory; |
| 31 | use oat\oatbox\service\ConfigurableService; |
| 32 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 33 | use oat\generis\model\OntologyAwareTrait; |
| 34 | use Psr\Http\Message\ServerRequestInterface; |
| 35 | use Psr\Http\Message\ResponseInterface; |
| 36 | use common_exception_Error; |
| 37 | use common_exception_MissingParameter; |
| 38 | |
| 39 | use function GuzzleHttp\Psr7\stream_for; |
| 40 | |
| 41 | class SampleTemplateDownload extends ConfigurableService implements ServiceLocatorAwareInterface |
| 42 | { |
| 43 | use OntologyAwareTrait; |
| 44 | |
| 45 | private const CSV_SEPARATOR = ';'; |
| 46 | |
| 47 | /** |
| 48 | * @throws common_exception_Error |
| 49 | */ |
| 50 | public function download(ServerRequestInterface $request, ResponseInterface $response) |
| 51 | { |
| 52 | $queryParams = $request->getQueryParams(); |
| 53 | if (!$queryParams['uri']) { |
| 54 | throw new common_exception_MissingParameter('uri', __METHOD__); |
| 55 | } |
| 56 | $uri = $queryParams['uri']; |
| 57 | $metaDataArray = $this->getMetadataRepository()->findMetadataByClassUri($uri); |
| 58 | |
| 59 | $template = $this->getTemplateRepository()->findById(CsvTemplateRepository::DEFAULT); |
| 60 | $headers = $this->getTemplateHeaderParser()->parse($template, $metaDataArray); |
| 61 | |
| 62 | $templateSampleLines = $this->getTemplateSampleLines()->createMultiple($template); |
| 63 | |
| 64 | $className = $this->getClassName($uri); |
| 65 | $filename = $this->getFileName($className); |
| 66 | |
| 67 | $csvContent = $this->getCsvContent($headers, $templateSampleLines); |
| 68 | |
| 69 | return $response |
| 70 | ->withHeader('Content-Encoding', 'UTF-8') |
| 71 | ->withHeader('Content-Type', 'text/csv; charset=UTF-8') |
| 72 | ->withHeader("Content-Disposition", "attachment; filename=" . $filename) |
| 73 | ->withBody(stream_for($csvContent)); |
| 74 | } |
| 75 | |
| 76 | private function getCsvContent($headers, $templateSampleLines): string |
| 77 | { |
| 78 | $extraMetadataColumns = str_repeat( |
| 79 | self::CSV_SEPARATOR . '""', |
| 80 | count($headers) - count($templateSampleLines[0]) |
| 81 | ); |
| 82 | $sampleLines = implode(self::CSV_SEPARATOR, $headers) . PHP_EOL; |
| 83 | |
| 84 | foreach ($templateSampleLines as $row) { |
| 85 | foreach ($row as &$column) { |
| 86 | $column = '"' . $column . '"'; |
| 87 | } |
| 88 | |
| 89 | $sampleLines .= implode(self::CSV_SEPARATOR, $row) . $extraMetadataColumns . PHP_EOL; |
| 90 | } |
| 91 | |
| 92 | return $sampleLines; |
| 93 | } |
| 94 | |
| 95 | private function getFileName(string $className): string |
| 96 | { |
| 97 | return 'tabular_template_for_' |
| 98 | . $className |
| 99 | . '_' |
| 100 | . date('YmdHis') . rand(10, 99) |
| 101 | . '.csv'; |
| 102 | } |
| 103 | |
| 104 | public function getClassName(string $uri): string |
| 105 | { |
| 106 | return $this->getClass($uri)->getLabel(); |
| 107 | } |
| 108 | |
| 109 | private function getMetadataRepository(): MetadataRepository |
| 110 | { |
| 111 | return $this->getServiceLocator()->get(MetadataRepository::class); |
| 112 | } |
| 113 | |
| 114 | private function getTemplateHeaderParser(): TemplateHeaderParser |
| 115 | { |
| 116 | return $this->getServiceLocator()->get(TemplateHeaderParser::class); |
| 117 | } |
| 118 | |
| 119 | private function getTemplateRepository(): TemplateRepositoryInterface |
| 120 | { |
| 121 | return $this->getServiceLocator()->get(CsvTemplateRepository::class); |
| 122 | } |
| 123 | |
| 124 | private function getTemplateSampleLines(): CsvTemplateSampleLineFactory |
| 125 | { |
| 126 | return $this->getServiceLocator()->get(CsvTemplateSampleLineFactory::class); |
| 127 | } |
| 128 | } |