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

UniformEdgeSpecifics   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
eloc 20
c 1
b 0
f 1
dl 0
loc 136
ccs 30
cts 30
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getEdgeSet() 0 3 1
A remove() 0 4 2
A getEdgeSource() 0 3 1
A containsEdge() 0 3 1
A __construct() 0 3 1
A getEdgeTarget() 0 3 1
A getEdge() 0 6 2
A getEdgeWeight() 0 3 1
A add() 0 11 2
A setEdgeWeight() 0 3 1
1
<?php
2
3
namespace graphp\edge\specifics;
4
5
use BadMethodCallException;
6
use graphp\graph\GraphInterface;
7
use graphp\edge\EdgeInterface;
8
use graphp\edge\EdgeSet;
9
use graphp\vertex\VertexInterface;
10
11
/**
12
 * Class UniformEdgeSpecifics
13
 *
14
 * @package graphp\edge\specifics
15
 */
16
class UniformEdgeSpecifics implements EdgeSpecificsInterface
17
{
18
    /**
19
     * The edge set
20
     *
21
     * @var EdgeSet
22
     */
23
    protected $edgeSet;
24
    
25
    /**
26
     * Construct a new specifics
27
     */
28 22
    public function __construct()
29
    {
30 22
        $this->edgeSet = new EdgeSet();
31 22
    }
32
    
33
    /**
34
     * Check if the edge exists
35
     *
36
     * @param EdgeInterface $edge - the edge
37
     *
38
     * @return bool
39
     */
40 16
    public function containsEdge(EdgeInterface $edge): bool
41
    {
42 16
        return $this->edgeSet->contains($edge);
43
    }
44
    
45
     /**
46
     * Get the edge set
47
     *
48
     * @return EdgeSet
49
     */
50 10
    public function getEdgeSet(): EdgeSet
51
    {
52 10
        return $this->edgeSet;
53
    }
54
    
55
    /**
56
     * Remove the edge from the set
57
     *
58
     * @param EdgeInterface $edge - the edge
59
     */
60 3
    public function remove(EdgeInterface $edge): void
61
    {
62 3
        if ($this->edgeSet->contains($edge)) {
63 3
            $this->edgeSet->remove($edge);
64
        }
65 3
    }
66
    
67
    /**
68
     * Get the edge source vertex
69
     *
70
     * @param EdgeInterface $edge - the edge
71
     *
72
     * @return VertexInterface
73
     */
74 15
    public function getEdgeSource(EdgeInterface $edge): VertexInterface
75
    {
76 15
        return $edge->getSource();
77
    }
78
    
79
     /**
80
     * Get the edge target vertex
81
     *
82
     * @param EdgeInterface $edge - the edge
83
     *
84
     * @return VertexInterface
85
     */
86 15
    public function getEdgeTarget(EdgeInterface $edge): VertexInterface
87
    {
88 15
        return $edge->getTarget();
89
    }
90
    
91
    /**
92
     * Get the edge weight
93
     *
94
     * @param EdgeInterface $edge - the edge
95
     *
96
     * @return float
97
     */
98 1
    public function getEdgeWeight(EdgeInterface $edge): float
99
    {
100 1
        return GraphInterface::DEFAULT_EDGE_WEIGHT;
101
    }
102
    
103
    /**
104
     * Set the edge weight
105
     *
106
     * @param EdgeInterface $edge - the edge
107
     * @param float $weight - the weight
108
     *
109
     * @throws BadMethodCallException
110
     */
111 1
    public function setEdgeWeight(EdgeInterface $edge, float $weight): void
112
    {
113 1
        throw new BadMethodCallException("Method is not supported by this type of edge");
114
    }
115
    
116
    /**
117
     * Get the edge
118
     *
119
     * @param EdgeInterface $edge - the edge
120
     *
121
     * @return null|EdgeInterface
122
     */
123 1
    public function getEdge(EdgeInterface $edge): ?EdgeInterface
124
    {
125 1
        if (($id = array_search($edge, $this->edgeSet->getArrayCopy())) !== false) {
126 1
            return $this->edgeSet[$id];
127
        }
128 1
        return null;
129
    }
130
    
131
    /**
132
     * Add a new edge connecting the specified vertices
133
     * Return true, if edge was added
134
     *
135
     * @param EdgeInterface $edge - the edge
136
     * @param VertexInterface $sourceVertex - the source vertex
137
     * @param VertexInterface $sourceVertex - tge target vertex
138
     *
139
     * @return bool
140
     */
141 16
    public function add(EdgeInterface $edge, VertexInterface $sourceVertex, VertexInterface $targetVertex): bool
142
    {
143 16
        $edge->setSource($sourceVertex);
144 16
        $edge->setTarget($targetVertex);
145
146 16
        if (!$this->containsEdge($edge)) {
147 16
            $this->edgeSet[] = $edge;
148 16
            return true;
149
        }
150
151 1
        return false;
152
    }
153
}
154