Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
71 / 71 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
PickupSpreadSheet | |
100.00% |
71 / 71 |
|
100.00% |
4 / 4 |
12 | |
100.00% |
1 / 1 |
readResponse | |
100.00% |
34 / 34 |
|
100.00% |
1 / 1 |
2 | |||
readProcessList | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
setRows | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
5 | |||
convertspecialchars | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * @package Zmsadmin |
5 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
6 | **/ |
7 | |
8 | namespace BO\Zmsadmin; |
9 | |
10 | use League\Csv\Writer; |
11 | use League\Csv\Reader; |
12 | use League\Csv\EscapeFormula; |
13 | |
14 | class PickupSpreadSheet extends BaseController |
15 | { |
16 | public static $maxSqlLoops = 5; |
17 | public static $maxLoopLimit = 1000; |
18 | /** |
19 | * @SuppressWarnings(Param) |
20 | * @return String |
21 | */ |
22 | public function readResponse( |
23 | \Psr\Http\Message\RequestInterface $request, |
24 | \Psr\Http\Message\ResponseInterface $response, |
25 | array $args |
26 | ) { |
27 | $validator = $request->getAttribute('validator'); |
28 | $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 2])->getEntity(); |
29 | $selectedScope = $validator->getParameter('selectedscope')->isNumber()->getValue(); |
30 | $scopeId = ($selectedScope) ? $selectedScope : $workstation->scope['id']; |
31 | $scope = \App::$http->readGetResult('/scope/' . $scopeId . '/', [ |
32 | 'resolveReferences' => 1 |
33 | ])->getEntity(); |
34 | $department = \App::$http->readGetResult('/scope/' . $scopeId . '/department/')->getEntity(); |
35 | |
36 | $providerName = $scope['provider']['name']; |
37 | |
38 | $rows = []; |
39 | $rowCount = 1; |
40 | $loop = 0; |
41 | do { |
42 | $processList = $this->readProcessList($scopeId, $loop); |
43 | $rows = $this->setRows($processList, $rowCount, $rows); |
44 | $loop++; |
45 | $rowCount = count($rows) + 1; |
46 | } while (static::$maxSqlLoops > $loop); |
47 | |
48 | $writer = Writer::createFromString(); |
49 | $writer->setDelimiter(';'); |
50 | $writer->addFormatter(new EscapeFormula()); |
51 | $writer->insertOne(['Abholer','','','','','','','','']); |
52 | $writer->insertOne([$department->name . ' - ' . $providerName,'','','','','','','','']); |
53 | $writer->insertOne(['','Datum','Nr.','Name','Telefonnr.','eMail','Dienstleistung','Anmerkung']); |
54 | $writer->setOutputBOM(Reader::BOM_UTF8); |
55 | $writer->insertAll($rows); |
56 | |
57 | $response->getBody()->write($writer->toString()); |
58 | |
59 | $fileName = 'abholer_' . $providerName; |
60 | |
61 | return $response |
62 | ->withHeader('Content-Type', 'text/csv; charset=UTF-8') |
63 | ->withHeader('Content-Description', 'File Transfer') |
64 | ->withHeader( |
65 | 'Content-Disposition', |
66 | sprintf('download; filename="%s.csv"', $this->convertspecialChars($fileName)) |
67 | ); |
68 | } |
69 | |
70 | protected function readProcessList($scopeId, $loop, $retry = 0) |
71 | { |
72 | try { |
73 | return PickupQueue::getProcessList($scopeId, static::$maxLoopLimit, $loop * static::$maxLoopLimit); |
74 | } catch (\BO\Zmsclient\Exception\ApiFailed $exception) { |
75 | if ($retry < 3) { |
76 | sleep(1); // Let the other request complete his transaction |
77 | return $this->readProcessList($scopeId, $loop, $retry + 1); |
78 | } |
79 | throw $exception; |
80 | } |
81 | } |
82 | |
83 | protected function setRows($processList, $rowCount, $rows) |
84 | { |
85 | if ($processList) { |
86 | foreach ($processList->getArrayCopy() as $processItem) { |
87 | $client = $processItem->getFirstClient(); |
88 | $requestNameList = []; |
89 | if (count($processItem->getRequests())) { |
90 | foreach ($processItem->getRequests() as $requestItem) { |
91 | $requestNameList[] = $requestItem->getName(); |
92 | } |
93 | } |
94 | |
95 | $date = new \DateTime('@' . $processItem->queue['arrivalTime']); |
96 | $date->setTimezone(\App::$now->getTimezone()); |
97 | |
98 | $rows[] = [ |
99 | $rowCount, |
100 | $date->format('d.m.Y'), |
101 | $processItem->queue['number'], |
102 | $client['familyName'], |
103 | $client['telephone'], |
104 | $client['email'], |
105 | join(', ', $requestNameList), |
106 | $processItem->amendment |
107 | ]; |
108 | $rowCount++; |
109 | } |
110 | } |
111 | return $rows; |
112 | } |
113 | |
114 | protected function convertspecialchars($string) |
115 | { |
116 | |
117 | $convert = array ( |
118 | array ('ä','ae',), |
119 | array ('ö','oe',), |
120 | array ('ü','ue',), |
121 | array ('ß','ss',), |
122 | array (' ','_',), |
123 | ); |
124 | |
125 | |
126 | foreach ($convert as $array) { |
127 | $string = str_replace($array[0], $array[1], $string); |
128 | } |
129 | return $string; |
130 | } |
131 | } |