Completed
Push — master ( 4fc6a7...099af8 )
by Ventaquil
03:19
created

Creator::createPointFromID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
namespace Algorithms\Dijkstra;
3
4
class Creator
5
{
6
    private $points = array();
7
    private $labels = array();
8
9
    private static function checkPointID($point)
10
    {
11
        if (is_int($point) && ($point > 0)) {
12
            return 'int';
13
        } else {
14
            return 'string';
15
        }
16
    }
17
18
    private function randNewID()
19
    {
20
        do {
21
            $rand = mt_rand();
22
        } while (isset($this->points[$rand]));
23
24
        return $rand;
25
    }
26
27
    private function createPointFromID($point_id, $label = null)
28
    {
29
        if (empty($label)) {
30
            $new_point = Point::create($point_id);
31
        } else {
32
            $new_point = Point::create($point_id, $label);
33
        }
34
35
        $this->points[$point_id] = $new_point;
36
37
        return $new_point;
38
    }
39
40
    public function addPoint($point)
41
    {
42
        if (Point::checkPoint($point)) {
43
            $id = $point->getID();
44
45
            $this->points[$id] = $point;
46
47
            $label = $point->getLabel();
48
            if (!empty($label)) {
49
                $this->labels[$label] = $id;
50
            }
51
52
            return $point;
53
        } elseif (self::checkPointID($point) == 'int') {
54
            if (!isset($this->points[$point])) {
55
                return self::createPointFromID($point);
56
            } else {
57
                throw new CreatorException('Point was added earlier');
58
            }
59
        } else {
60
            if (!isset($this->labels[$point])) {
61
                $id = $this->randNewID();
62
63
                $this->labels[$point] = $id;
64
65
                return self::createPointFromID($id);
66
            } else {
67
                throw new CreatorException('Point was added earlier');
68
            }
69
        }
70
    }
71
72
    public function getPoint($point_id)
73
    {
74
        try {
75
            return $this->getPointOrFail($point_id);
76
        } catch (PointException $e) {
77
            return NULL;
78
        }
79
    }
80
81
    public function getPointOrFail($point_id)
82
    {
83
        if (isset($this->points[$point_id])) {
84
            return $this->points[$point_id];
85
        } elseif (isset($this->labels[$point_id])) {
86
            return $this->points[$this->labels[$point_id]];
87
        } else {
88
            throw new PointException('Point not exists');
89
        }
90
    }
91
92
    public function createConnections()
93
    {
94
        $relations = array();
95
96
        foreach ($this->points as $point_id => $point) {
97
            $relations[$point_id] = $point->getDinstances();
98
99
            foreach ($relations[$point_id] as $array_to_analyze) {
100
                if (!isset($relations[$array_to_analyze[0]])) {
101
                    $relations[$array_to_analyze[0]] = array();
102
                }
103
            }
104
        }
105
106
        return $relations;
107
    }
108
}
109