Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

Exporter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 73
rs 10
c 0
b 0
f 0

4 Methods

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