ResponseUtilsTrait::generateImageResponse()   A
last analyzed

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