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

PurchaseParser::parseArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client\Purchase;
6
7
use DateTime;
8
use LauLamanApps\IzettleApi\API\Purchase\Purchase;
9
use LauLamanApps\IzettleApi\API\Purchase\User;
10
use Money\Currency;
11
use Money\Money;
12
use Ramsey\Uuid\Uuid;
13
14
final class PurchaseParser
15
{
16 3
    public static function parseArray(array $purchases): array
17
    {
18 3
        $data = [];
19
20 3
        foreach ($purchases as $purchase) {
21 1
            $data[] = self::parse($purchase);
22
        }
23
24 3
        return $data;
25
    }
26
27 2
    public static function parse(array $purchase): Purchase
28
    {
29 2
        $currency = new Currency($purchase['currency']);
30
31 2
        $coordinates = null;
32 2
        if (array_key_exists('gpsCoordinates', $purchase)) {
33 2
            $coordinates = CoordinatesParser::parse($purchase['gpsCoordinates']);
34
        }
35
36 2
        $published = null;
37 2
        if (array_key_exists('published', $purchase)) {
38 2
            $published =  $purchase['published'];
39
        }
40
41 2
        return new Purchase(
42 2
            $purchase['purchaseUUID'],
43 2
            Uuid::fromString($purchase['purchaseUUID1']),
44 2
            new DateTime($purchase['timestamp']),
45 2
            $coordinates,
46 2
            $purchase['country'],
47 2
            new User($purchase['userId'], $purchase['userDisplayName']),
48 2
            $purchase['organizationId'],
49 2
            $purchase['purchaseNumber'],
50 2
            new Money($purchase['amount'], $currency),
51 2
            new Money($purchase['vatAmount'], $currency),
52 2
            ProductParser::parseArray($purchase['products'], $currency),
53 2
            PaymentParser::parseArray($purchase['payments'], $currency),
54 2
            VatParser::parseArray($purchase['groupedVatAmounts'], $currency),
55 2
            $purchase['receiptCopyAllowed'],
56 2
            $published,
57 2
            $purchase['refund'],
58 2
            $purchase['refunded']
59
        );
60
    }
61
}
62