Completed
Push — feature/EVO-8294-file-upload-r... ( ebbdfa )
by
unknown
63:34
created

ArchiveManager::buildArchiveResponse()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 3
eloc 17
nc 4
nop 2
1
<?php
2
/**
3
 * model class for Graviton\ArchiveBundle\Model\ActivityLog
4
 */
5
namespace Graviton\ArchiveBundle\Manager;
6
7
use Gaufrette\Filesystem;
8
use Graviton\ArchiveBundle\Document\Archive;
9
use Graviton\ArchiveBundle\Document\ArchiveMetadata;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
12
13
/**
14
 * Graviton\ArchiveBundle\Model\Archive
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class ArchiveManager
21
{
22
    /** @var Filesystem */
23
    private $filesystem;
24
25
26
    public function __construct(Filesystem $filesystem)
27
    {
28
        $this->filesystem = $filesystem;
29
    }
30
31
    private function getMimeType($key)
32
    {
33
        return $this->filesystem->mimeType($key);
34
    }
35
36
    private function getFile($key)
37
    {
38
        return $this->filesystem->read($key);
39
    }
40
41
    /**
42
     * @param Response $response
43
     * @param Archive $archive
44
     * @return Response
45
     */
46
    public function buildArchiveResponse(Response $response, Archive $archive)
47
    {
48
        /** @var ArchiveMetadata $metadata */
49
        $metadata = $archive->getMetadata();
50
        if (!$metadata) {
51
            // TODO Do exception  here
52
        }
53
54
        // If no data, 404.
55
        $mimeType = $this->getMimeType($archive->getId());
56
        if ($mimeType !== $archive->getMetadata()->getMime() ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
57
            // TODO Do exception  here
58
        }
59
60
        // Read Data
61
        $file = $this->getFile($archive->getId());
62
63
        // Create Response
64
        $disposition = $response->headers->makeDisposition(
65
            ResponseHeaderBag::DISPOSITION_INLINE,
66
            $metadata->getFilename()
67
        );
68
        $response
69
            ->setStatusCode(Response::HTTP_OK)
70
            ->setContent($file);
71
        $response
72
            ->headers->set('Content-Type', $mimeType);
73
        $response
74
            ->headers->set('Content-Disposition', $disposition);
75
        return $response;
76
    }
77
78
}