Completed
Push — master ( 5e55b4...fe37cc )
by Andrew
02:24 queued 22s
created

Importer::importFlow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 1
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
        $nodes = json_decode($flowJson, true);
0 ignored issues
show
Unused Code introduced by
$nodes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
19
20 1
        $id = uniqid();
21
22
        $body = [
23 1
            'id' => $id,
24 1
            'label' => $label,
25 1
            'nodes' => $this->getNodes($id, $flowJson)
26 1
        ];
27
28 1
        $data = $this->instance->jsonPost('flow', $body, $this->token);
29
30 1
        return $data['id'];
31
    }
32
33 1
    private function getNodes($z, $flowJson)
34
    {
35 1
        $rawNodes = json_decode($flowJson, true);
36
37 1
        $idMap = [];
38
39 1
        foreach ($rawNodes as $node) {
40 1
            $idMap[$node['id']] = uniqid();
41 1
        }
42
43 1
        return array_map(function ($node) use ($z, $idMap){
44 1
            $node['id'] = $idMap[$node['id']];
45 1
            $node['z'] = $z;
46
47 1
            foreach ($node['wires'] as &$wire) {
48 1
                foreach ($wire as &$id) {
49 1
                    $id = $idMap[$id];
50 1
                }
51 1
            }
52
53 1
            return $node;
54 1
        }, json_decode($flowJson, true));
55
    }
56
}
57