GraphConstructor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommitQuery() 0 14 4
A __construct() 0 2 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 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