Passed
Push — master ( ae14df...9d195a )
by Siim
12:53
created

AbstractRequest::getRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 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(): void
45
    {
46
        $response = $this->getResponse();
47
        $dto = $this->getDto();
48
        if (null !== $dto && null !== $response && null === $response->getDto()) {
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
68
        $requestHandler = $this->getRequestHandler();
69
        if ($cacheKey && $requestHandler && $jsonResponse = $requestHandler->getResponse()) {
70
            $data = $requestHandler->getCacheDataOrAdd(
71
                $cacheKey,
72
                static function () use ($closure, $jsonResponse) {
73
                    $closure();
74
                    return $jsonResponse->getContent();
75
                },
76
                $tags,
77
                $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

77
                /** @scrutinizer ignore-type */ $expiresAfter
Loading history...
78
            );
79
80
            $responseContent = json_decode($data, true);
81
            $response = $this->getResponse();
82
            if ($response) {
83
                $response->setJsonResponse(
84
                    new JsonResponse(
85
                        $responseContent,
86
                        $response->getResponseStatus(),
87
                        $response->getResponseHeaders()
88
                    )
89
                );
90
            }
91
        } else {
92
            /*
93
             * Handle request
94
             */
95
            $closure();
96
        }
97
    }
98
99
    /**
100
     * @throws \Psr\Cache\CacheException
101
     * @throws \Psr\Cache\InvalidArgumentException
102
     */
103
    public function handle()
104
    {
105
        $response = $this->getResponse();
106
        $requestHandler = $this->getRequestHandler();
107
        if ($response && $requestHandler && $translator = $requestHandler->getTranslator()) {
108
            $response->setTranslator($translator);
109
        }
110
        $this->getCachedResponse(
111
            function () use ($response) {
112
                $httpRequest = $this->getRequest();
113
                if ($httpRequest) {
114
                    $requestContent = $httpRequest->getContent();
115
                    $dto = $this->getDto();
116
                    if ($requestContent && $dto) {
117
                        $data = json_decode($requestContent, true);
118
                        $dto->populate($data);
119
                    }
120
                }
121
                if ($response) {
122
                    $response->init();
123
                }
124
            },
125
            $this->getCacheKey(),
126
            $this->getCacheTags(),
127
            $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...
128
        );
129
    }
130
131
    /**
132
     * @return Request|null
133
     */
134
    public function getRequest(): ?Request
135
    {
136
        return $this->request;
137
    }
138
139
    /**
140
     * @param Request|null $request
141
     */
142
    public function setRequest(?Request $request): void
143
    {
144
        $this->request = $request;
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getRoute(): string
151
    {
152
        return static::ROUTE;
153
    }
154
155
    /**
156
     * @return string|null
157
     */
158
    protected function getCacheKey(): ?string
159
    {
160
        return $this->getUrl();
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    abstract protected function getCacheTags(): array;
167
168
    /**
169
     * @return int|null
170
     */
171
    protected function getCacheExpiresAfter(): ?int
172
    {
173
        return null;
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    protected function getUrl(): string
180
    {
181
        return $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
182
    }
183
}
184