Completed
Push — master ( c3c281...ab94e9 )
by Gabriel
03:37
created

VariantMapper::map()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 53
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 0
cts 48
cp 0
rs 6.2566
c 0
b 0
f 0
cc 17
eloc 49
nc 17
nop 2
crap 306

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Waredesk\Mappers\Product;
4
5
use DateTime;
6
use Waredesk\Collections\Products\Variants\Codes;
7
use Waredesk\Collections\Products\Variants\Options;
8
use Waredesk\Collections\Products\Variants\Prices;
9
use Waredesk\Mappers\Product\Variant\CodesMapper;
10
use Waredesk\Mappers\Product\Variant\OptionsMapper;
11
use Waredesk\Mappers\Product\Variant\PricesMapper;
12
use Waredesk\Models\Product\Variant;
13
14
class VariantMapper
15
{
16
    public function map(Variant $variant, $data): Variant
17
    {
18
        foreach ($data as $key => $value) {
19
            switch ($key) {
20
                case 'id':
21
                    $variant->setId((int)$value);
22
                    break;
23
                case 'images':
24
                    $variant->setImages($value);
25
                    break;
26
                case 'options':
27
                    $variant->setOptions((new OptionsMapper())->map(new Options(), $value));
28
                    break;
29
                case 'codes':
30
                    $variant->setCodes((new CodesMapper())->map(new Codes(), $value));
31
                    break;
32
                case 'prices':
33
                    $variant->setPrices((new PricesMapper())->map(new Prices(), $value));
34
                    break;
35
                case 'description':
36
                    $variant->setDescription($value);
37
                    break;
38
                case 'notes':
39
                    $variant->setNotes($value);
40
                    break;
41
                case 'weight_unit':
42
                    $variant->setWeightUnit($value);
43
                    break;
44
                case 'length_unit':
45
                    $variant->setLengthUnit($value);
46
                    break;
47
                case 'weight':
48
                    $variant->setWeight((float)$value);
49
                    break;
50
                case 'height':
51
                    $variant->setHeight((float)$value);
52
                    break;
53
                case 'depth':
54
                    $variant->setDepth((float)$value);
55
                    break;
56
                case 'width':
57
                    $variant->setWidth((float)$value);
58
                    break;
59
                case 'creation_datetime':
60
                    $variant->setCreationDatetime(new DateTime($value));
61
                    break;
62
                case 'modification_datetime':
63
                    $variant->setModificationDatetime(new DateTime($value));
64
                    break;
65
            }
66
        }
67
        return $variant;
68
    }
69
}
70