Test Failed
Push — master ( 8960d3...0d17c6 )
by Mehmet
07:55
created

GraphConstructor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 47
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommitQuery() 0 14 4
A __construct() 0 3 1
A addEdge() 0 3 1
A addNode() 0 3 1
A getCommitQueryWithMerge() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Redislabs\Module\RedisGraph;
6
7
use Redislabs\Module\RedisGraph\Interfaces\QueryInterface;
8
9
class GraphConstructor
10
{
11
    private string $name;
12
    private array $nodes = [];
13
    private array $edges = [];
14
15
    public function __construct(string $name)
16
    {
17
        $this->name = $name;
18
    }
19
20
    public function addNode(Node $node): void
21
    {
22
        $this->nodes[] = $node;
23
    }
24
25
    public function addEdge(Edge $edge): void
26
    {
27
        $this->edges[] = $edge;
28
    }
29
30
    public function getCommitQuery(): QueryInterface
31
    {
32
        $query = 'CREATE ';
33
        foreach ($this->nodes as $index => $node) {
34
            $query .= $node->toString() . ', ';
35
        }
36
        $edgeCount = count($this->edges);
37
        foreach ($this->edges as $index => $edge) {
38
            $query .= $edge->toString();
39
            if ($index < $edgeCount - 1) {
40
                $query .= ', ';
41
            }
42
        }
43
        return new Query($this->name, $query);
44
    }
45
46
    public function getCommitQueryWithMerge(): QueryInterface
47
    {
48
        $query = '';
49
        foreach ($this->nodes as $index => $node) {
50
            $query .= 'MERGE ' . $node->toString() . ' ';
51
        }
52
        foreach ($this->edges as $index => $edge) {
53
            $query .= 'MERGE ' . $edge->toString() . ' ';
54
        }
55
        return new Query($this->name, trim($query));
56
    }
57
}
58