1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace App\Helpers\SEPAXML; |
20
|
|
|
|
21
|
|
|
use App\Entity\SEPAExport; |
22
|
|
|
use App\Helpers\ZIPBinaryFileResponseFacade; |
23
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
24
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
25
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
26
|
|
|
|
27
|
|
|
final class SEPAXMLExportResult implements \Countable |
28
|
|
|
{ |
29
|
|
|
/** @var SEPAExport[] */ |
30
|
|
|
private $sepa_exports; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param SEPAExport[] $sepa_exports |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $sepa_exports) |
36
|
|
|
{ |
37
|
|
|
if (count($sepa_exports) === 0) { |
38
|
|
|
throw new \InvalidArgumentException('$sepa_exports must not be empty!'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
//Perform some checks on the sepa exports |
42
|
|
|
foreach($sepa_exports as $sepa_export) { |
43
|
|
|
if (!$sepa_export instanceof SEPAExport) { |
44
|
|
|
throw new \InvalidArgumentException('$sepa_exports must all be of type SEPAExport!'); |
45
|
|
|
} |
46
|
|
|
if ($sepa_export->getXmlFile() === null) { |
47
|
|
|
throw new \InvalidArgumentException('Every Export in $sepa_exports must have an associated XML file!'); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->sepa_exports = $sepa_exports; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the number of SEPAExports contained |
56
|
|
|
* @return int|void |
57
|
|
|
*/ |
58
|
|
|
public function count() |
59
|
|
|
{ |
60
|
|
|
return count($this->sepa_exports); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the SEPA Exports contained in this result |
66
|
|
|
* @return SEPAExport[] |
67
|
|
|
*/ |
68
|
|
|
public function getSEPAExports(): array |
69
|
|
|
{ |
70
|
|
|
return $this->sepa_exports; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns an array of all XML files contained in this result. |
75
|
|
|
* The array keys contains suggested filenames for the files |
76
|
|
|
* @return \SplFileInfo[] |
77
|
|
|
*/ |
78
|
|
|
public function getXMLFiles(): array |
79
|
|
|
{ |
80
|
|
|
$result = []; |
81
|
|
|
|
82
|
|
|
foreach ($this->sepa_exports as $sepa_export) { |
83
|
|
|
$filename = $this->generateFilename($sepa_export); |
84
|
|
|
//Ensure that filename is not existing yet (add an increment suffix) |
85
|
|
|
$i = 2; |
86
|
|
|
while (isset($result[$filename])) { |
87
|
|
|
$filename = basename($filename) . '_' . sprintf("%d", $i++) . '.xml'; |
88
|
|
|
} |
89
|
|
|
$result[$filename] = $sepa_export->getXmlFile(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns an array of the contained XML files as strings. |
97
|
|
|
* The array keys contains suggested filenames for the files. |
98
|
|
|
* @return string[] |
99
|
|
|
*/ |
100
|
|
|
public function getXMLString(): array |
101
|
|
|
{ |
102
|
|
|
$result = []; |
103
|
|
|
$files = $this->getXMLFiles(); |
104
|
|
|
|
105
|
|
|
foreach ($files as $filename => $file) { |
106
|
|
|
$result[$filename] = file_get_contents($file->getPathname()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Generates a download response for the contained XML files. If only a single XML is contained, it will be downloaded |
114
|
|
|
* as XML file, otherwise as ZIP containing all files. |
115
|
|
|
* @param string $disposition_filename |
116
|
|
|
* @param bool $force_zip |
117
|
|
|
* @return BinaryFileResponse |
118
|
|
|
*/ |
119
|
|
|
public function getDownloadResponse(string $disposition_filename, bool $force_zip = false): BinaryFileResponse |
120
|
|
|
{ |
121
|
|
|
//If we have only one file, we can just return the file |
122
|
|
|
if ($force_zip === false && $this->count() === 1) { |
123
|
|
|
$response = new BinaryFileResponse($this->sepa_exports[0]); |
|
|
|
|
124
|
|
|
$response->setPrivate(); |
125
|
|
|
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $disposition_filename); |
126
|
|
|
} else { //Otherwise we download the file as ZIP |
127
|
|
|
$response = ZIPBinaryFileResponseFacade::createZIPResponseFromFiles($this->getXMLFiles(), $disposition_filename); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $response; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Persist all contained SEPAExport entities using the given EntityManager. |
135
|
|
|
* @param EntityManagerInterface $entityManager |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function persistSEPAExports(EntityManagerInterface $entityManager): void |
139
|
|
|
{ |
140
|
|
|
foreach($this->sepa_exports as $sepa_export) { |
141
|
|
|
$entityManager->persist($sepa_export); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function generateFilename(SEPAExport $export): string |
146
|
|
|
{ |
147
|
|
|
return $export->getDescription() . '_' . $export->getCreationDate()->format('Y-M-D-His') . '.xml'; |
148
|
|
|
} |
149
|
|
|
} |