Passed
Pull Request — master (#456)
by Alejandro
08:02
created

ResponseUtilsTrait::generateImageResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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