Cancelled
Pull Request — master (#11)
by Laurens
23:42
created

ProductBuilder::buildProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client\Purchase;
6
7
use LauLamanApps\IzettleApi\API\Image;
8
use LauLamanApps\IzettleApi\API\Purchase\Product;
9
use Money\Currency;
10
use Money\Money;
11
use Ramsey\Uuid\Uuid;
12
use Ramsey\Uuid\UuidInterface;
13
14
final class ProductBuilder implements ProductBuilderInterface
15
{
16
    /**
17
     * @return Product[]
18
     */
19 1
    public function buildFromJsonArray(array $products, Currency $currency): array
20
    {
21 1
        $data = [];
22
23 1
        foreach ($products as $product) {
24 1
            $data[] = $this->buildProduct($product, $currency);
25
        }
26
27 1
        return $data;
28
    }
29
30 1
    private function buildProduct(array $product, Currency $currency): Product
31
    {
32 1
        return new Product(
33 1
            $this->getUuidFromKey('productUuid', $product),
34 1
            $this->getUuidFromKey('variantUuid', $product),
35 1
            $this->getFromKey('name', $product),
36 1
            $this->getFromKey('variantName', $product),
37 1
            $this->getIntFromKey('quantity', $product),
38 1
            $this->getMoneyFromKey('unitPrice', $currency, $product),
39 1
            $this->getFromKey('vatPercentage', $product),
40 1
            $this->getMoneyFromKey('rowTaxableAmount', $currency, $product),
41 1
            $this->getImageFromKey('imageLookupKey', $product),
42 1
            $this->getFromKey('autoGenerated', $product),
43 1
            $this->getFromKey('libraryProduct', $product)
44
        );
45
    }
46
47 1
    private function getFromKey($key, array $data)
48
    {
49 1
        if (!array_key_exists($key, $data)) {
50 1
            return null;
51
        }
52
53 1
        return $data[$key];
54
    }
55
56 1
    private function getIntFromKey(string $key, array $data): int
57
    {
58 1
        return (int) $this->getFromKey($key, $data);
59
    }
60
61 1
    private function getUuidFromKey(string $key, array $data): ?UuidInterface
62
    {
63 1
        $data = $this->getFromKey($key, $data);
64 1
        if (!is_null($data)) {
65 1
            return Uuid::fromString($data);
66
        }
67
68 1
        return $data;
69
    }
70
71 1
    private function getMoneyFromKey(string $key, Currency $currency, array $data): Money
72
    {
73 1
        return new Money($this->getFromKey($key, $data), $currency);
74
    }
75
76 1
    private function getImageFromKey(string $key, array $data): ?Image
77
    {
78 1
        $data = $this->getFromKey($key, $data);
79 1
        if (!is_null($data)) {
80 1
            return new Image($data);
81
        }
82
83 1
        return $data;
84
    }
85
}
86