Completed
Push — master ( 055691...1f9c21 )
by Siim
13:14
created

AbstractResponse::populateDto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 17.01.19
6
 * Time: 9:19
7
 */
8
9
namespace Sf4\Api\Response;
10
11
use Sf4\Api\Dto\DtoInterface;
12
use Sf4\Api\Dto\DtoPopulator;
13
use Sf4\Api\Dto\DtoTrait;
14
use Sf4\Api\Dto\EmptyDto;
15
use Sf4\Api\Repository\AbstractRepository;
16
use Sf4\Api\Request\RequestTrait;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
19
abstract class AbstractResponse implements ResponseInterface
20
{
21
22
    use DtoTrait;
23
    use RequestTrait;
24
25
    /** @var DtoInterface $responseDto */
26
    protected $responseDto;
27
28
    /** @var int $responseStatus */
29
    protected $responseStatus;
30
31
    /** @var array $responseHeaders */
32
    protected $responseHeaders;
33
34
    public function __construct()
35
    {
36
        $this->createJsonResponse(new EmptyDto(), 200, static::HEADERS);
37
    }
38
39
    /**
40
     * @param DtoInterface $data
41
     * @param int $status
42
     * @param array $headers
43
     */
44
    protected function createJsonResponse(DtoInterface $data, int $status = 200, array $headers = self::HEADERS)
45
    {
46
        $this->setResponseDto($data);
47
        $this->setResponseStatus($status);
48
        $this->setResponseHeaders($headers);
49
    }
50
51
    public abstract function init();
52
53
    /**
54
     * @return JsonResponse
55
     */
56
    public function getJsonResponse(): JsonResponse
57
    {
58
        $response = new JsonResponse(
59
            $this->getResponseDto(),
60
            $this->getResponseStatus(),
61
            $this->getResponseHeaders()
62
        );
63
        $request = $this->getRequest()->getRequest();
64
        $response->headers->set(
65
            'Access-Control-Allow-Origin',
66
            $request->headers->get('Origin')
67
        );
68
        return $response;
69
    }
70
71
    protected function populateDto(DtoInterface $dto, array $data)
72
    {
73
        $dtoPopulator = new DtoPopulator();
74
        $dtoPopulator->populate($dto, $data);
75
    }
76
77
    /**
78
     * @return DtoInterface
79
     */
80
    public function getResponseDto(): DtoInterface
81
    {
82
        return $this->responseDto;
83
    }
84
85
    /**
86
     * @param DtoInterface $responseDto
87
     */
88
    public function setResponseDto(DtoInterface $responseDto): void
89
    {
90
        $this->responseDto = $responseDto;
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function getResponseStatus(): int
97
    {
98
        return $this->responseStatus;
99
    }
100
101
    /**
102
     * @param int $responseStatus
103
     */
104
    public function setResponseStatus($responseStatus): void
105
    {
106
        $this->responseStatus = $responseStatus;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getResponseHeaders(): array
113
    {
114
        return $this->responseHeaders;
115
    }
116
117
    /**
118
     * @param array $responseHeaders
119
     */
120
    public function setResponseHeaders(array $responseHeaders): void
121
    {
122
        $this->responseHeaders = $responseHeaders;
123
    }
124
125
    /**
126
     * @param string $entityClass
127
     * @return AbstractRepository|null
128
     */
129
    public function getRepository(string $entityClass): ?AbstractRepository
130
    {
131
        $request = $this->getRequest();
132
        if(!$request) {
133
            return null;
134
        }
135
        $requestHandler = $request->getRequestHandler();
136
        $manager = $requestHandler->getEntityManager();
137
138
        $repository = $manager->getRepository($entityClass);
139
        if($repository instanceof AbstractRepository) {
140
            return $repository;
141
        }
142
143
        return null;
144
    }
145
}
146