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

ResponseUtilsTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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 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