shouldUseDefaultResponseTransformers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest;
6
7
use CCT\Component\Rest\Exception\InvalidParameterException;
8
use CCT\Component\Rest\Http\RequestInterface;
9
use CCT\Component\Rest\Http\SerializerRequestInterface;
10
use CCT\Component\Rest\Http\Transform\RequestTransform;
11
use CCT\Component\Rest\Http\Transform\ResponseTransform;
12
use CCT\Component\Rest\Serializer\Context\Context;
13
use CCT\Component\Rest\Serializer\JMSSerializerBuilder;
14
use CCT\Component\Rest\Transformer\Request\FormObjectTransformer;
15
use CCT\Component\Rest\Transformer\Response\ObjectCollectionTransformer;
16
use CCT\Component\Rest\Transformer\Response\ObjectTransformer;
17
use GuzzleHttp\Client as GuzzleClient;
18
use CCT\Component\Rest\Serializer\SerializerInterface;
19
20
abstract class AbstractClient
21
{
22
    /**
23
     * @var GuzzleClient
24
     */
25
    protected $client;
26
27
    /**
28
     * @var Config
29
     */
30
    protected $config;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $defaultConfig = true;
36
37
    /**
38
     * @var SerializerInterface
39
     */
40
    protected static $serializer;
41
42
    /**
43
     * AbstractClient constructor.
44
     *
45
     * @param Config $config
46
     * @param bool $defaultConfig
47
     *
48
     * @throws \InvalidArgumentException
49
     */
50 2
    public function __construct(Config $config, bool $defaultConfig = true)
51
    {
52 2
        if (false === $config->has(Config::ENDPOINT)) {
53 1
            throw new \InvalidArgumentException(
54 1
                sprintf('Configuration key %s is missing', Config::ENDPOINT)
55
            );
56
        }
57
58 2
        $this->defaultConfig = $defaultConfig;
59 2
        $this->config = $config;
60 2
        $this->client = new GuzzleClient([
61 2
            'base_uri' => $config->get(Config::ENDPOINT),
62 2
            'verify' => $config->get(Config::CURL_CA_VERIFY, true)
63
        ]);
64
65 2
        if ($defaultConfig) {
66 2
            $this->applyDefaults();
67
        }
68 2
    }
69
70
    /**
71
     * Enable Default Config
72
     */
73
    public function enableDefaultConfig(): void
74
    {
75
        $this->defaultConfig = true;
76
    }
77
78
    /**
79
     * Disabled default config
80
     */
81
    public function disableDefaultConfig(): void
82
    {
83
        $this->defaultConfig = false;
84
    }
85
86
    /**
87
     * Is default config enabled
88
     *
89
     * @return bool
90
     */
91
    public function isDefaultConfig(): bool
92
    {
93
        return $this->defaultConfig;
94
    }
95
96
    /**
97
     * Applies the default config
98
     *
99
     * @return mixed
100
     */
101
    abstract protected function applyDefaults();
102
103
    /**
104
     * Clears Config values
105
     */
106
    public function clearDefaults(): void
107
    {
108
        $this->config->remove(Config::METADATA_DIRS);
109
        $this->config->remove(Config::DEBUG);
110
        $this->config->remove(Config::EVENT_SUBSCRIBERS);
111
        $this->config->remove(Config::SERIALIZATION_HANDLERS);
112
        $this->config->remove(Config::OBJECT_CONSTRUCTOR);
113
        $this->config->remove(Config::USE_DEFAULT_RESPONSE_TRANSFORMERS);
114
    }
115
116
    /**
117
     * @param Config $config
118
     *
119
     * @return SerializerInterface|null
120
     * @throws \JMS\Serializer\Exception\RuntimeException
121
     * @throws \JMS\Serializer\Exception\InvalidArgumentException
122
     */
123 1
    protected function buildSerializer(Config $config): ?SerializerInterface
124
    {
125
126 1
        if (class_exists('JMS\Serializer\Serializer')) {
127 1
            return JMSSerializerBuilder::createByConfig($config)
128 1
                ->configureDefaults()
129 1
                ->build();
130
        }
131
132
        if (class_exists('Symfony\Component\Serializer\Serializer')) {
133
            return JMSSerializerBuilder::createByConfig($config)
134
                ->configureDefaults()
135
                ->build();
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * Gets the built serializer
143
     *
144
     * @param Config $config
145
     *
146
     * @return SerializerInterface|null
147
     * @throws \JMS\Serializer\Exception\RuntimeException
148
     * @throws \JMS\Serializer\Exception\InvalidArgumentException
149
     */
150 1
    protected function getBuiltSerializer(Config $config): ?SerializerInterface
151
    {
152 1
        if (null === static::$serializer) {
153 1
            static::$serializer = $this->buildSerializer($config);
154
        }
155
156 1
        return static::$serializer;
157
    }
158
159
    /**
160
     * @param $class
161
     * @param Config $config
162
     * @param SerializerInterface|null $serializer
163
     *
164
     * @return object
165
     * @throws \ReflectionException
166
     * @throws \CCT\Component\Rest\Exception\InvalidParameterException
167
     */
168 1
    protected function createRequestInstance($class, Config $config, SerializerInterface $serializer = null)
169
    {
170 1
        $reflectionClass = new \ReflectionClass($class);
171
172 1
        if (!$reflectionClass->implementsInterface(RequestInterface::class)) {
173
            throw new InvalidParameterException(sprintf(
174
                'The class must be an instance of %s',
175
                RequestInterface::class
176
            ));
177
        }
178
179 1
        if (!$reflectionClass->implementsInterface(SerializerRequestInterface::class)) {
180
            return $reflectionClass->newInstance(
181
                $this->client
182
            );
183
        }
184
185 1
        return $reflectionClass->newInstance(
186 1
            $this->client,
187 1
            $config,
188 1
            $serializer,
189 1
            $this->createRequestTransform($config),
190 1
            $this->createResponseTransform($config)
191
        );
192
    }
193
194
    /**
195
     * Should use the default response transformers?
196
     *
197
     * @return bool
198
     */
199 1
    protected function shouldUseDefaultResponseTransformers(): bool
200
    {
201 1
        return (bool)$this->config->get(Config::USE_DEFAULT_RESPONSE_TRANSFORMERS, true);
202
    }
203
204
    /**
205
     * @param Config $config
206
     * @param SerializerInterface $serializer
207
     * @param $modelClass
208
     */
209 1
    protected function applyDefaultResponseTransformers(
210
        Config $config,
211
        SerializerInterface $serializer,
212
        $modelClass
213
    ): void {
214 1
        $config->set(Config::RESPONSE_TRANSFORMERS, [
215 1
            new ObjectTransformer($serializer, $modelClass, new Context()),
216 1
            new ObjectCollectionTransformer($serializer, $modelClass, new Context())
217
        ]);
218 1
    }
219
220
    /**
221
     * @param Config $config
222
     * @param SerializerInterface $serializer
223
     */
224
    protected function applyDefaultRequestTransformers(Config $config, SerializerInterface $serializer): void
225
    {
226
        $config->set(Config::REQUEST_TRANSFORMERS, [
227
            new FormObjectTransformer($serializer, new Context()),
228
        ]);
229
    }
230
231
    /**
232
     * @param Config $config
233
     *
234
     * @return RequestTransform|null
235
     */
236 1
    protected function createRequestTransform(Config $config): ?RequestTransform
237
    {
238 1
        return new RequestTransform(
239 1
            $config->get(Config::REQUEST_TRANSFORMERS, [])
240
        );
241
    }
242
243
    /**
244
     * @param Config $config
245
     *
246
     * @return ResponseTransform|null
247
     */
248 1
    protected function createResponseTransform(Config $config): ?ResponseTransform
249
    {
250 1
        return new ResponseTransform(
251 1
            $config->get(Config::RESPONSE_TRANSFORMERS, [])
252
        );
253
    }
254
}
255