Completed
Pull Request — master (#277)
by Alejandro
07:37
created

ResponseUtilsTrait::generateBinaryResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Util;
5
6
use Fig\Http\Message\StatusCodeInterface as StatusCode;
7
use finfo;
8
use Psr\Http\Message\ResponseInterface;
9
use Zend\Diactoros\Response;
10
use Zend\Diactoros\Stream;
11
use Zend\Stdlib\ArrayUtils;
12
use const FILEINFO_MIME;
13
14
trait ResponseUtilsTrait
15
{
16
    private function generateImageResponse(string $imagePath): ResponseInterface
17
    {
18
        return $this->generateBinaryResponse($imagePath);
19
    }
20
21
    private function generateBinaryResponse(string $path, array $extraHeaders = []): ResponseInterface
22
    {
23
        $body = new Stream($path);
24
        return new Response($body, StatusCode::STATUS_OK, ArrayUtils::merge([
25
            'Content-Type' => (new finfo(FILEINFO_MIME))->file($path),
26
            'Content-Length' => (string) $body->getSize(),
27
        ], $extraHeaders));
28 1
    }
29
}
30