GraphConstructor::getCommitQuery()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 14
rs 9.9666
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 array $nodes = [];
12
    private array $edges = [];
13
14
    public function __construct(private string $name)
15
    {
16
    }
17
18
    public function addNode(Node $node): void
19
    {
20
        $this->nodes[] = $node;
21
    }
22
23
    public function addEdge(Edge $edge): void
24
    {
25
        $this->edges[] = $edge;
26
    }
27
28
    public function getCommitQuery(): QueryInterface
29
    {
30
        $query = 'CREATE ';
31
        foreach ($this->nodes as $index => $node) {
32
            $query .= $node->toString() . ', ';
33
        }
34
        $edgeCount = count($this->edges);
35
        foreach ($this->edges as $index => $edge) {
36
            $query .= $edge->toString();
37
            if ($index < $edgeCount - 1) {
38
                $query .= ', ';
39
            }
40
        }
41
        return new Query($this->name, $query);
42
    }
43
44
    public function getCommitQueryWithMerge(): QueryInterface
45
    {
46
        $query = '';
47
        foreach ($this->nodes as $index => $node) {
48
            $query .= 'MERGE ' . $node->toString() . ' ';
49
        }
50
        foreach ($this->edges as $index => $edge) {
51
            $query .= 'MERGE ' . $edge->toString() . ' ';
52
        }
53
        return new Query($this->name, trim($query));
54
    }
55
}
56