Completed
Pull Request — master (#11)
by Laurens
02:02
created

ProductBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 58
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A buildFromJson() 0 9 2
A buildFromArray() 0 10 2
A build() 0 17 1
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\Universal\ImageBuilderInterface;
11
use Ramsey\Uuid\Uuid;
12
13
final class ProductBuilder implements ProductBuilderInterface
14
{
15
    private $imageBuilder;
16
    private $categoryBuilder;
17
    private $variantBuilder;
18
19 4
    public function __construct(
20
        CategoryBuilderInterface $categoryBuilder,
21
        ImageBuilderInterface $imageBuilder,
22
        VariantBuilderInterface $variantBuilder
23
    ) {
24 4
        $this->categoryBuilder = $categoryBuilder;
25 4
        $this->imageBuilder = $imageBuilder;
26 4
        $this->variantBuilder = $variantBuilder;
27 4
    }
28
29
    /**
30
     * @return Product[]
31
     */
32 2
    public function buildFromJson(string $json): array
33
    {
34 2
        $products = [];
35 2
        foreach (json_decode($json, true) as $purchase) {
36 2
            $products[] = $this->build($purchase);
37
        }
38
39 2
        return $products;
40
    }
41
42 2
    public function buildFromArray(array $data): ProductCollection
43
    {
44 2
        $productCollection = new ProductCollection();
45
46 2
        foreach ($data as $product) {
47 2
            $productCollection->add($this->build($product));
48
        }
49
50 2
        return $productCollection;
51
    }
52
53 4
    private function build(array $data): Product
54
    {
55 4
        return Product::create(
56 4
            Uuid::fromString($data['uuid']),
57 4
            $this->categoryBuilder->buildFromArray($data['categories']),
58 4
            $data['name'],
59 4
            $data['description'],
60 4
            $this->imageBuilder->buildFromArray($data['imageLookupKeys']),
61 4
            $this->variantBuilder->buildFromArray($data['variants']),
62 4
            $data['externalReference'],
63 4
            $data['etag'],
64 4
            new DateTime($data['updated']),
65 4
            Uuid::fromString($data['updatedBy']),
66 4
            new DateTime($data['created']),
67 4
            (float) $data['vatPercentage']
68
        );
69
    }
70
}
71