Passed
Pull Request — master (#4)
by
unknown
02:00
created

Edge::toStringWithMerge()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Redislabs\Module\RedisGraph;
6
7
final class Edge
8
{
9
    private string $queryPredicate;
10
    private Node $sourceNode;
11
    private Node $destinationNode;
12
    private ?string $relation;
13
    private ?iterable $properties = [];
14
15
    public function __construct(
16
        string $queryPredicate,
17
        Node $sourceNode,
18
        ?string $relation,
19
        Node $destinationNode,
20
        ?iterable $properties = []
21
    ) {
22
        $this->queryPredicate = $queryPredicate;
23
        $this->sourceNode = $sourceNode;
24
        $this->destinationNode = $destinationNode;
25
        $this->relation = $relation;
26
        $this->properties = $properties;
27
    }
28
29
    public static function create(Node $sourceNode, string $relation, Node $destinationNode): self
30
    {
31
        return new self('CREATE', $sourceNode, $relation, $destinationNode);
32
    }
33
    public static function merge(Node $sourceNode, string $relation, Node $destinationNode): self
34
    {
35
        return new self('MERGE', $sourceNode, $relation, $destinationNode);
36
    }
37
38
    public function withProperties(iterable $properties): self
39
    {
40
        $new = clone $this;
41
        $new->properties = $properties;
42
        return $new;
43
    }
44
45
    public function getType(): string
46
    {
47
        return $this->queryPredicate;
48
    }
49
50
    public function toString(): string
51
    {
52
        if ($this->queryPredicate === 'MERGE') {
53
            return $this->toStringWithMerge();
54
        }
55
        return $this->toStringWithCreate();
56
    }
57
58
    public function toStringWithMerge(): string
59
    {
60
        $edgeString = '(' . $this->sourceNode->getAlias() . ')';
61
        $edgeString .= '-[';
62
        if ($this->relation !== null) {
63
            $edgeString .= ':' . $this->relation . ' ';
64
        }
65
        if ($this->properties) {
66
            $edgeString .= '{' . $this->getProps($this->properties) . '}';
67
        }
68
        $edgeString .= ']->';
69
        $edgeString .= '(' . $this->destinationNode->getAlias() . ')';
70
        return $edgeString;
71
    }
72
73
    public function toStringWithCreate(): string
74
    {
75
        $edgeString = '(' . $this->sourceNode->getAlias() . ':' . $this->sourceNode->getLabel() . ')';
76
        $edgeString .= '-[';
77
        if ($this->relation !== null) {
78
            $edgeString .= ':' . $this->relation . ' ';
79
        }
80
        if ($this->properties) {
81
            $edgeString .= '{' . $this->getProps($this->properties) . '}';
82
        }
83
        $edgeString .= ']->';
84
        $edgeString .= '(' . $this->destinationNode->getAlias() . ':' . $this->destinationNode->getLabel() . ')';
85
        return $edgeString;
86
    }
87
88
    private function getProps(iterable $properties): string
89
    {
90
        $props = '';
91
        foreach ($properties as $propKey => $propValue) {
92
            if ($props !== '') {
93
                $props .= ', ';
94
            }
95
            $props .= $propKey . ': ' . $this->getPropValueWithDataType($propValue);
96
        }
97
        return $props;
98
    }
99
100
    private function getPropValueWithDataType($propValue)
101
    {
102
        $type = gettype($propValue);
103
        if ($type === 'string') {
104
            return quotedString((string) $propValue);
105
        }
106
        return (int) $propValue;
107
    }
108
109
    public function getQueryPredicate(): string
110
    {
111
        return $this->queryPredicate;
112
    }
113
}
114