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

DiscountParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 26.19 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 11
loc 42
ccs 24
cts 24
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 16 3

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\Discount;
9
use LauLamanApps\IzettleApi\API\Product\DiscountCollection;
10
use LauLamanApps\IzettleApi\Client\ImageParser;
11
use Money\Currency;
12
use Money\Money;
13
use Psr\Http\Message\ResponseInterface;
14
use Ramsey\Uuid\Uuid;
15
16
final class DiscountParser
17
{
18 3 View Code Duplication
    public static function createFromResponse(ResponseInterface $response): array
19
    {
20 3
        $products = [];
21 3
        $data = json_decode($response->getBody()->getContents(), true);
22
23 3
        foreach ($data as $product) {
24 2
            $products[] = self::parse($product);
25
        }
26
27 3
        return $products;
28
    }
29
30 2
    public static function parseArray($discounts): DiscountCollection
31
    {
32 2
        $discountCollection = new DiscountCollection();
33
34 2
        foreach ($discounts as $discount) {
35 1
            $discountCollection->add(self::parse($discount));
36
        }
37
38 2
        return $discountCollection;
39
    }
40
    
41 3
    private static function parse(array $product): Discount
42
    {
43 3
        return Discount::create(
44 3
            Uuid::fromString($product['uuid']),
45 3
            $product['name'],
46 3
            $product['description'],
47 3
            ImageParser::parseArray($product['imageLookupKeys']),
48 3
            $product['amount'] ? new Money($product['amount']['amount'], new Currency($product['amount']['currencyId'])) : null,
49 3
            $product['percentage'] ? (float) $product['percentage'] : null,
50 3
            $product['externalReference'],
51 3
            $product['etag'],
52 3
            new DateTime($product['updated']),
53 3
            Uuid::fromString($product['updatedBy']),
54 3
            new DateTime($product['created'])
55
        );
56
    }
57
}
58