Completed
Branch master (01c74f)
by Bingo
08:40 queued 04:19
created

GraphBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
eloc 31
c 1
b 0
f 1
dl 0
loc 159
ccs 39
cts 39
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A removeEdge() 0 7 1
A addEdgeChain() 0 9 2
A addVertices() 0 6 2
A addEdge() 0 14 2
A removeVertices() 0 6 2
A addGraph() 0 4 1
A addVertex() 0 4 1
A removeVertex() 0 4 1
A build() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace graphp\graph;
4
5
use graphp\edge\EdgeInterface;
6
use graphp\vertex\VertexInterface;
7
use graphp\vertex\VertexSet;
8
9
/**
10
 * Class GraphBuilder
11
 *
12
 * @package graphp\graph
13
 */
14
class GraphBuilder
15
{
16
    /**
17
     * The graph
18
     *
19
     * @var GraphInterface
20
     */
21
    private $graph;
22
    
23
    /**
24
     * Construct the graph
25
     *
26
     * @param GraphInterface $graph - the graph
27
     */
28 6
    public function __construct(GraphInterface $graph)
29
    {
30 6
        $this->graph = $graph;
31 6
    }
32
33
    /**
34
     * Add a vertex to the graph being built
35
     *
36
     * @param VertexInterface $vertex - the vertex
37
     *
38
     * @return self
39
     */
40 3
    public function addVertex(VertexInterface $vertex): self
41
    {
42 3
        $this->graph->addVertex($vertex);
43 3
        return $this;
44
    }
45
46
    /**
47
     * Add vertices to the graph being built
48
     *
49
     * @param VertexSet $vertices - the vertices
50
     *
51
     * @return self
52
     */
53 1
    public function addVertices(VertexSet $vertices): self
54
    {
55 1
        foreach ($vertices as $vertex) {
56 1
            $this->addVertex($vertex);
57
        }
58 1
        return $this;
59
    }
60
61
    /**
62
     * Add an edge to the graph being built
63
     *
64
     * @param VertexInterface $vertex - the source vertex
65
     * @param VertexInterface $targetVertex - the target vertex
66
     * @param EdgeInterface $edge - the edge to be added
67
     *
68
     * @return self
69
     */
70 2
    public function addEdge(
71
        VertexInterface $sourceVertex,
72
        VertexInterface $targetVertex,
73
        ?EdgeInterface $edge = null,
74
        ?float $weight = null
75
    ): self {
76 2
        if (is_null($edge)) {
77 2
            GraphUtils::addEdgeWithVertices($this->graph, $sourceVertex, $targetVertex, $weight);
78
        } else {
79 1
            $this->addVertex($sourceVertex);
80 1
            $this->addVertex($targetVertex);
81 1
            $this->graph->addEdge($sourceVertex, $targetVertex, $edge);
82
        }
83 2
        return $this;
84
    }
85
86
    /**
87
     * Add an chain of edges to the graph being built
88
     *
89
     * @param VertexInterface $first - the first vertex
90
     * @param VertexInterface $second - the second vertex
91
     * @param mixed $vertices - the remaining vertices
92
     *
93
     * @return self
94
     */
95 1
    public function addEdgeChain(VertexInterface $first, VertexInterface $second, ...$vertices): self
96
    {
97 1
        $this->addEdge($first, $second);
98 1
        $last = $second;
99 1
        foreach ($vertices as $vertex) {
100 1
            $this->addEdge($last, $vertex);
101 1
            $last = $vertex;
102
        }
103 1
        return $this;
104
    }
105
106
    /**
107
     * Add all the vertices and all the edges of the specified graph to the graph being built
108
     *
109
     * @param GraphInterface $sourceGraph - the graph
110
     *
111
     * @return self
112
     */
113 1
    public function addGraph(GraphInterface $sourceGraph): self
114
    {
115 1
        GraphUtils::addGraph($this->graph, $sourceGraph);
116 1
        return $this;
117
    }
118
119
    /**
120
     * Remove the specified vertex from the graph being built
121
     *
122
     * @param VertexInterface $vertex - the vertex to remove
123
     *
124
     * @return self
125
     */
126 1
    public function removeVertex(VertexInterface $vertex): self
127
    {
128 1
        $this->graph->removeVertex($vertex);
129 1
        return $this;
130
    }
131
132
    /**
133
     * Remove vertices from the graph being built
134
     *
135
     * @param VertexSet $vertices - the vertices to remove
136
     *
137
     * @return self
138
     */
139 1
    public function removeVertices(VertexSet $vertices): self
140
    {
141 1
        foreach ($vertices as $vertex) {
142 1
            $this->removeVertex($vertex);
143
        }
144 1
        return $this;
145
    }
146
147
    /**
148
     * Remove the edge from the graph being built
149
     *
150
     * @param VertexInterface $sourceVertex - the edge source vertex
151
     * @param VertexInterface $tagretVertex - the edge target vertex
152
     * @param EdgeInterface $edge - the edge
153
     *
154
     * @return self
155
     */
156 1
    public function removeEdge(
157
        ?VertexInterface $sourceVertex = null,
158
        ?VertexInterface $targetVertex = null,
159
        ?EdgeInterface $edge = null
160
    ): self {
161 1
        $this->graph->removeEdge($sourceVertex, $targetVertex, $edge);
162 1
        return $this;
163
    }
164
165
    /**
166
     * Get the graph being built
167
     *
168
     * @return GraphInterface
169
     */
170 1
    public function build(): GraphInterface
171
    {
172 1
        return $this->graph;
173
    }
174
}
175