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

CategoryBuilder::buildFromJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
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\Product;
6
7
use DateTime;
8
use LauLamanApps\IzettleApi\API\Product\Category;
9
use LauLamanApps\IzettleApi\API\Product\CategoryCollection;
10
use Psr\Http\Message\ResponseInterface;
11
use Ramsey\Uuid\Uuid;
12
13
final class CategoryBuilder implements CategoryBuilderInterface
14
{
15
    /**
16
     * @return Category[]
17
     */
18 2
    public function buildFromJson(string $json): array
19
    {
20 2
        $categories = [];
21 2
        foreach (json_decode($json, true) as $category) {
22 2
            $categories[] = $this->build($category);
23
        }
24
25 2
        return $categories;
26
    }
27
28 4
    public function buildFromArray($categories): CategoryCollection
29
    {
30 4
        $collection = new CategoryCollection();
31
32 4
        foreach ($categories as $category) {
33 2
            $collection->add($this->build($category));
34
        }
35
36 4
        return $collection;
37
    }
38
39 4
    private function build(array $category): Category
40
    {
41 4
        return Category::create(
42 4
            Uuid::fromString($category['uuid']),
43 4
            $category['name'],
44 4
            $category['etag'],
45 4
            new DateTime($category['updated']),
46 4
            Uuid::fromString($category['updatedBy']),
47 4
            new DateTime($category['created'])
48
        );
49
    }
50
}
51