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

CategoryParser::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\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 CategoryParser
14
{
15 2 View Code Duplication
    public static function createFromResponse(ResponseInterface $response): array
16
    {
17 2
        $categories = [];
18 2
        $data = json_decode($response->getBody()->getContents(), true);
19
20 2
        foreach ($data as $category) {
21 1
            $categories[] = self::createFromArray($category);
22
        }
23
24 2
        return $categories;
25
    }
26
27 4
    public static function parseArray($categories)
28
    {
29 4
        $collection = new CategoryCollection();
30
31 4
        foreach ($categories as $category) {
32 2
            $collection->add(self::createFromArray($category));
33
        }
34
35 4
        return $collection;
36
    }
37
38 3
    private static function createFromArray(array $category): Category
39
    {
40 3
        return Category::create(
41 3
            Uuid::fromString($category['uuid']),
42 3
            $category['name'],
43 3
            $category['etag'],
44 3
            new DateTime($category['updated']),
45 3
            Uuid::fromString($category['updatedBy']),
46 3
            new DateTime($category['created'])
47
        );
48
    }
49
}
50