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

AbstractClient::buildSerializer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
ccs 5
cts 10
cp 0.5
crap 4.125
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest;
6
7
use Assert\Assert;
8
use CCT\Component\Rest\Exception\InvalidParameterException;
9
use CCT\Component\Rest\Http\RequestInterface;
10
use CCT\Component\Rest\Http\SerializerRequestInterface;
11
use CCT\Component\Rest\Http\Transform\RequestTransform;
12
use CCT\Component\Rest\Http\Transform\ResponseTransform;
13
use CCT\Component\Rest\Serializer\Context\Context;
14
use CCT\Component\Rest\Serializer\JMSSerializerBuilder;
15
use CCT\Component\Rest\Transformer\Request\FormObjectTransformer;
16
use CCT\Component\Rest\Transformer\Response\CollectionObjectTransformer;
17
use CCT\Component\Rest\Transformer\Response\ObjectTransformer;
18
use GuzzleHttp\Client as GuzzleClient;
19
use CCT\Component\Rest\Serializer\SerializerInterface;
20
21
abstract class AbstractClient
22
{
23
    /**
24
     * @var GuzzleClient
25
     */
26
    protected $client;
27
28
    /**
29
     * @var Config
30
     */
31
    protected $config;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $defaultConfig = true;
37
38
    /**
39
     * @var SerializerInterface
40
     */
41
    protected static $serializer;
42
43 2
    public function __construct(Config $config, bool $defaultConfig = true)
44
    {
45 2
        Assert::that($config->toArray())->keyExists(Config::ENDPOINT);
46
47 2
        $this->defaultConfig = $defaultConfig;
48 2
        $this->config = $config;
49 2
        $this->client = new GuzzleClient([
50 2
            'base_uri' => $config->get(Config::ENDPOINT)
51
        ]);
52
53 2
        if ($defaultConfig) {
54 2
            $this->applyDefaults();
55
        }
56 2
    }
57
58
    public function enableDefaultConfig()
59
    {
60
        $this->defaultConfig = true;
61
    }
62
63
    public function disableDefaultConfig()
64
    {
65
        $this->defaultConfig = false;
66
    }
67
68
    public function isDefaultConfig(): bool
69
    {
70
        return $this->defaultConfig;
71
    }
72
73
    abstract protected function applyDefaults();
74
75
    public function clearDefaults()
76
    {
77
        $this->config->remove(Config::METADATA_DIRS);
78
        $this->config->remove(Config::DEBUG);
79
        $this->config->remove(Config::EVENT_SUBSCRIBERS);
80
        $this->config->remove(Config::SERIALIZATION_HANDLERS);
81
        $this->config->remove(Config::OBJECT_CONSTRUCTOR);
82
        $this->config->remove(Config::USE_DEFAULT_RESPONSE_TRANSFORMERS);
83
    }
84
85 1
    protected function buildSerializer(Config $config)
86
    {
87
88 1
        if (class_exists('JMS\Serializer\Serializer')) {
89 1
            return JMSSerializerBuilder::createByConfig($config)
90 1
                ->configureDefaults()
91 1
                ->build();
92
        }
93
94
        if (class_exists('Symfony\Component\Serializer\Serializer')) {
95
            return JMSSerializerBuilder::createByConfig($config)
96
                ->configureDefaults()
97
                ->build();
98
        }
99
100
        return null;
101
    }
102
103 1
    protected function getBuiltSerializer(Config $config)
104
    {
105 1
        if (null === static::$serializer) {
106 1
            static::$serializer = $this->buildSerializer($config);
107
        }
108
109 1
        return static::$serializer;
110
    }
111
112 1
    protected function createRequestInstance($class, Config $config, SerializerInterface $serializer = null)
113
    {
114 1
        $reflectionClass = new \ReflectionClass($class);
115
116 1
        if (!$reflectionClass->implementsInterface(RequestInterface::class)) {
117
            throw new InvalidParameterException(sprintf(
118
                'The class must be an instance of %s',
119
                RequestInterface::class
120
            ));
121
        }
122
123 1
        if (!$reflectionClass->implementsInterface(SerializerRequestInterface::class)) {
124
            return $reflectionClass->newInstance(
125
                $this->client
126
            );
127
        }
128
129 1
        return $reflectionClass->newInstance(
130 1
            $this->client,
131 1
            $config,
132 1
            $serializer,
133 1
            $this->createRequestTransform($config),
134 1
            $this->createResponseTransform($config)
135
        );
136
    }
137
138
    /**
139
     * Should use the default response transformers?
140
     *
141
     * @return bool
142
     */
143 1
    protected function shouldUseDefaultResponseTransformers(): bool
144
    {
145 1
        return (bool)$this->config->get(Config::USE_DEFAULT_RESPONSE_TRANSFORMERS, true);
146
    }
147
148 1
    protected function applyDefaultResponseTransformers(Config $config, SerializerInterface $serializer, $modelClass)
149
    {
150 1
        $config->set(Config::RESPONSE_TRANSFORMERS, [
151 1
            new ObjectTransformer($serializer, $modelClass, new Context()),
152 1
            new CollectionObjectTransformer($serializer, $modelClass, new Context())
153
        ]);
154 1
    }
155
156
    protected function applyDefaultRequestTransformers(Config $config, SerializerInterface $serializer)
157
    {
158
        $config->set(Config::REQUEST_TRANSFORMERS, [
159
            new FormObjectTransformer($serializer, new Context()),
160
        ]);
161
    }
162
163 1
    protected function createRequestTransform(Config $config): ?RequestTransform
164
    {
165 1
        return new RequestTransform(
166 1
            $config->get(Config::REQUEST_TRANSFORMERS, [])
167
        );
168
    }
169
170 1
    protected function createResponseTransform(Config $config): ?ResponseTransform
171
    {
172 1
        return new ResponseTransform(
173 1
            $config->get(Config::RESPONSE_TRANSFORMERS, [])
174
        );
175
    }
176
}
177