Passed
Push — master ( 176634...e5b2a3 )
by Paul
05:47
created

JMSSerializerBuilder   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Test Coverage

Coverage 48.75%

Importance

Changes 0
Metric Value
dl 0
loc 198
ccs 39
cts 80
cp 0.4875
rs 10
c 0
b 0
f 0
wmc 25

15 Methods

Rating   Name   Duplication   Size   Complexity  
A addMetadataDir() 0 11 1
A createByConfig() 0 3 1
A __construct() 0 4 1
A removeMetadataDir() 0 13 3
A build() 0 13 1
A removeEventSubscribers() 0 13 3
A applyEventSubscribers() 0 10 2
A removeSerializationHandler() 0 13 3
A applyMetadataDirectory() 0 7 2
A configureDefaults() 0 6 1
A setObjectConstructor() 0 5 1
A addSerializationHandler() 0 7 1
A applySerializationHandlers() 0 12 2
A applyObjectConstructor() 0 8 2
A addEventSubscribers() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest\Serializer;
6
7
use CCT\Component\Rest\Config;
8
use JMS\Serializer\Construction\ObjectConstructorInterface;
9
use JMS\Serializer\EventDispatcher\EventDispatcher;
10
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
11
use JMS\Serializer\Handler\HandlerRegistry;
12
use JMS\Serializer\Handler\SubscribingHandlerInterface;
13
use JMS\Serializer\SerializerBuilder;
14
15
class JMSSerializerBuilder implements SerializerBuilderInterface
16
{
17
    /**
18
     * @var Config
19
     */
20
    protected $config;
21
22
    /**
23
     * @var SerializerBuilder
24
     */
25
    protected $jmsSerializerBuilder;
26
27 24
    public function __construct(Config $config)
28
    {
29 24
        $this->config = $config;
30 24
        $this->jmsSerializerBuilder = SerializerBuilder::create();
31 24
    }
32
33 24
    public static function createByConfig(Config $config)
34
    {
35 24
        return new static($config);
36
    }
37
38
    public function addMetadataDir($dir, $namespacePrefix)
39
    {
40
        $metadataDirs = $this->config->get(Config::METADATA_DIRS, []);
41
        $metadataDirs[] = [
42
            'dir' => $dir,
43
            'namespacePrefix' => $namespacePrefix
44
        ];
45
46
        $this->config->set(Config::METADATA_DIRS, $metadataDirs);
47
48
        return $this;
49
    }
50
51
    public function removeMetadataDir($dir)
52
    {
53
        $metadataDirs = $this->config->get(Config::METADATA_DIRS, []);
54
55
        foreach ($metadataDirs as $key => $metadataDir) {
56
            if ($metadataDir['dir'] === $dir) {
57
                unset($metadataDirs[$key]);
58
            }
59
        }
60
61
        $this->config->set(Config::METADATA_DIRS, $metadataDirs);
62
63
        return $this;
64
    }
65
66
    public function addEventSubscribers(EventSubscriberInterface $eventSubscriber)
67
    {
68
        $this->config->set(Config::EVENT_SUBSCRIBERS, [$eventSubscriber]);
69
70
        return $this;
71
    }
72
73
    public function removeEventSubscribers(EventSubscriberInterface $eventSubscriber)
74
    {
75
        $eventSubscribers = $this->config->get(Config::EVENT_SUBSCRIBERS, []);
76
77
        foreach ($eventSubscribers as $key => $storedEventSubscribers) {
78
            if (\get_class($storedEventSubscribers) === \get_class($eventSubscriber)) {
79
                unset($eventSubscribers[$key]);
80
            }
81
        }
82
83
        $this->config->set(Config::EVENT_SUBSCRIBERS, $eventSubscribers);
84
85
        return $this;
86
    }
87
88
    public function addSerializationHandler(SubscribingHandlerInterface $subscribingHandler)
89
    {
90
        $this->config->merge([
0 ignored issues
show
Bug introduced by
The method merge() does not exist on CCT\Component\Rest\Config. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        $this->config->/** @scrutinizer ignore-call */ 
91
                       merge([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
            Config::SERIALIZATION_HANDLERS => [$subscribingHandler]
92
        ]);
93
94
        return $this;
95
    }
96
97
    public function removeSerializationHandler(SubscribingHandlerInterface $subscribingHandler)
98
    {
99
        $subscribingHandlers = $this->config->get(Config::SERIALIZATION_HANDLERS, []);
100
101
        foreach ($subscribingHandlers as $key => $storedSubscribingHandlers) {
102
            if (\get_class($storedSubscribingHandlers) === \get_class($subscribingHandler)) {
103
                unset($subscribingHandlers[$key]);
104
            }
105
        }
106
107
        $this->config->set(Config::SERIALIZATION_HANDLERS, $subscribingHandlers);
108
109
        return $this;
110
    }
111
112
    public function setObjectConstructor(ObjectConstructorInterface $objectConstructor)
113
    {
114
        $this->config->set(Config::OBJECT_CONSTRUCTOR, $objectConstructor);
115
116
        return $this;
117
    }
118
119 24
    public function configureDefaults()
120
    {
121 24
        $this->jmsSerializerBuilder->addDefaultHandlers();
122 24
        $this->jmsSerializerBuilder->addDefaultListeners();
123
124 24
        return $this;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     * @throws \JMS\Serializer\Exception\InvalidArgumentException
130
     * @throws \JMS\Serializer\Exception\RuntimeException
131
     */
132 24
    public function build(): SerializerInterface
133
    {
134 24
        $this->jmsSerializerBuilder->setDebug($this->config->get('debug', false));
135
136
        $this
137 24
            ->applyMetadataDirectory()
138 24
            ->applyEventSubscribers()
139 24
            ->applySerializationHandlers()
140 24
            ->applyObjectConstructor();
141
142 24
        $serializer = $this->jmsSerializerBuilder->build();
143
144 24
        return new JMSSerializerAdapter($serializer);
145
    }
146
147
    /**
148
     * Apply Metadata Directories
149
     *
150
     * @return $this
151
     * @throws \JMS\Serializer\Exception\InvalidArgumentException
152
     */
153 24
    protected function applyMetadataDirectory(): self
154
    {
155 24
        foreach ($this->config->get(Config::METADATA_DIRS, []) as $metadataDir) {
156 24
            $this->jmsSerializerBuilder->addMetadataDir($metadataDir['dir'], $metadataDir['namespacePrefix']);
157
        }
158
159 24
        return $this;
160
    }
161
162
    /**
163
     * Apply event subscribers
164
     *
165
     * @return $this
166
     * @throws \JMS\Serializer\Exception\InvalidArgumentException
167
     */
168 24
    protected function applyEventSubscribers(): self
169
    {
170 24
        $eventSubscribers = $this->config->get(Config::EVENT_SUBSCRIBERS, []);
171 24
        $this->jmsSerializerBuilder->configureListeners(function (EventDispatcher $dispatcher) use ($eventSubscribers) {
172 24
            foreach ($eventSubscribers as $eventSubscriber) {
173
                $dispatcher->addSubscriber($eventSubscriber);
174
            }
175 24
        });
176
177 24
        return $this;
178
    }
179
180
    /**
181
     * Apply serialization handlers
182
     *
183
     * @return $this
184
     * @throws \JMS\Serializer\Exception\RuntimeException
185
     */
186 24
    protected function applySerializationHandlers(): self
187
    {
188 24
        $serializationHandlers = $this->config->get(Config::SERIALIZATION_HANDLERS, []);
189 24
        $this->jmsSerializerBuilder->configureHandlers(
190 24
            function (HandlerRegistry $handlerRegistry) use ($serializationHandlers) {
191 24
                foreach ($serializationHandlers as $handler) {
192
                    $handlerRegistry->registerSubscribingHandler($handler);
193
                }
194 24
            }
195
        );
196
197 24
        return $this;
198
    }
199
200
    /**
201
     * Apply object constructor
202
     *
203
     * @return $this
204
     */
205 24
    protected function applyObjectConstructor(): self
206
    {
207 24
        $objectConstructor = $this->config->get(Config::OBJECT_CONSTRUCTOR, null);
208 24
        if (null !== $objectConstructor) {
209
            $this->jmsSerializerBuilder->setObjectConstructor($objectConstructor);
210
        }
211
212 24
        return $this;
213
    }
214
}
215