Test Failed
Push — master ( 9e6cf9...d006e3 )
by Petr
04:08
created

ApiResponseFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Response;
4
5
use AppBundle\Entity\Image;
6
use AppBundle\Exception\UnsupportedTypeException;
7
use Symfony\Component\HttpFoundation\Response;
8
9
/**
10
 * @author Vehsamrak
11
 */
12
class ApiResponseFactory
13
{
14
15
    /** @var string */
16
    private $filePath;
17
18
    public function __construct(string $applicationRootPath)
19
    {
20
        $this->filePath = realpath($applicationRootPath . '/../var/upload');
21
    }
22
23
    public function createResponse($responseData)
24
    {
25
        if ($responseData instanceof Image) {
26
            $imagesBasePath = $this->filePath . '/images/';
27
            $imagePath = $imagesBasePath . $responseData->getName();
28
            $response = new FileResponse($imagePath);
29
        } else {
30
            throw new UnsupportedTypeException();
31
        }
32
33
        return $response;
34
    }
35
36
    public function createNotFoundResponse(): ApiError
37
    {
38
        return new ApiError('Resource was not found.', Response::HTTP_NOT_FOUND);
39
    }
40
}
41