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

ResponseUtilsTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 7
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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