Passed
Push — master ( 312ccc...32eec2 )
by Paul
05:50
created

AbstractSerializerRequest::appendToUri()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
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 CCT\Component\Rest\Config;
8
use CCT\Component\Rest\Http\Definition\QueryParams;
9
use CCT\Component\Rest\Http\Transform\RequestTransformInterface;
10
use CCT\Component\Rest\Http\Transform\ResponseTransformInterface;
11
use CCT\Component\Rest\Serializer\Context\Context;
12
use CCT\Component\Rest\Serializer\SerializerInterface;
13
use GuzzleHttp\Client as GuzzleClient;
14
15
abstract class AbstractSerializerRequest extends AbstractRequest implements SerializerRequestInterface
16
{
17
    /**
18
     * @var SerializerInterface
19
     */
20
    protected $serializer;
21
22
    /**
23
     * @var RequestTransformInterface
24
     */
25
    protected $requestTransform;
26
27
    /**
28
     * @var ResponseTransformInterface
29
     */
30
    protected $responseTransform;
31
32
    /**
33
     * AbstractSerializerRequest constructor.
34
     *
35
     * @param GuzzleClient $client
36
     * @param Config $config
37
     * @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...
38
     * @param RequestTransformInterface $requestTransform
39
     * @param ResponseTransformInterface $responseTransform
40
     */
41 4
    public function __construct(
42
        GuzzleClient $client,
43
        Config $config,
44
        $serializer = null,
45
        RequestTransformInterface $requestTransform = null,
46
        ResponseTransformInterface $responseTransform = null
47
    ) {
48 4
        parent::__construct($client, $config);
49
50 4
        $this->serializer = $serializer;
51 4
        $this->requestTransform = $requestTransform;
52 4
        $this->responseTransform = $responseTransform;
53 4
    }
54
55
    /**
56
     * @param string $method
57
     * @param string $uri
58
     * @param array $formData
59
     * @param QueryParams|null $queryParams
60
     *
61
     * @return ResponseInterface|\Symfony\Component\HttpFoundation\Response
62
     */
63 3
    protected function execute($method, string $uri, $formData = [], QueryParams $queryParams = null)
64
    {
65 3
        $response = parent::execute($method, $uri, $formData, $queryParams);
66
67 3
        if (null !== $this->responseTransform) {
68 2
            $this->responseTransform->transform(
69 2
                $response,
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type Symfony\Component\HttpFoundation\Response; however, parameter $response of CCT\Component\Rest\Http\...mInterface::transform() does only seem to accept CCT\Component\Rest\Http\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

69
                /** @scrutinizer ignore-type */ $response,
Loading history...
70 2
                $this->config->get('serialization_context')
71
            );
72
        }
73
74 3
        $this->config->set('serialization_context', []);
75
76 3
        return $response;
77
    }
78
79
    /**
80
     * @param array|object $formData
81
     * @param QueryParams|null $queryParams
82
     *
83
     * @return array
84
     */
85 3
    protected function getRequestOptions($formData = [], QueryParams $queryParams = null)
86
    {
87 3
        if (null !== $this->requestTransform) {
88 2
            $formData = $this->requestTransform->transform(
89 2
                $formData,
90 2
                $this->config->get('serialization_context')
91
            );
92
        }
93
94 3
        return parent::getRequestOptions($formData, $queryParams);
95
    }
96
97
    /**
98
     * Sets the Serialization context based in the groups the request should deal with.
99
     *
100
     * @param array $groups
101
     *
102
     * @return void
103
     */
104
    protected function setSerializationContextFor(array $groups = []): void
105
    {
106
        $serializationContext = Context::create()->setGroups($groups);
107
108
        $this->config->set('serialization_context', $serializationContext);
109
    }
110
}
111