Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
27 / 30
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Download
90.00% covered (success)
90.00%
27 / 30
80.00% covered (warning)
80.00%
4 / 5
7.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 writeDownload
72.73% covered (warning)
72.73%
8 / 11
0.00% covered (danger)
0.00%
0 / 1
2.08
 getSpreadSheet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getWriter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setSpreadSheet
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 *
5 * @package zmsstatistic
6 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
7 *
8 */
9
10namespace BO\Zmsstatistic\Helper;
11
12use BO\Slim\Response;
13use Fig\Http\Message\StatusCodeInterface;
14use PhpOffice\PhpSpreadsheet\Spreadsheet;
15use PhpOffice\PhpSpreadsheet\IOFactory;
16
17class 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 ('xlsx' == $this->type) {
69            $this->writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx');
70        }
71        return $this->writer;
72    }
73
74    public function setSpreadSheet(
75        $title = 'statistic',
76        $creator = 'berlinonline',
77        $subject = '',
78        $description = 'statistic document',
79        $keywords = 'statistic zms'
80    ) {
81        $this->title = $title;
82        $this->spreadsheet = new Spreadsheet();
83        $this->spreadsheet
84            ->getProperties()
85            ->setCreator($creator)
86            ->setLastModifiedBy($creator)
87            ->setTitle($title)
88            ->setSubject($subject)
89            ->setDescription($description)
90            ->setKeywords($keywords)
91            ->setCategory($subject);
92        return $this;
93    }
94}