VariantMapper::map()   C
last analyzed

Complexity

Conditions 16
Paths 16

Size

Total Lines 43
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 16

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 37
cts 37
cp 1
rs 5.0151
c 0
b 0
f 0
cc 16
eloc 39
nc 16
nop 2
crap 16

How to fix   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\Items\Attributes as ItemsAttributes;
7
use Waredesk\Collections\Products\Variants\Attributes;
8
use Waredesk\Collections\Products\Variants\Codes;
9
use Waredesk\Collections\Products\Variants\Prices;
10
use Waredesk\Mapper;
11
use Waredesk\Mappers\Product\Variant\Item\AttributesMapper as ItemAttributesMapper;
12
use Waredesk\Mappers\Product\Variant\AttributesMapper;
13
use Waredesk\Mappers\Product\Variant\CodesMapper;
14
use Waredesk\Mappers\Product\Variant\PricesMapper;
15
use Waredesk\Models\Product\Variant;
16
17
class VariantMapper extends Mapper
18
{
19 6
    public function map(Variant $variant, $data): Variant
20
    {
21 6
        $finalData = [];
22 6
        foreach ($data as $key => $value) {
23
            switch ($key) {
24 6
                case 'attributes':
25 6
                    $finalData['attributes'] = (new AttributesMapper())->map(new Attributes(), $value);
26 6
                    break;
27 6
                case 'codes':
28 6
                    $finalData['codes'] = (new CodesMapper())->map(new Codes(), $value);
29 6
                    break;
30 6
                case 'prices':
31 6
                    $finalData['prices'] = (new PricesMapper())->map(new Prices(), $value);
32 6
                    break;
33 6
                case 'items_attributes':
34 6
                    $finalData['items_attributes'] = (new ItemAttributesMapper())->map(new ItemsAttributes(), $value);
35 6
                    break;
36 6
                case 'weight':
37 6
                    $finalData['weight'] = null === $value ? null : (float)$value;
38 6
                    break;
39 6
                case 'height':
40 6
                    $finalData['height'] = null === $value ? null : (float)$value;
41 6
                    break;
42 6
                case 'depth':
43 6
                    $finalData['depth'] = null === $value ? null : (float)$value;
44 6
                    break;
45 6
                case 'width':
46 6
                    $finalData['width'] = null === $value ? null : (float)$value;
47 6
                    break;
48 6
                case 'creation':
49 6
                    $finalData['creation'] = new DateTime($value);
50 6
                    break;
51 6
                case 'modification':
52 6
                    $finalData['modification'] = new DateTime($value);
53 6
                    break;
54
                default:
55 6
                    $finalData[$key] = $value;
56 6
                    break;
57
            }
58
        }
59 6
        $variant->reset($finalData);
60 6
        return $variant;
61
    }
62
}
63