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

ApiResponseFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createResponse() 0 12 2
A createNotFoundResponse() 0 4 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