Passed
Push — master ( fe8874...312ccc )
by Paul
02:35
created

setSerializationContextFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest\Http;
6
7
use Assert\Assert;
8
use CCT\Component\Rest\Config;
9
use CCT\Component\Rest\Exception\InvalidParameterException;
10
use CCT\Component\Rest\Http\Definition\QueryParams;
11
use CCT\Component\Rest\Http\Transform\RequestTransformInterface;
12
use CCT\Component\Rest\Http\Transform\ResponseTransformInterface;
13
use CCT\Component\Rest\Serializer\Context\Context;
14
use CCT\Component\Rest\Serializer\SerializerInterface;
15
use GuzzleHttp\Client as GuzzleClient;
16
use GuzzleHttp\Psr7\Uri;
17
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
18
19
abstract class AbstractSerializerRequest extends AbstractRequest implements SerializerRequestInterface
20
{
21
    /**
22
     * @var SerializerInterface
23
     */
24
    protected $serializer;
25
26
    /**
27
     * @var Config
28
     */
29
    protected $config;
30
31
    /**
32
     * @var RequestTransformInterface
33
     */
34
    protected $requestTransform;
35
36
    /**
37
     * @var ResponseTransformInterface
38
     */
39
    protected $responseTransform;
40
41
    /**
42
     * AbstractSerializerRequest constructor.
43
     *
44
     * @param GuzzleClient $client
45
     * @param Config $config
46
     * @param null $serializer
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $serializer is correct as it would always require null to be passed?
Loading history...
47
     * @param RequestTransformInterface $requestTransform
48
     * @param ResponseTransformInterface $responseTransform
49
     */
50 4
    public function __construct(
51
        GuzzleClient $client,
52
        Config $config,
53
        $serializer = null,
54
        RequestTransformInterface $requestTransform = null,
55
        ResponseTransformInterface $responseTransform = null
56
    ) {
57 4
        parent::__construct($client);
58
59 4
        $this->serializer = $serializer;
60 4
        $this->config = $config;
61
62 4
        $this->setUp();
63 4
        $this->validateConfig();
64
65 4
        $this->requestTransform = $requestTransform;
66
67 4
        $this->responseTransform = $responseTransform;
68 4
    }
69
70
    /**
71
     * @return string|null
72
     */
73 3
    public function getUri()
74
    {
75 3
        return $this->config->get(Config::URI_PREFIX);
76
    }
77
78
    /**
79
     * @param string $method
80
     * @param string $uri
81
     * @param array|object $formData
82
     * @param QueryParams|null $queryParams
83
     *
84
     * @return ResponseInterface
85
     */
86 3
    protected function execute($method, string $uri, $formData = [], QueryParams $queryParams = null)
87
    {
88 3
        $response = $this->createResponseRefFromResponse(
89 3
            parent::execute($method, $uri, $formData, $queryParams)
0 ignored issues
show
Bug introduced by
It seems like parent::execute($method,...formData, $queryParams) can also be of type Symfony\Component\HttpFoundation\Response; however, parameter $response of CCT\Component\Rest\Http\...sponseRefFromResponse() does only seem to accept Psr\Http\Message\ResponseInterface, 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

89
            /** @scrutinizer ignore-type */ parent::execute($method, $uri, $formData, $queryParams)
Loading history...
90
        );
91
92 3
        if (null !== $this->responseTransform) {
93 2
            $this->responseTransform->transform($response);
94
        }
95 3
        $this->config->set('serialization_context', []);
96
97 3
        return $response;
98
    }
99
100
    /**
101
     * @param array|object $formData
102
     *
103
     * @return array
104
     */
105 3
    protected function getRequestOptions($formData = [])
106
    {
107 3
        if (null !== $this->requestTransform) {
108 2
            $formData = $this->requestTransform->transform($formData);
109
        }
110
111
        return [
112 3
            'form_params' => $formData,
113 3
            'headers' => $this->getHeaders()->toArray()
114
        ];
115
    }
116
117
    /**
118
     * Create Response reflection from a response
119
     *
120
     * @param PsrResponseInterface $response
121
     *
122
     * @return ResponseInterface|object
123
     */
124 3
    protected function createResponseRefFromResponse(PsrResponseInterface $response)
125
    {
126 3
        $responseRef = $this->createResponseReflectionInstance();
127
128 3
        return $responseRef->newInstance(
129 3
            $response->getBody()->getContents(),
130 3
            $response->getStatusCode(),
131 3
            $response->getHeaders()
132
        );
133
    }
134
135
    /**
136
     * @param string $uri
137
     *
138
     * @return string
139
     */
140
    protected function formatUri(string $uri): string
141
    {
142
        $baseUri = $this->client->getConfig('base_uri');
143
144
        // todo: review
145
        return ($baseUri instanceof Uri && $baseUri->getPath())
146
            ? rtrim($baseUri->getPath(), '/') . '/' . ltrim($uri, '/')
147
            : $uri;
148
    }
149
150
    /**
151
     * Appends new parameters to the URI.
152
     *
153
     * @param string $complement
154
     * @param string|null $uri
155
     *
156
     * @return string
157
     */
158
    protected function appendToUri(string $complement, ?string $uri = null)
159
    {
160
        $uri = $uri ?: $this->config->get(Config::URI_PREFIX);
161
162
        return sprintf(
163
            '%s/%s',
164
            rtrim($uri, '/'),
165
            ltrim($complement, '/')
166
        );
167
    }
168
169
    /**
170
     * Sets the Serialization context based in the groups the request should deal with.
171
     *
172
     * @param array $groups
173
     *
174
     * @return void
175
     */
176
    protected function setSerializationContextFor(array $groups = []): void
177
    {
178
        $serializationContext = Context::create()->setGroups($groups);
179
180
        $this->config->set('serialization_context', $serializationContext);
181
    }
182
183
    /**
184
     * Creates a Reflection Response class.
185
     *
186
     * @return \ReflectionClass
187
     */
188 3
    private function createResponseReflectionInstance(): \ReflectionClass
189
    {
190 3
        $responseClass = $this->config->get(Config::RESPONSE_CLASS, Response::class);
191 3
        $responseRef = new \ReflectionClass($responseClass);
192
193 3
        if (!$responseRef->implementsInterface(ResponseInterface::class)) {
194
            throw new InvalidParameterException(sprintf(
195
                'The response class must be an implementation of %s',
196
                ResponseInterface::class
197
            ));
198
        }
199
200 3
        return $responseRef;
201
    }
202
203
    /**
204
     * Validates the required parameters from Config file.
205
     *
206
     * @return void
207
     */
208 4
    private function validateConfig()
209
    {
210 4
        Assert::lazy()
211 4
            ->that($this->config->toArray(), Config::URI_PREFIX)->keyExists(Config::URI_PREFIX)
212 4
            ->verifyNow();
213 4
    }
214
215
    /**
216
     * Initialization of the request.
217
     *
218
     * @return void
219
     */
220
    abstract protected function setUp();
221
}
222