Completed
Push — master ( 065cdd...96faaf )
by Alejandro
09:28
created

ResponseUtilsTrait::generateImageResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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
    protected function generateImageResponse($imagePath)
23
    {
24
        return $this->generateBinaryResponse($imagePath);
25
    }
26
27
    protected function generateBinaryResponse($path, $extraHeaders = [])
28
    {
29
        $body = new Stream($path);
30
        return new Response($body, 200, ArrayUtils::merge([
31
            'Content-Type' => (new \finfo(FILEINFO_MIME))->file($path),
32
            'Content-Length' => (string) $body->getSize(),
33
        ], $extraHeaders));
34
    }
35
}
36