Edge::toStringWithCreate()   A
last analyzed

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
    public function __construct(
10
        private string $type,
11
        private Node $sourceNode,
12
        private ?string $relation,
13
        private Node $destinationNode,
14
        private ?iterable $properties = []
15
    ) {
16
    }
17
18
    public static function create(Node $sourceNode, string $relation, Node $destinationNode): self
19
    {
20
        return new self('CREATE', $sourceNode, $relation, $destinationNode);
21
    }
22
    public static function merge(Node $sourceNode, string $relation, Node $destinationNode): self
23
    {
24
        return new self('MERGE', $sourceNode, $relation, $destinationNode);
25
    }
26
27
    public function withProperties(iterable $properties): self
28
    {
29
        $new = clone $this;
30
        $new->properties = $properties;
31
        return $new;
32
    }
33
34
    public function getType(): string
35
    {
36
        return $this->type;
37
    }
38
39
    public function toString(): string
40
    {
41
        if ($this->type === 'MERGE') {
42
            return $this->toStringWithMerge();
43
        }
44
        return $this->toStringWithCreate();
45
    }
46
47
    public function toStringWithMerge(): string
48
    {
49
        $edgeString = '(' . $this->sourceNode->getAlias() . ')';
50
        $edgeString .= '-[';
51
        if ($this->relation !== null) {
52
            $edgeString .= ':' . $this->relation . ' ';
53
        }
54
        if ($this->properties) {
55
            $edgeString .= '{' . $this->getProps($this->properties) . '}';
56
        }
57
        $edgeString .= ']->';
58
        $edgeString .= '(' . $this->destinationNode->getAlias() . ')';
59
        return $edgeString;
60
    }
61
62
    public function toStringWithCreate(): string
63
    {
64
        $edgeString = '(' . $this->sourceNode->getAlias() . ':' . $this->sourceNode->getLabel() . ')';
65
        $edgeString .= '-[';
66
        if ($this->relation !== null) {
67
            $edgeString .= ':' . $this->relation . ' ';
68
        }
69
        if ($this->properties) {
70
            $edgeString .= '{' . $this->getProps($this->properties) . '}';
71
        }
72
        $edgeString .= ']->';
73
        $edgeString .= '(' . $this->destinationNode->getAlias() . ':' . $this->destinationNode->getLabel() . ')';
74
        return $edgeString;
75
    }
76
77
    private function getProps(iterable $properties): string
78
    {
79
        $props = '';
80
        foreach ($properties as $propKey => $propValue) {
81
            if ($props !== '') {
82
                $props .= ', ';
83
            }
84
            $props .= $propKey . ': ' . $this->getPropValueWithDataType($propValue);
85
        }
86
        return $props;
87
    }
88
89
    private function getPropValueWithDataType($propValue)
90
    {
91
        $type = gettype($propValue);
92
        if ($type === 'string') {
93
            return quotedString((string) $propValue);
94
        }
95
        return (int) $propValue;
96
    }
97
}
98