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

ResponseUtilsTrait::generateBinaryResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
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