Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
60.42% |
29 / 48 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Download | |
60.42% |
29 / 48 |
|
80.00% |
4 / 5 |
14.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| writeDownload | |
36.67% |
11 / 30 |
|
0.00% |
0 / 1 |
8.06 | |||
| getSpreadSheet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getWriter | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| setSpreadSheet | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * @package zmsstatistic |
| 6 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace BO\Zmsstatistic\Helper; |
| 11 | |
| 12 | use Fig\Http\Message\StatusCodeInterface; |
| 13 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 14 | use PhpOffice\PhpSpreadsheet\IOFactory; |
| 15 | use Psr\Http\Message\ResponseInterface; |
| 16 | |
| 17 | class Download |
| 18 | { |
| 19 | protected mixed $writer = null; |
| 20 | |
| 21 | protected mixed $spreadsheet = null; |
| 22 | |
| 23 | protected string $period = ''; |
| 24 | |
| 25 | protected string $title = 'statistik'; |
| 26 | |
| 27 | protected mixed $type = 'xlsx'; |
| 28 | |
| 29 | public function __construct(mixed $request) |
| 30 | { |
| 31 | $validator = $request->getAttribute('validator'); |
| 32 | $this->type = $validator->getParameter('type')->isString()->setDefault('xlsx')->getValue(); |
| 33 | } |
| 34 | |
| 35 | public function writeDownload(ResponseInterface $response): mixed |
| 36 | { |
| 37 | $resource = fopen('php://temp', 'x+'); |
| 38 | if ($resource === false) { |
| 39 | \App::$log->error('Failed to open temporary stream for spreadsheet download', [ |
| 40 | 'event' => 'statistic_download_failed', |
| 41 | 'reason' => 'fopen', |
| 42 | ]); |
| 43 | return $response->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); |
| 44 | } |
| 45 | |
| 46 | try { |
| 47 | $this->getWriter()->save($resource); |
| 48 | rewind($resource); |
| 49 | $contents = stream_get_contents($resource); |
| 50 | if ($contents === false) { |
| 51 | fclose($resource); |
| 52 | \App::$log->error('Failed to read spreadsheet download stream', [ |
| 53 | 'event' => 'statistic_download_failed', |
| 54 | 'reason' => 'stream_get_contents', |
| 55 | ]); |
| 56 | return $response->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); |
| 57 | } |
| 58 | $response->getBody()->write($contents); |
| 59 | } catch (\Exception $e) { |
| 60 | fclose($resource); |
| 61 | \App::$log->error('Failed to write spreadsheet download', [ |
| 62 | 'event' => 'statistic_download_failed', |
| 63 | 'reason' => 'exception', |
| 64 | 'exception' => $e, |
| 65 | ]); |
| 66 | |
| 67 | return $response->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); |
| 68 | } |
| 69 | |
| 70 | fclose($resource); |
| 71 | |
| 72 | return $response |
| 73 | ->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') |
| 74 | ->withHeader('Content-Disposition', sprintf('attachment; filename="%s.%s"', $this->title, $this->type)); |
| 75 | } |
| 76 | |
| 77 | public function getSpreadSheet(): mixed |
| 78 | { |
| 79 | return $this->spreadsheet; |
| 80 | } |
| 81 | |
| 82 | public function getWriter(): mixed |
| 83 | { |
| 84 | if ('xlsx' == $this->type) { |
| 85 | $this->writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx'); |
| 86 | } |
| 87 | return $this->writer; |
| 88 | } |
| 89 | |
| 90 | public function setSpreadSheet( |
| 91 | string $title = 'statistic', |
| 92 | mixed $creator = 'berlinonline', |
| 93 | mixed $subject = '', |
| 94 | mixed $description = 'statistic document', |
| 95 | mixed $keywords = 'statistic zms' |
| 96 | ): static { |
| 97 | $this->title = $title; |
| 98 | $this->spreadsheet = new Spreadsheet(); |
| 99 | $this->spreadsheet |
| 100 | ->getProperties() |
| 101 | ->setCreator($creator) |
| 102 | ->setLastModifiedBy($creator) |
| 103 | ->setTitle($title) |
| 104 | ->setSubject($subject) |
| 105 | ->setDescription($description) |
| 106 | ->setKeywords($keywords) |
| 107 | ->setCategory($subject); |
| 108 | return $this; |
| 109 | } |
| 110 | } |