Passed
Pull Request — master (#10)
by Alejandro
02:42
created

ResponseUtilsTrait::generateBinaryResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
    /**
18
     * @deprecated Use generateBinaryResponse method instead
19
     */
20 2
    private function generateImageResponse(string $imagePath): ResponseInterface
21
    {
22 2
        return $this->generateBinaryResponse($imagePath);
23
    }
24
25 3
    private function generateBinaryResponse(string $path, array $extraHeaders = []): ResponseInterface
26
    {
27 3
        $body = new Stream($path);
28 3
        return new Response($body, StatusCode::STATUS_OK, ArrayUtils::merge([
29 3
            'Content-Type' => (new finfo(FILEINFO_MIME))->file($path),
30 3
            'Content-Length' => (string) $body->getSize(),
31 3
        ], $extraHeaders));
32
    }
33
}
34