Passed
Push — master ( 354d5b...4607a5 )
by Reyo
02:27
created

ProjectNormalizer   F

Complexity

Total Complexity 184

Size/Duplication

Total Lines 402
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 307
c 1
b 0
f 0
dl 0
loc 402
ccs 0
cts 392
cp 0
rs 2
wmc 184

How to fix   Complexity   

Complex Class

Complex classes like ProjectNormalizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ProjectNormalizer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the timechimp bundle package.
7
 * (c) Connect Holland.
8
 */
9
10
namespace ConnectHolland\TimechimpBundle\Api\Normalizer;
11
12
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18
19
class ProjectNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface
20
{
21
    use DenormalizerAwareTrait;
22
    use NormalizerAwareTrait;
23
24
    public function supportsDenormalization($data, $type, $format = null)
25
    {
26
        return $type === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\Project';
27
    }
28
29
    public function supportsNormalization($data, $format = null)
30
    {
31
        return is_object($data) && get_class($data) === 'ConnectHolland\\TimechimpBundle\\Api\\Model\\Project';
32
    }
33
34
    public function denormalize($data, $class, $format = null, array $context = [])
35
    {
36
        if (!is_object($data)) {
37
            return null;
38
        }
39
        $object = new \ConnectHolland\TimechimpBundle\Api\Model\Project();
40
        if (property_exists($data, 'remainingBudgetHours') && $data->{'remainingBudgetHours'} !== null) {
41
            $object->setRemainingBudgetHours($data->{'remainingBudgetHours'});
42
        } elseif (property_exists($data, 'remainingBudgetHours') && $data->{'remainingBudgetHours'} === null) {
43
            $object->setRemainingBudgetHours(null);
44
        }
45
        if (property_exists($data, 'tagIds') && $data->{'tagIds'} !== null) {
46
            $values = [];
47
            foreach ($data->{'tagIds'} as $value) {
48
                $values[] = $value;
49
            }
50
            $object->setTagIds($values);
51
        } elseif (property_exists($data, 'tagIds') && $data->{'tagIds'} === null) {
52
            $object->setTagIds(null);
53
        }
54
        if (property_exists($data, 'tagNames') && $data->{'tagNames'} !== null) {
55
            $values_1 = [];
56
            foreach ($data->{'tagNames'} as $value_1) {
57
                $values_1[] = $value_1;
58
            }
59
            $object->setTagNames($values_1);
60
        } elseif (property_exists($data, 'tagNames') && $data->{'tagNames'} === null) {
61
            $object->setTagNames(null);
62
        }
63
        if (property_exists($data, 'unspecified') && $data->{'unspecified'} !== null) {
64
            $object->setUnspecified($data->{'unspecified'});
65
        } elseif (property_exists($data, 'unspecified') && $data->{'unspecified'} === null) {
66
            $object->setUnspecified(null);
67
        }
68
        if (property_exists($data, 'invoiceDate') && $data->{'invoiceDate'} !== null) {
69
            $object->setInvoiceDate(\DateTime::createFromFormat('Y-m-d\\TH:i:s', $data->{'invoiceDate'}));
70
        } elseif (property_exists($data, 'invoiceDate') && $data->{'invoiceDate'} === null) {
71
            $object->setInvoiceDate(null);
72
        }
73
        if (property_exists($data, 'invoiceInInstallments') && $data->{'invoiceInInstallments'} !== null) {
74
            $object->setInvoiceInInstallments($data->{'invoiceInInstallments'});
75
        } elseif (property_exists($data, 'invoiceInInstallments') && $data->{'invoiceInInstallments'} === null) {
76
            $object->setInvoiceInInstallments(null);
77
        }
78
        if (property_exists($data, 'budgetNotificationPercentage') && $data->{'budgetNotificationPercentage'} !== null) {
79
            $object->setBudgetNotificationPercentage($data->{'budgetNotificationPercentage'});
80
        } elseif (property_exists($data, 'budgetNotificationPercentage') && $data->{'budgetNotificationPercentage'} === null) {
81
            $object->setBudgetNotificationPercentage(null);
82
        }
83
        if (property_exists($data, 'budgetNotificationHasBeenSent') && $data->{'budgetNotificationHasBeenSent'} !== null) {
84
            $object->setBudgetNotificationHasBeenSent($data->{'budgetNotificationHasBeenSent'});
85
        } elseif (property_exists($data, 'budgetNotificationHasBeenSent') && $data->{'budgetNotificationHasBeenSent'} === null) {
86
            $object->setBudgetNotificationHasBeenSent(null);
87
        }
88
        if (property_exists($data, 'clientId') && $data->{'clientId'} !== null) {
89
            $object->setClientId($data->{'clientId'});
90
        } elseif (property_exists($data, 'clientId') && $data->{'clientId'} === null) {
91
            $object->setClientId(null);
92
        }
93
        if (property_exists($data, 'invoiceStatus') && $data->{'invoiceStatus'} !== null) {
94
            $object->setInvoiceStatus($data->{'invoiceStatus'});
95
        } elseif (property_exists($data, 'invoiceStatus') && $data->{'invoiceStatus'} === null) {
96
            $object->setInvoiceStatus(null);
97
        }
98
        if (property_exists($data, 'invoiceId') && $data->{'invoiceId'} !== null) {
99
            $object->setInvoiceId($data->{'invoiceId'});
100
        } elseif (property_exists($data, 'invoiceId') && $data->{'invoiceId'} === null) {
101
            $object->setInvoiceId(null);
102
        }
103
        if (property_exists($data, 'color') && $data->{'color'} !== null) {
104
            $object->setColor($data->{'color'});
105
        } elseif (property_exists($data, 'color') && $data->{'color'} === null) {
106
            $object->setColor(null);
107
        }
108
        if (property_exists($data, 'visibleOnSchedule') && $data->{'visibleOnSchedule'} !== null) {
109
            $object->setVisibleOnSchedule($data->{'visibleOnSchedule'});
110
        } elseif (property_exists($data, 'visibleOnSchedule') && $data->{'visibleOnSchedule'} === null) {
111
            $object->setVisibleOnSchedule(null);
112
        }
113
        if (property_exists($data, 'externalUrl') && $data->{'externalUrl'} !== null) {
114
            $object->setExternalUrl($data->{'externalUrl'});
115
        } elseif (property_exists($data, 'externalUrl') && $data->{'externalUrl'} === null) {
116
            $object->setExternalUrl(null);
117
        }
118
        if (property_exists($data, 'externalName') && $data->{'externalName'} !== null) {
119
            $object->setExternalName($data->{'externalName'});
120
        } elseif (property_exists($data, 'externalName') && $data->{'externalName'} === null) {
121
            $object->setExternalName(null);
122
        }
123
        if (property_exists($data, 'invoiceReference') && $data->{'invoiceReference'} !== null) {
124
            $object->setInvoiceReference($data->{'invoiceReference'});
125
        } elseif (property_exists($data, 'invoiceReference') && $data->{'invoiceReference'} === null) {
126
            $object->setInvoiceReference(null);
127
        }
128
        if (property_exists($data, 'projectTasks') && $data->{'projectTasks'} !== null) {
129
            $values_2 = [];
130
            foreach ($data->{'projectTasks'} as $value_2) {
131
                $values_2[] = $this->denormalizer->denormalize($value_2, 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectTask', 'json', $context);
132
            }
133
            $object->setProjectTasks($values_2);
134
        } elseif (property_exists($data, 'projectTasks') && $data->{'projectTasks'} === null) {
135
            $object->setProjectTasks(null);
136
        }
137
        if (property_exists($data, 'projectUsers') && $data->{'projectUsers'} !== null) {
138
            $values_3 = [];
139
            foreach ($data->{'projectUsers'} as $value_3) {
140
                $values_3[] = $this->denormalizer->denormalize($value_3, 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectUser', 'json', $context);
141
            }
142
            $object->setProjectUsers($values_3);
143
        } elseif (property_exists($data, 'projectUsers') && $data->{'projectUsers'} === null) {
144
            $object->setProjectUsers(null);
145
        }
146
        if (property_exists($data, 'id') && $data->{'id'} !== null) {
147
            $object->setId($data->{'id'});
148
        } elseif (property_exists($data, 'id') && $data->{'id'} === null) {
149
            $object->setId(null);
150
        }
151
        if (property_exists($data, 'active') && $data->{'active'} !== null) {
152
            $object->setActive($data->{'active'});
153
        } elseif (property_exists($data, 'active') && $data->{'active'} === null) {
154
            $object->setActive(null);
155
        }
156
        if (property_exists($data, 'customerId') && $data->{'customerId'} !== null) {
157
            $object->setCustomerId($data->{'customerId'});
158
        } elseif (property_exists($data, 'customerId') && $data->{'customerId'} === null) {
159
            $object->setCustomerId(null);
160
        }
161
        if (property_exists($data, 'customerName') && $data->{'customerName'} !== null) {
162
            $object->setCustomerName($data->{'customerName'});
163
        } elseif (property_exists($data, 'customerName') && $data->{'customerName'} === null) {
164
            $object->setCustomerName(null);
165
        }
166
        if (property_exists($data, 'name') && $data->{'name'} !== null) {
167
            $object->setName($data->{'name'});
168
        } elseif (property_exists($data, 'name') && $data->{'name'} === null) {
169
            $object->setName(null);
170
        }
171
        if (property_exists($data, 'code') && $data->{'code'} !== null) {
172
            $object->setCode($data->{'code'});
173
        } elseif (property_exists($data, 'code') && $data->{'code'} === null) {
174
            $object->setCode(null);
175
        }
176
        if (property_exists($data, 'notes') && $data->{'notes'} !== null) {
177
            $object->setNotes($data->{'notes'});
178
        } elseif (property_exists($data, 'notes') && $data->{'notes'} === null) {
179
            $object->setNotes(null);
180
        }
181
        if (property_exists($data, 'invoiceMethod') && $data->{'invoiceMethod'} !== null) {
182
            $object->setInvoiceMethod($data->{'invoiceMethod'});
183
        } elseif (property_exists($data, 'invoiceMethod') && $data->{'invoiceMethod'} === null) {
184
            $object->setInvoiceMethod(null);
185
        }
186
        if (property_exists($data, 'hourlyRate') && $data->{'hourlyRate'} !== null) {
187
            $object->setHourlyRate($data->{'hourlyRate'});
188
        } elseif (property_exists($data, 'hourlyRate') && $data->{'hourlyRate'} === null) {
189
            $object->setHourlyRate(null);
190
        }
191
        if (property_exists($data, 'rate') && $data->{'rate'} !== null) {
192
            $object->setRate($data->{'rate'});
193
        } elseif (property_exists($data, 'rate') && $data->{'rate'} === null) {
194
            $object->setRate(null);
195
        }
196
        if (property_exists($data, 'budgetMethod') && $data->{'budgetMethod'} !== null) {
197
            $object->setBudgetMethod($data->{'budgetMethod'});
198
        } elseif (property_exists($data, 'budgetMethod') && $data->{'budgetMethod'} === null) {
199
            $object->setBudgetMethod(null);
200
        }
201
        if (property_exists($data, 'budgetRate') && $data->{'budgetRate'} !== null) {
202
            $object->setBudgetRate($data->{'budgetRate'});
203
        } elseif (property_exists($data, 'budgetRate') && $data->{'budgetRate'} === null) {
204
            $object->setBudgetRate(null);
205
        }
206
        if (property_exists($data, 'budgetHours') && $data->{'budgetHours'} !== null) {
207
            $object->setBudgetHours($data->{'budgetHours'});
208
        } elseif (property_exists($data, 'budgetHours') && $data->{'budgetHours'} === null) {
209
            $object->setBudgetHours(null);
210
        }
211
        if (property_exists($data, 'startDate') && $data->{'startDate'} !== null) {
212
            $object->setStartDate(\DateTime::createFromFormat('Y-m-d\\TH:i:s', $data->{'startDate'}));
213
        } elseif (property_exists($data, 'startDate') && $data->{'startDate'} === null) {
214
            $object->setStartDate(null);
215
        }
216
        if (property_exists($data, 'endDate') && $data->{'endDate'} !== null) {
217
            $object->setEndDate(\DateTime::createFromFormat('Y-m-d\\TH:i:s', $data->{'endDate'}));
218
        } elseif (property_exists($data, 'endDate') && $data->{'endDate'} === null) {
219
            $object->setEndDate(null);
220
        }
221
        if (property_exists($data, 'projectSubscription') && $data->{'projectSubscription'} !== null) {
222
            $object->setProjectSubscription($this->denormalizer->denormalize($data->{'projectSubscription'}, 'ConnectHolland\\TimechimpBundle\\Api\\Model\\ProjectSubscription', 'json', $context));
223
        } elseif (property_exists($data, 'projectSubscription') && $data->{'projectSubscription'} === null) {
224
            $object->setProjectSubscription(null);
225
        }
226
227
        return $object;
228
    }
229
230
    public function normalize($object, $format = null, array $context = [])
231
    {
232
        $data = new \stdClass();
233
        if (null !== $object->getRemainingBudgetHours()) {
234
            $data->{'remainingBudgetHours'} = $object->getRemainingBudgetHours();
235
        } else {
236
            $data->{'remainingBudgetHours'} = null;
237
        }
238
        if (null !== $object->getTagIds()) {
239
            $values = [];
240
            foreach ($object->getTagIds() as $value) {
241
                $values[] = $value;
242
            }
243
            $data->{'tagIds'} = $values;
244
        } else {
245
            $data->{'tagIds'} = null;
246
        }
247
        if (null !== $object->getTagNames()) {
248
            $values_1 = [];
249
            foreach ($object->getTagNames() as $value_1) {
250
                $values_1[] = $value_1;
251
            }
252
            $data->{'tagNames'} = $values_1;
253
        } else {
254
            $data->{'tagNames'} = null;
255
        }
256
        if (null !== $object->getUnspecified()) {
257
            $data->{'unspecified'} = $object->getUnspecified();
258
        } else {
259
            $data->{'unspecified'} = null;
260
        }
261
        if (null !== $object->getInvoiceDate()) {
262
            $data->{'invoiceDate'} = $object->getInvoiceDate()->format('Y-m-d\\TH:i:s');
263
        } else {
264
            $data->{'invoiceDate'} = null;
265
        }
266
        if (null !== $object->getInvoiceInInstallments()) {
267
            $data->{'invoiceInInstallments'} = $object->getInvoiceInInstallments();
268
        } else {
269
            $data->{'invoiceInInstallments'} = null;
270
        }
271
        if (null !== $object->getBudgetNotificationPercentage()) {
272
            $data->{'budgetNotificationPercentage'} = $object->getBudgetNotificationPercentage();
273
        } else {
274
            $data->{'budgetNotificationPercentage'} = null;
275
        }
276
        if (null !== $object->getBudgetNotificationHasBeenSent()) {
277
            $data->{'budgetNotificationHasBeenSent'} = $object->getBudgetNotificationHasBeenSent();
278
        } else {
279
            $data->{'budgetNotificationHasBeenSent'} = null;
280
        }
281
        if (null !== $object->getClientId()) {
282
            $data->{'clientId'} = $object->getClientId();
283
        } else {
284
            $data->{'clientId'} = null;
285
        }
286
        if (null !== $object->getInvoiceStatus()) {
287
            $data->{'invoiceStatus'} = $object->getInvoiceStatus();
288
        } else {
289
            $data->{'invoiceStatus'} = null;
290
        }
291
        if (null !== $object->getInvoiceId()) {
292
            $data->{'invoiceId'} = $object->getInvoiceId();
293
        } else {
294
            $data->{'invoiceId'} = null;
295
        }
296
        if (null !== $object->getColor()) {
297
            $data->{'color'} = $object->getColor();
298
        } else {
299
            $data->{'color'} = null;
300
        }
301
        if (null !== $object->getVisibleOnSchedule()) {
302
            $data->{'visibleOnSchedule'} = $object->getVisibleOnSchedule();
303
        } else {
304
            $data->{'visibleOnSchedule'} = null;
305
        }
306
        if (null !== $object->getExternalUrl()) {
307
            $data->{'externalUrl'} = $object->getExternalUrl();
308
        } else {
309
            $data->{'externalUrl'} = null;
310
        }
311
        if (null !== $object->getExternalName()) {
312
            $data->{'externalName'} = $object->getExternalName();
313
        } else {
314
            $data->{'externalName'} = null;
315
        }
316
        if (null !== $object->getInvoiceReference()) {
317
            $data->{'invoiceReference'} = $object->getInvoiceReference();
318
        } else {
319
            $data->{'invoiceReference'} = null;
320
        }
321
        if (null !== $object->getProjectTasks()) {
322
            $values_2 = [];
323
            foreach ($object->getProjectTasks() as $value_2) {
324
                $values_2[] = $this->normalizer->normalize($value_2, 'json', $context);
325
            }
326
            $data->{'projectTasks'} = $values_2;
327
        } else {
328
            $data->{'projectTasks'} = null;
329
        }
330
        if (null !== $object->getProjectUsers()) {
331
            $values_3 = [];
332
            foreach ($object->getProjectUsers() as $value_3) {
333
                $values_3[] = $this->normalizer->normalize($value_3, 'json', $context);
334
            }
335
            $data->{'projectUsers'} = $values_3;
336
        } else {
337
            $data->{'projectUsers'} = null;
338
        }
339
        if (null !== $object->getId()) {
340
            $data->{'id'} = $object->getId();
341
        } else {
342
            $data->{'id'} = null;
343
        }
344
        if (null !== $object->getActive()) {
345
            $data->{'active'} = $object->getActive();
346
        } else {
347
            $data->{'active'} = null;
348
        }
349
        if (null !== $object->getCustomerId()) {
350
            $data->{'customerId'} = $object->getCustomerId();
351
        } else {
352
            $data->{'customerId'} = null;
353
        }
354
        if (null !== $object->getCustomerName()) {
355
            $data->{'customerName'} = $object->getCustomerName();
356
        } else {
357
            $data->{'customerName'} = null;
358
        }
359
        if (null !== $object->getName()) {
360
            $data->{'name'} = $object->getName();
361
        } else {
362
            $data->{'name'} = null;
363
        }
364
        if (null !== $object->getCode()) {
365
            $data->{'code'} = $object->getCode();
366
        } else {
367
            $data->{'code'} = null;
368
        }
369
        if (null !== $object->getNotes()) {
370
            $data->{'notes'} = $object->getNotes();
371
        } else {
372
            $data->{'notes'} = null;
373
        }
374
        if (null !== $object->getInvoiceMethod()) {
375
            $data->{'invoiceMethod'} = $object->getInvoiceMethod();
376
        } else {
377
            $data->{'invoiceMethod'} = null;
378
        }
379
        if (null !== $object->getHourlyRate()) {
380
            $data->{'hourlyRate'} = $object->getHourlyRate();
381
        } else {
382
            $data->{'hourlyRate'} = null;
383
        }
384
        if (null !== $object->getRate()) {
385
            $data->{'rate'} = $object->getRate();
386
        } else {
387
            $data->{'rate'} = null;
388
        }
389
        if (null !== $object->getBudgetMethod()) {
390
            $data->{'budgetMethod'} = $object->getBudgetMethod();
391
        } else {
392
            $data->{'budgetMethod'} = null;
393
        }
394
        if (null !== $object->getBudgetRate()) {
395
            $data->{'budgetRate'} = $object->getBudgetRate();
396
        } else {
397
            $data->{'budgetRate'} = null;
398
        }
399
        if (null !== $object->getBudgetHours()) {
400
            $data->{'budgetHours'} = $object->getBudgetHours();
401
        } else {
402
            $data->{'budgetHours'} = null;
403
        }
404
        if (null !== $object->getStartDate()) {
405
            $data->{'startDate'} = $object->getStartDate()->format('Y-m-d\\TH:i:s');
406
        } else {
407
            $data->{'startDate'} = null;
408
        }
409
        if (null !== $object->getEndDate()) {
410
            $data->{'endDate'} = $object->getEndDate()->format('Y-m-d\\TH:i:s');
411
        } else {
412
            $data->{'endDate'} = null;
413
        }
414
        if (null !== $object->getProjectSubscription()) {
415
            $data->{'projectSubscription'} = $this->normalizer->normalize($object->getProjectSubscription(), 'json', $context);
416
        } else {
417
            $data->{'projectSubscription'} = null;
418
        }
419
420
        return $data;
421
    }
422
}
423