Completed
Push — master ( d3519f...c00a08 )
by Siim
36:11 queued 21:17
created

AbstractResponse::populateDto()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 8
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\DtoTrait;
13
use Sf4\Api\Dto\EmptyDto;
14
use Sf4\Api\Dto\ErrorDto;
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
    /**
72
     * @param DtoInterface $dto
73
     * @param array $data
74
     */
75
    protected function populateDto(DtoInterface $dto, array $data)
76
    {
77
        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...
78
            try {
79
                $dto->populate($data);
80
            } catch (\ReflectionException $e) {
81
                $dto = new ErrorDto();
82
                $dto->error = $e->getMessage();
83
            }
84
        }
85
    }
86
87
    /**
88
     * @return DtoInterface
89
     */
90
    public function getResponseDto(): DtoInterface
91
    {
92
        return $this->responseDto;
93
    }
94
95
    /**
96
     * @param DtoInterface $responseDto
97
     */
98
    public function setResponseDto(DtoInterface $responseDto): void
99
    {
100
        $this->responseDto = $responseDto;
101
    }
102
103
    /**
104
     * @return int
105
     */
106
    public function getResponseStatus(): int
107
    {
108
        return $this->responseStatus;
109
    }
110
111
    /**
112
     * @param int $responseStatus
113
     */
114
    public function setResponseStatus($responseStatus): void
115
    {
116
        $this->responseStatus = $responseStatus;
117
    }
118
119
    /**
120
     * @return array
121
     */
122
    public function getResponseHeaders(): array
123
    {
124
        return $this->responseHeaders;
125
    }
126
127
    /**
128
     * @param array $responseHeaders
129
     */
130
    public function setResponseHeaders(array $responseHeaders): void
131
    {
132
        $this->responseHeaders = $responseHeaders;
133
    }
134
135
    /**
136
     * @param string $entityClass
137
     * @return AbstractRepository|null
138
     */
139
    public function getRepository(string $entityClass): ?AbstractRepository
140
    {
141
        $request = $this->getRequest();
142
        if(!$request) {
143
            return null;
144
        }
145
        $requestHandler = $request->getRequestHandler();
146
        $manager = $requestHandler->getEntityManager();
147
148
        $repository = $manager->getRepository($entityClass);
149
        if($repository instanceof AbstractRepository) {
150
            return $repository;
151
        }
152
153
        return null;
154
    }
155
}
156