Completed
Push — master ( b2620d...cacd0e )
by Gabriel
02:27
created

ProductMapper::map()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 19
cts 19
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 6
nop 2
crap 6
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 3
    public function map(Product $product, array $data): Product
13
    {
14 3
        $finalData = [];
15 3
        foreach ($data as $key => $value) {
16
            switch ($key) {
17 3
                case 'id':
18 3
                    $finalData['id'] = (int)$value;
19 3
                    break;
20 3
                case 'variants':
21 3
                    $finalData['variants'] = (new VariantsMapper())->map(new Variants(), $value);
22 3
                    break;
23 3
                case 'creation_datetime':
24 3
                    $finalData['creation_datetime'] = new DateTime($value);
25 3
                    break;
26 3
                case 'modification_datetime':
27 3
                    $finalData['modification_datetime'] = new DateTime($value);
28 3
                    break;
29
                default:
30 3
                    $finalData[$key] = $value;
31 3
                    break;
32
            }
33
        }
34 3
        $product->reset($finalData);
35 3
        return $product;
36
    }
37
}
38