Completed
Pull Request — master (#105)
by Mikołaj
04:26 queued 01:11
created

ResponseUtilsTrait::generateDownloadFileResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Shlinkio\Shlink\Common\Util;
3
4
use Zend\Diactoros\Response;
5
use Zend\Diactoros\Stream;
6
use Zend\Stdlib\ArrayUtils;
7
8
trait ResponseUtilsTrait
9
{
10
    protected function generateDownloadFileResponse($filePath)
11
    {
12
        return $this->generateBinaryResponse($filePath, [
13
            'Content-Disposition' => 'attachment; filename=' . basename($filePath),
14
            'Content-Transfer-Encoding' => 'Binary',
15
            'Content-Description' => 'File Transfer',
16
            'Pragma' => 'public',
17
            'Expires' => '0',
18
            'Cache-Control' => 'must-revalidate',
19
        ]);
20
    }
21
22 1
    protected function generateImageResponse($imagePath)
23
    {
24 1
        return $this->generateBinaryResponse($imagePath);
25
    }
26
27 1
    protected function generateBinaryResponse($path, $extraHeaders = [])
28
    {
29 1
        $body = new Stream($path);
30 1
        return new Response($body, 200, ArrayUtils::merge([
31 1
            'Content-Type' => (new \finfo(FILEINFO_MIME))->file($path),
32 1
            'Content-Length' => (string) $body->getSize(),
33 1
        ], $extraHeaders));
34
    }
35
}
36