Completed
Pull Request — master (#29)
by
unknown
06:10
created

DataObjectBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 49
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 22 6
A isAList() 0 4 1
A buildItem() 0 18 4
1
<?php
2
3
namespace Softonic\GraphQL;
4
5
use Softonic\GraphQL\Query\Collection;
6
use Softonic\GraphQL\Query\Item;
7
8
class DataObjectBuilder
9
{
10 14
    public function build(array $data): array
11
    {
12 14
        $dataObject = [];
13 14
        foreach ($data as $queryName => $objectsData) {
14 14
            if (is_array($objectsData) && !empty($objectsData)) {
15 10
                if ($this->isAList($objectsData)) {
16 2
                    $items = [];
17 2
                    foreach ($objectsData as $objectData) {
18 2
                        $items[] = $this->buildItem($objectData);
19
                    }
20
21 2
                    $dataObject[$queryName] = new Collection($items);
22
                } else {
23 10
                    $dataObject[$queryName] = $this->buildItem($objectsData);
24
                }
25
            } else {
26 14
                $dataObject[$queryName] = $objectsData;
27
            }
28
        }
29
30 14
        return $dataObject;
31
    }
32
33 10
    private function isAList(array $data): bool
34
    {
35 10
        return array_values($data) === $data;
36
    }
37
38 10
    private function buildItem(array $data): Item
39
    {
40 10
        $itemData = [];
41 10
        foreach ($data as $key => $value) {
42 10
            if (is_array($value)) {
43 6
                $items = [];
44 6
                foreach ($value as $valueItemData) {
45 4
                    $items[] = $this->buildItem($valueItemData);
46
                }
47
48 6
                $itemData[$key] = new Collection($items);
49
            } else {
50 10
                $itemData[$key] = $value;
51
            }
52
        }
53
54 10
        return new Item($itemData);
55
    }
56
}
57