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

ResponseUtilsTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 8
cts 16
cp 0.5
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateImageResponse() 0 4 1
A generateBinaryResponse() 0 8 1
A generateDownloadFileResponse() 0 11 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