1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: siim |
5
|
|
|
* Date: 17.01.19 |
6
|
|
|
* Time: 8:03 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Sf4\Api\Request; |
10
|
|
|
|
11
|
|
|
use Sf4\Api\Dto\DtoInterface; |
12
|
|
|
use Sf4\Api\Dto\Traits\DtoTrait; |
13
|
|
|
use Sf4\Api\RequestHandler\RequestHandlerTrait; |
14
|
|
|
use Sf4\Api\Response\ResponseInterface; |
15
|
|
|
use Sf4\Api\Response\ResponseTrait; |
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
abstract class AbstractRequest implements RequestInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use ResponseTrait; |
23
|
|
|
use DtoTrait; |
24
|
|
|
use RequestHandlerTrait; |
25
|
|
|
|
26
|
|
|
/** @var Request|null $request */ |
27
|
|
|
protected $request; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AbstractRequest constructor. |
31
|
|
|
* @param ResponseInterface $response |
32
|
|
|
* @param DtoInterface $dto |
33
|
|
|
*/ |
34
|
|
|
public function init(ResponseInterface $response, DtoInterface $dto = null) |
35
|
|
|
{ |
36
|
|
|
$response->setRequest($this); |
37
|
|
|
$this->setResponse($response); |
38
|
|
|
if ($dto) { |
39
|
|
|
$this->setDto($dto); |
40
|
|
|
$this->attachDtoToResponse(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function attachDtoToResponse() |
45
|
|
|
{ |
46
|
|
|
$response = $this->getResponse(); |
47
|
|
|
$dto = $this->getDto(); |
48
|
|
|
if ($response !== null && $response->getDto() === null && $dto !== null) { |
49
|
|
|
$response->setDto($dto); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \Closure $closure |
55
|
|
|
* @param string|null $cacheKey |
56
|
|
|
* @param array $tags |
57
|
|
|
* @param int|null $expiresAfter |
58
|
|
|
* @throws \Psr\Cache\CacheException |
59
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
60
|
|
|
*/ |
61
|
|
|
public function getCachedResponse( |
62
|
|
|
\Closure $closure, |
63
|
|
|
string $cacheKey = null, |
64
|
|
|
array $tags = [], |
65
|
|
|
int $expiresAfter = null |
66
|
|
|
) { |
67
|
|
|
if (null === $expiresAfter) { |
68
|
|
|
$expiresAfter = 10; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($cacheKey) { |
72
|
|
|
$cacheKey = md5($cacheKey); |
73
|
|
|
$requestHandler = $this->getRequestHandler(); |
74
|
|
|
|
75
|
|
|
$data = $requestHandler->getCacheDataOrAdd( |
76
|
|
|
$cacheKey, |
77
|
|
|
function () use ($closure, $requestHandler) { |
78
|
|
|
$closure(); |
79
|
|
|
return $requestHandler->getResponse()->getContent(); |
80
|
|
|
}, |
81
|
|
|
$tags, |
82
|
|
|
$expiresAfter |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$responseContent = json_decode($data, true); |
86
|
|
|
$this->getResponse()->setJsonResponse( |
87
|
|
|
new JsonResponse( |
88
|
|
|
$responseContent, |
89
|
|
|
$this->getResponse()->getResponseStatus(), |
90
|
|
|
$this->getResponse()->getResponseHeaders() |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
} else { |
94
|
|
|
/* |
95
|
|
|
* Handle request |
96
|
|
|
*/ |
97
|
|
|
$closure(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @throws \Psr\Cache\CacheException |
103
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
104
|
|
|
*/ |
105
|
|
|
public function handle() |
106
|
|
|
{ |
107
|
|
|
$this->getCachedResponse( |
108
|
|
|
function () { |
109
|
|
|
$requestContent = $this->getRequest()->getContent(); |
110
|
|
|
if ($requestContent) { |
111
|
|
|
$data = json_decode($requestContent, true); |
112
|
|
|
$this->getDto()->populate($data); |
113
|
|
|
} |
114
|
|
|
$this->getResponse()->init(); |
115
|
|
|
}, |
116
|
|
|
$this->getCacheKey(), |
117
|
|
|
$this->getCacheTags(), |
118
|
|
|
$this->getCacheExpiresAfter() |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return Request|null |
124
|
|
|
*/ |
125
|
|
|
public function getRequest(): ?Request |
126
|
|
|
{ |
127
|
|
|
return $this->request; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Request|null $request |
132
|
|
|
*/ |
133
|
|
|
public function setRequest(?Request $request): void |
134
|
|
|
{ |
135
|
|
|
$this->request = $request; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getRoute(): string |
142
|
|
|
{ |
143
|
|
|
return static::ROUTE; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function getCacheKey(): string |
147
|
|
|
{ |
148
|
|
|
return $this->getRoute(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
abstract protected function getCacheTags(): array; |
152
|
|
|
|
153
|
|
|
abstract protected function getCacheExpiresAfter(): ?int; |
154
|
|
|
} |
155
|
|
|
|