setSerializationContextFor()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
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 SerializerInterface|null $serializer
38
     * @param RequestTransformInterface $requestTransform
39
     * @param ResponseTransformInterface $responseTransform
40
     */
41 4
    public function __construct(
42
        GuzzleClient $client,
43
        Config $config,
44
        SerializerInterface $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
62
     * @throws \ReflectionException
63
     * @throws \CCT\Component\Rest\Exception\InvalidParameterException
64
     * @throws \RuntimeException
65
     * @throws \GuzzleHttp\Exception\RequestException
66
     * @throws \CCT\Component\Rest\Exception\ServiceUnavailableException
67
     */
68 3
    protected function execute(
69
        $method,
70
        string $uri,
71
        $formData = [],
72
        QueryParams $queryParams = null
73
    ): ResponseInterface {
74 3
        $response = parent::execute($method, $uri, $formData, $queryParams);
75
76 3
        if (null !== $this->responseTransform) {
77 2
            $this->responseTransform->transform(
78 2
                $response,
79 2
                $this->config->get('serialization_context')
80
            );
81
        }
82
83 3
        $this->config->set('serialization_context', []);
84
85 3
        return $response;
86
    }
87
88
    /**
89
     * @param array|object $formData
90
     * @param QueryParams|null $queryParams
91
     *
92
     * @return array
93
     */
94 3
    protected function getRequestOptions($formData = [], QueryParams $queryParams = null): array
95
    {
96 3
        if (null !== $this->requestTransform) {
97 2
            $formData = $this->requestTransform->transform(
98 2
                $formData,
99 2
                $this->config->get('serialization_context')
100
            );
101
        }
102
103 3
        return parent::getRequestOptions($formData, $queryParams);
104
    }
105
106
    /**
107
     * Sets the Serialization context based in the groups the request should deal with.
108
     *
109
     * @param array $groups
110
     *
111
     * @return void
112
     */
113
    protected function setSerializationContextFor(array $groups = []): void
114
    {
115
        $serializationContext = Context::create()->setGroups($groups);
116
117
        $this->config->set('serialization_context', $serializationContext);
118
    }
119
}
120