Completed
Pull Request — master (#52)
by
unknown
02:44
created

Exporter::getResponse()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\CoreBundle\Exporter;
14
15
use Exporter\Source\SourceIteratorInterface;
16
use Exporter\Writer\TypedWriterInterface;
17
use Symfony\Component\HttpFoundation\StreamedResponse;
18
19
class Exporter
20
{
21
    /**
22
     * @var TypedWriterInterface[]
23
     */
24
    private $writers;
25
26
    /**
27
     * @param TypedWriterInterface[] $writers an array of allowed typed writers, indexed by format name
28
     */
29
    public function __construct(array $writers = array())
30
    {
31
        $this->writers = array();
32
33
        foreach ($writers as $writer) {
34
            $this->addWriter($writer);
35
        }
36
    }
37
38
    /**
39
     * @throws \RuntimeException
40
     *
41
     * @param string                  $format
42
     * @param string                  $filename
43
     * @param SourceIteratorInterface $source
44
     *
45
     * @return StreamedResponse
46
     */
47
    public function getResponse($format, $filename, SourceIteratorInterface $source)
48
    {
49
50
        if (!array_key_exists($format, $this->writers)) {
51
            throw new \RuntimeException(sprintf(
52
                'Invalid "%s" format, supported formats are : "%s"',
53
                $format,
54
                implode(', ', array_keys($this->writers))
55
            ));
56
        }
57
        $writer = $this->writers[$format];
58
59
        $callback = function () use ($source, $writer) {
60
            $handler = \Blast\CoreBundle\Exporter\Handler::create($source, $writer);
61
            $handler->export();
62
        };
63
64
        $headers = array(
65
            'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
66
        );
67
68
        $headers['Content-Type'] = $writer->getDefaultMimeType();
69
70
        return new StreamedResponse($callback, 200, $headers);
71
    }
72
73
    /**
74
     * Returns a simple array of export formats.
75
     *
76
     * @return string[] writer formats as returned by the TypedWriterInterface::getFormat() method
77
     */
78
    public function getAvailableFormats()
79
    {
80
        return array_keys($this->writers);
81
    }
82
83
    /**
84
     * The main benefit of this method is the type hinting.
85
     *
86
     * @param TypedWriterInterface $writer a possible writer for exporting data
87
     */
88
    public function addWriter(TypedWriterInterface $writer)
89
    {
90
        $this->writers[$writer->getFormat()] = $writer;
91
    }
92
}
93