Importer::getNodes()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 18
cts 18
cp 1
rs 8.439
cc 5
eloc 14
nc 2
nop 2
crap 5
1
<?php
2
3
namespace NodeRED;
4
5
class Importer
6
{
7
    private $instance;
8
    private $token;
9
10 1
    public function __construct(Instance $instance, OAuthToken $token)
11
    {
12 1
        $this->instance = $instance;
13 1
        $this->token = $token;
14 1
    }
15
16 1
    public function importFlow($label, $flowJson)
17
    {
18 1
        $id = uniqid();
19
20
        $body = [
21 1
            'id' => $id,
22 1
            'label' => $label,
23 1
            'nodes' => $this->getNodes($id, $flowJson)
24 1
        ];
25
26 1
        $data = $this->instance->jsonPost('flow', $body, $this->token);
27
28 1
        return $data['id'];
29
    }
30
31 1
    private function getNodes($z, $flowJson)
32
    {
33 1
        $rawNodes = json_decode($flowJson, true);
34
35 1
        $idMap = [];
36
37 1
        foreach ($rawNodes as $node) {
38 1
            $idMap[$node['id']] = uniqid();
39 1
        }
40
41 1
        return array_map(function ($node) use ($z, $idMap){
42 1
            $node['id'] = $idMap[$node['id']];
43 1
            $node['z'] = $z;
44
45 1
            if (isset($node['wires'])) {
46 1
                foreach ($node['wires'] as &$wire) {
47 1
                    foreach ($wire as &$id) {
48 1
                        $id = $idMap[$id];
49 1
                    }
50 1
                }
51 1
            }
52
53 1
            return $node;
54 1
        }, json_decode($flowJson, true));
55
    }
56
}
57