Passed
Branch releases/1.0-dev (6a4c8c)
by Laurens
03:03
created

ProductBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 83
ccs 41
cts 42
cp 0.9762
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A buildFromArray() 0 10 2
A build() 0 16 1
A getFromKey() 0 8 2
A getIntFromKey() 0 4 1
A getUuidFromKey() 0 9 2
A getMoneyFromKey() 0 4 1
A getVatFromKey() 0 11 3
A getImageFromKey() 0 9 2
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 LauLamanApps\IzettleApi\API\Universal\Vat;
10
use Money\Currency;
11
use Money\Money;
12
use Ramsey\Uuid\Uuid;
13
use Ramsey\Uuid\UuidInterface;
14
15
final class ProductBuilder implements ProductBuilderInterface
16
{
17
    /**
18
     * @return Product[]
19
     */
20 3
    public function buildFromArray(array $products, Currency $currency): array
21
    {
22 3
        $data = [];
23
24 3
        foreach ($products as $product) {
25 3
            $data[] = $this->build($product, $currency);
26
        }
27
28 3
        return $data;
29
    }
30
31 3
    private function build(array $product, Currency $currency): Product
32
    {
33 3
        return new Product(
34 3
            $this->getUuidFromKey('productUuid', $product),
35 3
            $this->getUuidFromKey('variantUuid', $product),
36 3
            $this->getFromKey('name', $product),
37 3
            $this->getFromKey('variantName', $product),
38 3
            $this->getIntFromKey('quantity', $product),
39 3
            $this->getMoneyFromKey('unitPrice', $currency, $product),
40 3
            $this->getVatFromKey('vatPercentage', $product),
41 3
            $this->getMoneyFromKey('rowTaxableAmount', $currency, $product),
42 3
            $this->getImageFromKey('imageLookupKey', $product),
43 3
            $this->getFromKey('autoGenerated', $product),
44 3
            $this->getFromKey('libraryProduct', $product)
45
        );
46
    }
47
48 3
    private function getFromKey($key, array $data)
49
    {
50 3
        if (!array_key_exists($key, $data)) {
51 3
            return null;
52
        }
53
54 3
        return $data[$key];
55
    }
56
57 3
    private function getIntFromKey(string $key, array $data): int
58
    {
59 3
        return (int) $this->getFromKey($key, $data);
60
    }
61
62 3
    private function getUuidFromKey(string $key, array $data): ?UuidInterface
63
    {
64 3
        $data = $this->getFromKey($key, $data);
65 3
        if (!is_null($data)) {
66 1
            return Uuid::fromString($data);
67
        }
68
69 3
        return $data;
70
    }
71
72 3
    private function getMoneyFromKey(string $key, Currency $currency, array $data): Money
73
    {
74 3
        return new Money($this->getFromKey($key, $data), $currency);
75
    }
76 3
    private function getVatFromKey($key, array $data): ?Vat
77
    {
78 3
        if (!array_key_exists($key, $data)) {
79
            return null;
80
        }
81 3
        if ($data[$key] == 0) {
82 1
            return null;
83
        }
84
85 3
        return new Vat((string)$data[$key]);
86
    }
87
88 3
    private function getImageFromKey(string $key, array $data): ?Image
89
    {
90 3
        $data = $this->getFromKey($key, $data);
91 3
        if (!is_null($data)) {
92 1
            return new Image($data);
93
        }
94
95 3
        return $data;
96
    }
97
}
98