Completed
Push — master ( 195fbe...724c19 )
by Ventaquil
02:47
created

Creator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 49
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addPoint() 0 8 1
A dumpPoints() 0 8 2
A getPoint() 0 8 2
A getPointIndex() 0 4 1
A getPointOrFail() 0 8 2
A searchPoint() 0 4 1
1
<?php
2
3
namespace PHPAlgorithms\Dijkstra;
4
5
use PHPAlgorithms\Dijkstra\Exceptions\CreatorException;
6
7
class Creator {
8
    private $points = array();
9
10
    public function addPoint($label = null)
11
    {
12
        $point = new Point($label);
13
14
        $this->points[] = $point;
15
16
        return $point;
17
    }
18
19
    public function dumpPoints()
20
    {
21
        foreach ($this->points as $index => $point) {
22
            $point->id = $index;
23
        }
24
25
        return $this->points;
26
    }
27
28
    public function getPoint($index)
29
    {
30
        try {
31
            return $this->getPointOrFail($index);
32
        } catch (CreatorException $e) {
33
            return null;
34
        }
35
    }
36
37
    public function getPointIndex(Point $point)
38
    {
39
        return array_search($point, $this->points);
40
    }
41
42
    public function getPointOrFail($index)
43
    {
44
        if (!isset($this->points[$index])) {
45
            throw new CreatorException("Point with index {$index} not exists");
46
        }
47
48
        return $this->points[$index];
49
    }
50
51
    public function searchPoint(Point $point)
52
    {
53
        return in_array($point, $this->points);
54
    }
55
}
56