Calculate::setEndXY()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
    namespace PatchFind;
4
    
5
    use PatchFind\Graph\Generate;
6
    use PatchFind\Algorithm\Dijkstra;
7
    
8
    class Calculate implements CalculateInterface
9
    {
10
        /**
11
         * start x position
12
         * @var int
13
         */
14
        public $startX;
15
        
16
        /**
17
         * start y position
18
         * @var int
19
         */
20
        public $startY;
21
        
22
        /**
23
         * end x position
24
         * @var int
25
         */
26
        public $endX;
27
        
28
        /**
29
         * end y position
30
         * @var int
31
         */
32
        public $endY;
33
        
34
        /**
35
         * initial starting position
36
         *
37
         * @param  int  $x
38
         * @param  int  $y
39
         * @return bool
40
         * @throws Exception\InvalidArgumentException
41
         */
42
        public function setStartXY($x, $y){
43
            if(self::validateInput($x) && self::validateInput($y)){
44
                $this->startX = $x;
45
                $this->startY = $y;
46
            }else{
47
                throw new Exception\InvalidArgumentException('Invalid input data. All value should be an INT, from 0 to 1000');
48
            }
49
            
50
            return true;
51
        }
52
        
53
        /**
54
         * initial ending position
55
         *
56
         * @param  int  $x
57
         * @param  int  $y
58
         * @return bool
59
         * @throws Exception\InvalidArgumentException
60
         */
61
        public function setEndXY($x, $y){
62
            if(self::validateInput($x) && self::validateInput($y)){
63
                $this->endX = $x;
64
                $this->endY = $y;
65
            }else{
66
                throw new Exception\InvalidArgumentException('Invalid input data. All value should be an INT, from 0 to 1000');
67
            }
68
            
69
            return true;
70
        }
71
        
72
        /**
73
         * define graph size and prepare structure
74
         *
75
         * @param  int $width
76
         * @param  int $height
77
         * @return array
78
         */
79
        public function makeGraph($width, $height){
80
            $w = $this->check($width, 'x');
81
            $h = $this->check($height, 'y');
82
            
83
            $graph = new Generate();
84
            $output = $graph->createGraph($w, $h);
85
            
86
            return $output;
87
        }
88
        
89
        /**
90
         * loading obstacle map
91
         *
92
         * @param  string $map
93
         * @return array
94
         * @throws Exception\InvalidArgumentException
95
         */
96
        public function loadGraph($map){
97
            $graph = new Generate();
98
            if(mime_content_type($map)=="image/jpeg"){
99
                $output = $graph->loadFromFile($map);
100
            }else{
101
                throw new Exception\InvalidArgumentException('This is not .jpg file!');
102
            }
103
            return $output;
104
        }
105
        
106
        /**
107
         * calculation of the shortest route
108
         *
109
         * @param  array $graph
110
         * @return array
111
         */
112
        public function getPatch($graph){
113
            $calculate = new Dijkstra();
114
            
115
            $output = $calculate->calculateRoad($this->startX, $this->startY, $this->endX, $this->endY, $graph);
116
            
117
            return $output;
118
        }
119
        
120
        /**
121
         * render the shortest route preview
122
         *
123
         * @param  string  $file
124
         * @param  array   $patch
125
         * @return string
126
         */
127
        public function renderPatch($file, $patch){
128
            $im = imagecreatefromjpeg($file);
129
            if(!$im){
0 ignored issues
show
introduced by
$im is of type false|resource, thus it always evaluated to false.
Loading history...
130
                $red = imagecolorallocatealpha($im, 255, 0, 0, 0); 
131
                foreach ($patch as $key => $value){
132
                        $value2 = substr($value, 0, -1);
133
                        $value3 = substr($value2, 1); 
134
                        $exp = explode(",",$value3);
135
                        $pX = $exp[0];
136
                        $pY = $exp[1];
137
138
                        imagefill($im, $pX, $pY, $red);
139
140
                }
141
142
                imagejpeg($im);
143
                imagedestroy($im);
144
            }else{
145
                $file = "err";
146
            }
147
            return $file;
148
        }
149
        
150
        /**
151
         * check graph size with end position
152
         *
153
         * @param  int    $value
154
         * @param  string $type
155
         * @return int
156
         * @throws Exception\InvalidArgumentException
157
         */
158
        protected function check($value, $type){
159
            switch($type):
160
                case 'x':
161
                    if($this->endX>$value || self::validateInput($value)===false){
162
                        throw new Exception\InvalidArgumentException('Invalid value or type of graph width');
163
                    }
164
                    break;
165
                case 'y':
166
                    if($this->endY>$value || self::validateInput($value)===false){
167
                        throw new Exception\InvalidArgumentException('Invalid value or type of graph height');
168
                    }
169
                    break;
170
            endswitch;
171
           
172
            return $value;
173
        }
174
        
175
        /**
176
         * validate input value
177
         *
178
         * @param  int  $value
179
         * @return bool
180
         */
181
        protected static function validateInput($value){
182
            if(!is_int($value) || $value<0 || $value>1000){
183
                $output = false;
184
            }else{
185
                $output = true;
186
            }
187
            
188
            return $output;
189
        }
190
191
    }
192
193