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

ProductMapper::map()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 27
cp 0
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 28
nc 10
nop 2
crap 110

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;
4
5
use Waredesk\Collections\Products\Variants;
6
use Waredesk\Mappers\Product\VariantsMapper;
7
use Waredesk\Models\Product;
8
use DateTime;
9
10
class ProductMapper
11
{
12
    public function map(Product $product, array $data): Product
13
    {
14
        foreach ($data as $key => $value) {
15
            switch ($key) {
16
                case 'id':
17
                    $product->setId((int)$value);
18
                    break;
19
                case 'images':
20
                    $product->setImages($value);
21
                    break;
22
                case 'variants':
23
                    $product->setVariants((new VariantsMapper())->map(new Variants(), $value));
24
                    break;
25
                case 'name':
26
                    $product->setName($value);
27
                    break;
28
                case 'description':
29
                    $product->setDescription($value);
30
                    break;
31
                case 'notes':
32
                    $product->setNotes($value);
33
                    break;
34
                case 'creation_datetime':
35
                    $product->setCreationDatetime(new DateTime($value));
36
                    break;
37
                case 'modification_datetime':
38
                    $product->setModificationDatetime(new DateTime($value));
39
                    break;
40
            }
41
        }
42
        return $product;
43
    }
44
}
45