Passed
Push — master ( 1d5d1c...1894e2 )
by Siim
26:48 queued 11:50
created

AbstractResponse::populateDto()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 10
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 Symfony\Component\HttpFoundation\JsonResponse;
19
20
abstract class AbstractResponse implements ResponseInterface
21
{
22
23
    use DtoTrait;
24
    use RequestTrait;
25
    use CreateErrorDtoTrait;
26
27
    /** @var DtoInterface $responseDto */
28
    protected $responseDto;
29
30
    /** @var int $responseStatus */
31
    protected $responseStatus;
32
33
    /** @var array $responseHeaders */
34
    protected $responseHeaders;
35
36
    /** @var JsonResponse $jsonResponse */
37
    protected $jsonResponse;
38
39
    public function __construct()
40
    {
41
        $this->createJsonResponse(new EmptyDto(), 200, static::HEADERS);
42
    }
43
44
    /**
45
     * @param DtoInterface $data
46
     * @param int $status
47
     * @param array $headers
48
     */
49
    protected function createJsonResponse(DtoInterface $data, int $status = 200, array $headers = self::HEADERS)
50
    {
51
        $this->setResponseDto($data);
52
        $this->setResponseStatus($status);
53
        $this->setResponseHeaders($headers);
54
    }
55
56
    abstract public function init();
57
58
    /**
59
     * @return JsonResponse
60
     */
61
    public function getJsonResponse(): JsonResponse
62
    {
63
        if (!$this->jsonResponse) {
64
            $response = new JsonResponse(
65
                $this->getResponseDto()->toArray(),
66
                $this->getResponseStatus(),
67
                $this->getResponseHeaders()
68
            );
69
        } else {
70
            $response = $this->jsonResponse;
71
        }
72
        $request = $this->getRequest()->getRequest();
73
        $response->headers->set(
74
            'Access-Control-Allow-Origin',
75
            $request->headers->get('Origin')
76
        );
77
        return $response;
78
    }
79
80
    /**
81
     * @param JsonResponse $jsonResponse
82
     */
83
    public function setJsonResponse(JsonResponse $jsonResponse)
84
    {
85
        $this->jsonResponse = $jsonResponse;
86
    }
87
88
    /**
89
     * @return DtoInterface
90
     */
91
    public function getResponseDto(): DtoInterface
92
    {
93
        return $this->responseDto;
94
    }
95
96
    /**
97
     * @param DtoInterface $responseDto
98
     */
99
    public function setResponseDto(DtoInterface $responseDto): void
100
    {
101
        $this->responseDto = $responseDto;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getResponseStatus(): int
108
    {
109
        return $this->responseStatus;
110
    }
111
112
    /**
113
     * @param int $responseStatus
114
     */
115
    public function setResponseStatus($responseStatus): void
116
    {
117
        $this->responseStatus = $responseStatus;
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getResponseHeaders(): array
124
    {
125
        return $this->responseHeaders;
126
    }
127
128
    /**
129
     * @param array $responseHeaders
130
     */
131
    public function setResponseHeaders(array $responseHeaders): void
132
    {
133
        $this->responseHeaders = $responseHeaders;
134
    }
135
136
    /**
137
     * @param string $tableName
138
     * @return AbstractRepository|null
139
     */
140
    public function getRepository(string $tableName): ?RepositoryInterface
141
    {
142
        $request = $this->getRequest();
143
        if (!$request) {
144
            return null;
145
        }
146
        $requestHandler = $request->getRequestHandler();
147
148
        $repositoryFactory = $requestHandler->getRepositoryFactory();
149
        return $repositoryFactory->create($tableName);
150
    }
151
152
    /**
153
     * @param $id
154
     * @param array $parameters
155
     * @param null $domain
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $domain is correct as it would always require null to be passed?
Loading history...
156
     * @param null $locale
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $locale is correct as it would always require null to be passed?
Loading history...
157
     * @return string
158
     */
159
    public function translate($id, array $parameters = array(), $domain = null, $locale = null)
160
    {
161
        $translator = $this->getRequest()->getRequestHandler()->getTranslator();
162
163
        return $translator->trans($id, $parameters, $domain, $locale);
164
    }
165
166
    /**
167
     * @param DtoInterface $dto
168
     * @param array $data |null
169
     * @return DtoInterface|\Sf4\Api\Dto\Response\ErrorDto
170
     */
171
    protected function populateDto(DtoInterface $dto, ?array $data)
172
    {
173
        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...
174
            try {
175
                $dto->populate($data);
176
            } catch (\ReflectionException $e) {
177
                $dto = $this->createErrorDto($e);
178
            }
179
        }
180
        return $dto;
181
    }
182
}
183