Completed
Push — master ( dd934e...088db2 )
by T
01:58
created

VisJs   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 45
ccs 25
cts 25
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getOutput() 0 38 4
1
<?php
2
3
namespace tomzx\Graphp\VisJs;
4
5
use Fhaculty\Graph\Edge\Directed;
6
use Fhaculty\Graph\Exporter\ExporterInterface;
7
use Fhaculty\Graph\Graph;
8
9
class VisJs implements ExporterInterface
10
{
11
	/**
12
	 * @param \Fhaculty\Graph\Graph $graph
13
	 * @return string
14
	 */
15 6
	public function getOutput(Graph $graph)
16
	{
17 6
		$vertices = [];
18
		/** @var \Fhaculty\Graph\Vertex $vertex */
19 6
		foreach ($graph->getVertices() as $vertex) {
20
			$data = [
21 5
				'id'    => $vertex->getId(),
22 5
			];
23
24 5
			$vertexAttribute = $vertex->getAttributeBag()->getAttributes();
25 5
			$data += $vertexAttribute;
26
27 5
			$vertices[] .= json_encode($data);
28 6
		}
29 6
		$output = 'var nodes = [' . PHP_EOL . implode(',' . PHP_EOL, $vertices) . PHP_EOL . '];' . PHP_EOL;
30
31 6
		$edges = [];
32
		/** @var \Fhaculty\Graph\Edge\Base $edge */
33 6
		foreach ($graph->getEdges() as $edge) {
34 3
			$vertices = $edge->getVertices()->getVector();
35
			$data = [
36 3
				'from' => $vertices[0]->getId(),
37 3
				'to'   => $vertices[1]->getId(),
38 3
			];
39
40 3
			if ($edge instanceof Directed) {
41 2
				$data['arrows'] = 'to';
42 2
			}
43
44 3
			$edgeAttributes = $edge->getAttributeBag()->getAttributes();
45 3
			$data += $edgeAttributes;
46
47 3
			$edges[] = json_encode($data);
48 6
		}
49 6
		$output .= 'var edges = [' . PHP_EOL . implode(',' . PHP_EOL, $edges) . PHP_EOL . '];';
50
51 6
		return $output;
52
	}
53
}
54