Passed
Push — master ( eff73f...ae14df )
by Siim
14:39
created

AbstractResponse::translate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 5
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\Response\EmptyDto;
13
use Sf4\Api\Dto\Traits\CreateErrorDtoTrait;
14
use Sf4\Api\Dto\Traits\DtoTrait;
15
use Sf4\Api\Repository\AbstractRepository;
16
use Sf4\Api\Repository\RepositoryInterface;
17
use Sf4\Api\Request\RequestTrait;
18
use Sf4\Api\Utils\Traits\TranslatorTrait;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
21
abstract class AbstractResponse implements ResponseInterface
22
{
23
24
    use DtoTrait;
25
    use RequestTrait;
26
    use CreateErrorDtoTrait;
27
    use TranslatorTrait;
28
29
    /** @var DtoInterface $responseDto */
30
    protected $responseDto;
31
32
    /** @var int $responseStatus */
33
    protected $responseStatus;
34
35
    /** @var array $responseHeaders */
36
    protected $responseHeaders;
37
38
    /** @var JsonResponse $jsonResponse */
39
    protected $jsonResponse;
40
41
    public function __construct()
42
    {
43
        $request = $this->getRequest();
44
        if ($request) {
45
            $requestHandler = $request->getRequestHandler();
46
            if ($requestHandler) {
47
                $translator = $requestHandler->getTranslator();
48
                $this->setTranslator($translator);
49
            }
50
        }
51
        $this->createJsonResponse(new EmptyDto(), 200, static::HEADERS);
52
    }
53
54
    /**
55
     * @param DtoInterface $data
56
     * @param int $status
57
     * @param array $headers
58
     */
59
    protected function createJsonResponse(DtoInterface $data, int $status = 200, array $headers = self::HEADERS): void
60
    {
61
        $this->setResponseDto($data);
62
        $this->setResponseStatus($status);
63
        $this->setResponseHeaders($headers);
64
    }
65
66
    abstract public function init();
67
68
    /**
69
     * @return JsonResponse
70
     */
71
    public function getJsonResponse(): JsonResponse
72
    {
73
        if (!$this->jsonResponse) {
74
            $response = new JsonResponse(
75
                $this->getResponseDto()->toArray(),
76
                $this->getResponseStatus(),
77
                $this->getResponseHeaders()
78
            );
79
        } else {
80
            $response = $this->jsonResponse;
81
        }
82
        $request = $this->getRequest();
83
        if ($request) {
84
            $httpRequest = $request->getRequest();
85
            if ($httpRequest) {
86
                $response->headers->set(
87
                    'Access-Control-Allow-Origin',
88
                    $httpRequest->headers->get('Origin')
89
                );
90
            }
91
        }
92
        return $response;
93
    }
94
95
    /**
96
     * @param JsonResponse $jsonResponse
97
     */
98
    public function setJsonResponse(JsonResponse $jsonResponse)
99
    {
100
        $this->jsonResponse = $jsonResponse;
101
    }
102
103
    /**
104
     * @return DtoInterface
105
     */
106
    public function getResponseDto(): DtoInterface
107
    {
108
        return $this->responseDto;
109
    }
110
111
    /**
112
     * @param DtoInterface $responseDto
113
     */
114
    public function setResponseDto(DtoInterface $responseDto): void
115
    {
116
        $this->responseDto = $responseDto;
117
    }
118
119
    /**
120
     * @return int
121
     */
122
    public function getResponseStatus(): int
123
    {
124
        return $this->responseStatus;
125
    }
126
127
    /**
128
     * @param int $responseStatus
129
     */
130
    public function setResponseStatus($responseStatus): void
131
    {
132
        $this->responseStatus = $responseStatus;
133
    }
134
135
    /**
136
     * @return array
137
     */
138
    public function getResponseHeaders(): array
139
    {
140
        return $this->responseHeaders;
141
    }
142
143
    /**
144
     * @param array $responseHeaders
145
     */
146
    public function setResponseHeaders(array $responseHeaders): void
147
    {
148
        $this->responseHeaders = $responseHeaders;
149
    }
150
151
    /**
152
     * @param string $tableName
153
     * @return AbstractRepository|null
154
     */
155
    public function getRepository(string $tableName): ?RepositoryInterface
156
    {
157
        $request = $this->getRequest();
158
        if (!$request) {
159
            return null;
160
        }
161
        $requestHandler = $request->getRequestHandler();
162
        if ($requestHandler) {
163
            $repositoryFactory = $requestHandler->getRepositoryFactory();
164
            return $repositoryFactory->create($tableName);
165
        }
166
167
        return null;
168
    }
169
170
    /**
171
     * @param DtoInterface $dto
172
     * @param array $data |null
173
     * @return DtoInterface|\Sf4\Api\Dto\Response\ErrorDto
174
     */
175
    protected function populateDto(DtoInterface $dto, ?array $data)
176
    {
177
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
178
            try {
179
                $dto->populate($data);
180
            } catch (\ReflectionException $e) {
181
                $dto = $this->createErrorDto($e);
182
            }
183
        }
184
        return $dto;
185
    }
186
}
187