Passed
Push — master ( 3df341...332155 )
by Siim
17:52 queued 02:54
created

AbstractRequest::setRequest()   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 1
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()
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()
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...
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->getUrl();
149
    }
150
151
    abstract protected function getCacheTags(): array;
152
153
    protected function getCacheExpiresAfter(): ?int
154
    {
155
        return null;
156
    }
157
158
    protected function getUrl(): string
159
    {
160
        return $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
161
    }
162
}
163