AbstractRequest::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 7
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: 8:03
7
 */
8
9
namespace Sf4\Api\Request;
10
11
use Closure;
12
use Psr\Cache\CacheException;
13
use Psr\Cache\InvalidArgumentException;
14
use Sf4\Api\Dto\DtoInterface;
15
use Sf4\Api\Dto\Traits\DtoTrait;
16
use Sf4\Api\RequestHandler\RequestHandlerTrait;
17
use Sf4\Api\Response\ResponseInterface;
18
use Sf4\Api\Response\ResponseTrait;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
22
abstract class AbstractRequest implements RequestInterface
23
{
24
25
    use ResponseTrait;
26
    use DtoTrait;
27
    use RequestHandlerTrait;
28
29
    /** @var Request|null $request */
30
    protected $request;
31
32
    /**
33
     * AbstractRequest constructor.
34
     * @param ResponseInterface $response
35
     * @param DtoInterface $dto
36
     */
37
    public function init(ResponseInterface $response, DtoInterface $dto = null)
38
    {
39
        $response->setRequest($this);
40
        $this->setResponse($response);
41
        if ($dto) {
42
            $this->setDto($dto);
43
            $this->attachDtoToResponse();
44
        }
45
    }
46
47
    protected function attachDtoToResponse(): void
48
    {
49
        $response = $this->getResponse();
50
        $dto = $this->getDto();
51
        if (null !== $dto && null !== $response && null === $response->getDto()) {
52
            $response->setDto($dto);
53
        }
54
    }
55
56
    /**
57
     * @param Closure $closure
58
     * @param string|null $cacheKey
59
     * @param array $tags
60
     * @param int|null $expiresAfter
61
     * @throws CacheException
62
     * @throws InvalidArgumentException
63
     */
64
    public function getCachedResponse(
65
        Closure $closure,
66
        string $cacheKey = null,
67
        array $tags = [],
68
        int $expiresAfter = null
69
    ) {
70
71
        $requestHandler = $this->getRequestHandler();
72
        if ($cacheKey && $requestHandler && $jsonResponse = $requestHandler->getResponse()) {
73
            $data = $requestHandler->getCacheDataOrAdd(
74
                $cacheKey,
75
                static function () use ($closure, $jsonResponse) {
76
                    $closure();
77
                    return $jsonResponse->getContent();
78
                },
79
                $tags,
80
                $expiresAfter
0 ignored issues
show
Bug introduced by
It seems like $expiresAfter can also be of type integer; however, parameter $expiresAfter of Sf4\Api\RequestHandler\T...ce::getCacheDataOrAdd() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                /** @scrutinizer ignore-type */ $expiresAfter
Loading history...
81
            );
82
83
            $responseContent = json_decode($data, true);
84
            $response = $this->getResponse();
85
            if ($response) {
86
                $response->setJsonResponse(
87
                    new JsonResponse(
88
                        $responseContent,
89
                        $response->getResponseStatus(),
90
                        $response->getResponseHeaders()
91
                    )
92
                );
93
            }
94
        } else {
95
            /*
96
             * Handle request
97
             */
98
            $closure();
99
        }
100
    }
101
102
    /**
103
     * @throws CacheException
104
     * @throws InvalidArgumentException
105
     */
106
    public function handle()
107
    {
108
        $response = $this->getResponse();
109
        $requestHandler = $this->getRequestHandler();
110
        if ($response && $requestHandler && $translator = $requestHandler->getTranslator()) {
111
            $response->setTranslator($translator);
112
        }
113
        $this->getCachedResponse(
114
            function () use ($response) {
115
                $httpRequest = $this->getRequest();
116
                if ($httpRequest) {
117
                    $requestContent = $httpRequest->getContent();
118
                    $dto = $this->getDto();
119
                    if ($requestContent && $dto) {
120
                        $data = json_decode($requestContent, true);
121
                        $dto->populate($data);
122
                    }
123
                }
124
                if ($response) {
125
                    $response->init();
126
                }
127
            },
128
            $this->getCacheKey(),
129
            $this->getCacheTags(),
130
            $this->getCacheExpiresAfter()
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getCacheExpiresAfter() targeting Sf4\Api\Request\Abstract...:getCacheExpiresAfter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
131
        );
132
    }
133
134
    /**
135
     * @return Request|null
136
     */
137
    public function getRequest(): ?Request
138
    {
139
        return $this->request;
140
    }
141
142
    /**
143
     * @param Request|null $request
144
     */
145
    public function setRequest(?Request $request): void
146
    {
147
        $this->request = $request;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getRoute(): string
154
    {
155
        return static::ROUTE;
156
    }
157
158
    /**
159
     * @return string|null
160
     */
161
    protected function getCacheKey(): ?string
162
    {
163
        return $this->getUrl();
164
    }
165
166
    /**
167
     * @return array
168
     */
169
    abstract protected function getCacheTags(): array;
170
171
    /**
172
     * @return int|null
173
     */
174
    protected function getCacheExpiresAfter(): ?int
175
    {
176
        return null;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    protected function getUrl(): string
183
    {
184
        return $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
185
    }
186
}
187