SimpleGraph::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2.0438

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 4
dl 0
loc 25
ccs 14
cts 18
cp 0.7778
crap 2.0438
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Graphp\Graph\Types;
4
5
use Graphp\Graph\AbstractGraph;
6
use Graphp\Graph\Builder\GraphTypeBuilder;
7
use Graphp\Graph\Builder\GraphBuilder;
8
use Graphp\Util\SupplierUtil;
9
use Graphp\Util\SupplierInterface;
10
11
/**
12
 * Class SimpleGraph
13
 *
14
 * @package  Graphp\Graph\Types
15
 */
16
class SimpleGraph extends AbstractGraph
17
{
18
    /**
19
     * Create a new simple graph
20
     *
21
     * @param string $edgeClass - the edge class
22
     * @param SupplierInterface $vertexSupplier - the vertex supplier
23
     * @param SupplierInterface $edgeSupplier - the edge supplier
24
     * @param bool $weighted - if the graph is weighted
25
     */
26 8
    public function __construct(
27
        ?string $edgeClass = null,
28
        ?SupplierInterface $vertexSupplier = null,
29
        ?SupplierInterface $edgeSupplier = null,
30
        ?bool $weighted = null
31
    ) {
32 8
        $builder = new GraphTypeBuilder();
33 8
        $graphType = $builder->undirected()
34 8
                    ->allowSelfLoops(false)
35 8
                    ->allowMultipleEdges(false)
36 8
                    ->weighted($weighted)
37 8
                    ->build();
38 8
        if (!is_null($edgeClass)) {
39 8
            $util = new SupplierUtil();
40 8
            $edgeSupplier = $util->createSupplier($edgeClass);
41 8
            parent::__construct(
42 8
                $vertexSupplier,
43 8
                $edgeSupplier,
44 8
                $graphType
45
            );
46
        } else {
47
            parent::__construct(
48
                $vertexSupplier,
49
                $edgeSupplier,
50
                $graphType
51
            );
52
        }
53 8
    }
54
55
    /**
56
     * Create a simple graph builder
57
     *
58
     * @param string $edgeClass - the edge class
59
     * @param SupplierInterface $edgeSupplier - the edge supplier
60
     *
61
     * @return GraphBuilder
62
     */
63
    public function createBuilder(?string $edgeClass = null, ?SupplierInterface $edgeSupplier = null): GraphBuilder
64
    {
65
        if (!is_null($edgeClass)) {
66
            return new GraphBuilder(new SimpleGraph($edgeClass));
67
        }
68
        return new GraphBuilder(new SimpleGraph(null, $edgeSupplier));
69
    }
70
}
71