Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.43% |
32 / 35 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
Download | |
91.43% |
32 / 35 |
|
80.00% |
4 / 5 |
8.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
writeDownload | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
2.08 | |||
getSpreadSheet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getWriter | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
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 BO\Slim\Response; |
13 | use Fig\Http\Message\StatusCodeInterface; |
14 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
15 | use PhpOffice\PhpSpreadsheet\IOFactory; |
16 | |
17 | class Download |
18 | { |
19 | protected $writer = null; |
20 | |
21 | protected $spreadsheet = null; |
22 | |
23 | protected $period = ''; |
24 | |
25 | protected $title = 'statistik'; |
26 | |
27 | protected $type = 'xlsx'; |
28 | |
29 | public function __construct($request) |
30 | { |
31 | $validator = $request->getAttribute('validator'); |
32 | $this->type = $validator->getParameter('type')->isString()->setDefault('xlsx')->getValue(); |
33 | return $this; |
34 | } |
35 | |
36 | /** |
37 | * @param Response $response |
38 | * @return mixed |
39 | */ |
40 | public function writeDownload($response) |
41 | { |
42 | $resource = fopen('php://temp', 'x+'); |
43 | |
44 | try { |
45 | $this->getWriter()->save($resource); |
46 | rewind($resource); |
47 | $response->getBody()->write(stream_get_contents($resource)); |
48 | } catch (\Exception $e) { |
49 | fclose($resource); |
50 | |
51 | return $response->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR); |
52 | } |
53 | |
54 | fclose($resource); |
55 | |
56 | return $response |
57 | ->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') |
58 | ->withHeader('Content-Disposition', sprintf('attachment; filename="%s.%s"', $this->title, $this->type)); |
59 | } |
60 | |
61 | public function getSpreadSheet() |
62 | { |
63 | return $this->spreadsheet; |
64 | } |
65 | |
66 | public function getWriter() |
67 | { |
68 | if ('csv' == $this->type) { |
69 | $this->writer = IOFactory::createWriter($this->spreadsheet, 'Csv') |
70 | ->setUseBOM(true) |
71 | ->setSheetIndex(0) |
72 | ->setDelimiter(';'); |
73 | } |
74 | if ('xlsx' == $this->type) { |
75 | $this->writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx'); |
76 | } |
77 | return $this->writer; |
78 | } |
79 | |
80 | public function setSpreadSheet( |
81 | $title = 'statistic', |
82 | $creator = 'berlinonline', |
83 | $subject = '', |
84 | $description = 'statistic document', |
85 | $keywords = 'statistic zms' |
86 | ) { |
87 | $this->title = $title; |
88 | $this->spreadsheet = new Spreadsheet(); |
89 | $this->spreadsheet |
90 | ->getProperties() |
91 | ->setCreator($creator) |
92 | ->setLastModifiedBy($creator) |
93 | ->setTitle($title) |
94 | ->setSubject($subject) |
95 | ->setDescription($description) |
96 | ->setKeywords($keywords) |
97 | ->setCategory($subject); |
98 | return $this; |
99 | } |
100 | } |