Passed
Push — master ( 9ff768...b736eb )
by Laurens
02:33
created

ProductParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 25.58 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 11
loc 43
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromResponse() 11 11 2
A parseArray() 0 10 2
A parse() 0 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client\Product;
6
7
use DateTime;
8
use LauLamanApps\IzettleApi\API\Product\Product;
9
use LauLamanApps\IzettleApi\API\Product\ProductCollection;
10
use LauLamanApps\IzettleApi\Client\ImageParser;
11
use Psr\Http\Message\ResponseInterface;
12
use Ramsey\Uuid\Uuid;
13
14
final class ProductParser
15
{
16 2 View Code Duplication
    public static function createFromResponse(ResponseInterface $response): array
17
    {
18 2
        $products = [];
19 2
        $data = json_decode($response->getBody()->getContents(), true);
20
21 2
        foreach ($data as $purchase) {
22 1
            $products[] = self::parse($purchase);
23
        }
24
25 2
        return $products;
26
    }
27
28 2
    public static function parseArray(array $products): ProductCollection
29
    {
30 2
        $productCollection = new ProductCollection();
31
32 2
        foreach ($products as $product) {
33 1
            $productCollection->add(self::parse($product));
34
        }
35
36 2
        return $productCollection;
37
    }
38
39 2
    private static function parse(array $product): Product
40
    {
41 2
        return Product::create(
42 2
            Uuid::fromString($product['uuid']),
43 2
            CategoryParser::parseArray($product['categories']),
44 2
            $product['name'],
45 2
            $product['description'],
46 2
            ImageParser::parseArray($product['imageLookupKeys']),
47 2
            VariantParser::parseArray($product['variants']),
48 2
            $product['externalReference'],
49 2
            $product['etag'],
50 2
            new DateTime($product['updated']),
51 2
            Uuid::fromString($product['updatedBy']),
52 2
            new DateTime($product['created']),
53 2
            (float) $product['vatPercentage']
54
        );
55
    }
56
}
57