1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupRa\RaBundle\Service; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaSecondFactorExportCollection; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
25
|
|
|
|
26
|
|
|
class RaSecondFactorExport |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \Psr\Log\LoggerInterface |
30
|
|
|
*/ |
31
|
|
|
private $logger; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
LoggerInterface $logger |
35
|
|
|
) { |
36
|
|
|
$this->logger = $logger; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function export(RaSecondFactorExportCollection $collection, $fileName) |
40
|
|
|
{ |
41
|
|
|
$this->logger->notice(sprintf('Exporting %d rows to "%s"', $collection->count(), $fileName)); |
42
|
|
|
|
43
|
|
|
$keys = array_keys($collection->getColumnNames()); |
44
|
|
|
|
45
|
|
|
return new StreamedResponse( |
46
|
|
|
function () use ($collection, $keys) { |
47
|
|
|
$handle = fopen('php://output', 'r+'); |
48
|
|
|
fputcsv($handle, $collection->getColumnNames()); |
49
|
|
|
foreach ($collection->getElements() as $row) { |
50
|
|
|
$cells = []; |
51
|
|
|
$array = (array)$row; |
52
|
|
|
foreach ($keys as $key) { |
53
|
|
|
$cells[$key] = $array[$key]; |
54
|
|
|
} |
55
|
|
|
fputcsv($handle, $cells); |
56
|
|
|
} |
57
|
|
|
fflush($handle); |
58
|
|
|
fclose($handle); |
59
|
|
|
}, |
60
|
|
|
Response::HTTP_OK, |
61
|
|
|
[ |
62
|
|
|
'Content-Type' => 'application/csv', |
63
|
|
|
'Content-Disposition' => sprintf('attachment; filename="%s.csv"', $fileName), |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|