Completed
Push — master ( 1d579c...acecc2 )
by Magnus
02:07
created

IPHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 70.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 132
ccs 29
cts 41
cp 0.7073
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A checkOwnIP() 0 4 2
A minLat() 0 5 1
A checkIP() 0 28 3
A maxLong() 0 5 1
A minLong() 0 5 1
A maxLat() 0 5 1
A largeMapLink() 0 8 2
A mapLink() 0 9 2
1
<?php
2
3
namespace Malm18\IPChecker;
4
5
class IPHandler
6
{
7
8
9
    /**
10
    * Check active.
11
    *
12
    */
13
    // public function checkIP($theIP)
14
    // {
15
    //     $theIP2 = $theIP . " svansen";
16
    //     return $theIP2;
17
    // }
18
19
    // public function checkIP2($theIP)
20
    // {
21
    //     $hostname = "";
22
    //     $type = "";
23
    //
24
    //
25
    //     if (filter_var($theIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
26
    //         $type = "IPv6";
27
    //         $hostname = gethostbyaddr("$theIP");
28
    //     } elseif (filter_var($theIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
29
    //         $type = "IPv4";
30
    //         $hostname = gethostbyaddr("$theIP");
31
    //     } else {
32
    //         $type = "Inte riktig IP-adress";
33
    //     }
34
    //
35
    //     $ipInfo = array("ipaddress"=>$theIP, "hostname"=>$hostname, "type"=>$type);
36
    //     return $ipInfo;
37
    // }
38
39
40 2
    public function checkIP($theIP)
41
    {
42 2
        if (filter_var($theIP, FILTER_VALIDATE_IP)) {
43
            $url = 'http://api.ipstack.com/';
44
            $keys = require ANAX_INSTALL_PATH . "/config/keys.php";
45
            // $this->ipstackKey = $keys["ipstackKey"];
46
            // $apiKey = $this->ipstackKey;
47
            $apiKey = $keys["ipstackKey"];
48
            $requestUrl = $url . $theIP . '?access_key=' . $apiKey;
49
            $curl = curl_init($requestUrl);
50
            if ($curl) {
0 ignored issues
show
introduced by
$curl is of type false|resource, thus it always evaluated to false.
Loading history...
51
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
52
        // curl_setopt($curl, CURLOPT_HTTPHEADER, [
53
        //   'X-RapidAPI-Host: kvstore.p.rapidapi.com',
54
        //   'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxx',
55
        //   'Content-Type: application/json'
56
        // ]);
57
            $response = curl_exec($curl);
58
            $response2 = json_decode($response, true);
59
            curl_close($curl);
60
            // echo $response . PHP_EOL;
61
            return $response2;
62
        }    
63
        } else {
64 2
            $response = array("type" => "not valid ip", "ip" => "", "latitude"=> "", "longitude"=> "",
65
            "city" => "", "country_name" => "", "region_name" => "", "continent_name" => "", "location['country_code']" => "");
66
            // $response2 = json_decode($response, true);
67 2
            return $response;
68
        }
69
    }
70
71
72 4
    public function minLong($longitude)
73
    {
74
75 4
        $minLong = floatval($longitude)-0.6427;
76 4
        return $minLong;
77
    }
78
79 4
    public function maxLong($longitude)
80
    {
81
82 4
        $maxLong = floatval($longitude)+0.6427;
83 4
        return $maxLong;
84
    }
85
86 4
    public function minLat($latitude)
87
    {
88
89 4
        $minLat = (floatval($latitude)) - 0.260;
90 4
        return $minLat;
91
    }
92
93 4
    public function maxLat($latitude)
94
    {
95
96 4
        $maxLat = (floatval($latitude)) + 0.260;
97 4
        return $maxLat;
98
    }
99
100
101
    // public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
102
    // {
103
    //                 https://www.openstreetmap.org/export/embed.html?bbox=-6.8860333396912%2C53.093889465332%2C-5.6006333396912%2C53.613889465332&amp;layer=mapnik&amp;marker=53.353889465332%2-6.2433333396912
104
    //     // $link = "https://www.openstreetmap.org/export/embed.html?bbox=12.669982910156252%2C55.56592203025787%2C13.955383300781252%2C56.08506381314523&amp;layer=mapnik&amp;marker=55.82635894724891%2C13.31268310546875"
105
    //
106
    //     $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
107
    //
108
    //     return $link;
109
    // }
110
111 4
    public function mapLink($latitude, $longitude, $minLat, $maxLat, $minLong, $maxLong)
112
    {
113 4
        if ($latitude) {
114 3
            $link = "https://www.openstreetmap.org/export/embed.html?bbox=" . $minLong . "%2C" . $minLat . "%2C" . $maxLong . "%2C" . $maxLat . "&amp;layer=mapnik&amp;marker=" . $latitude . "%2C" . $longitude;
115
        } else {
116 1
            $link = "https://www.openstreetmap.org/export/embed.html?bbox=-0.64%2C85%2C0.64%2C90&amp;layer=mapnik&amp;marker=87.5%2C0";
117
        }
118
119 4
        return $link;
120
    }
121
122 1
    public function largeMapLink($latitude, $longitude)
123
    {
124 1
        $link = "";
125 1
        if ($latitude) {
126 1
            $link = "https://www.openstreetmap.org/?mlat=" . $latitude . "&amp;mlon=" . $longitude . "#map=10/" . $latitude . "/" . $longitude;
127
        // <a href="https://www.openstreetmap.org/?mlat=55.8264&amp;mlon=13.3127#map=10/55.8264/13.3127">
128
        }
129 1
        return $link;
130
    }
131
132
133 3
    public function checkOwnIP()
134
    {
135 3
        $remoteAddr = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'127.0.0.1';
136 3
        return $remoteAddr;
137
    }
138
139
140
141
142
// echo "http://api.ipstack.com/130.235.136.64?access_key=d1efc4cc23a8b14dfad565ee6bde80b8";
143
144
    // /**
145
    // * Check active.
146
    // *
147
    // * @return variable , variable
148
    // */
149
    // public function active($computer, $human)
150
    // {
151
    //     if ($computer->value() > $human->value()) {
152
    //         $computer->setActive(true);
153
    //         return $computer->getName();
154
    //     } else {
155
    //         $human->setActive(true);
156
    //         return $human->getName();
157
    //     }
158
    // }
159
160
161
    // /**
162
    // * Check active in another way.
163
    // *
164
    // * @return variable , variable
165
    // */
166
    // public function getActive($computer, $human)
167
    // {
168
    //     if ($computer->getActive() == true) {
169
    //         return $computer->getName();
170
    //     }
171
    //     return $human->getName();
172
    // }
173
174
175
176
    // /**
177
    // * Check active in yet another way.
178
    // *
179
    // * @return string , string
180
    // */
181
    // public function getActive2($computer, $human)
182
    // {
183
    //     if ($human->getTotalScore() >= 100) {
184
    //         $human->setWinner();
185
    //         return "pig/gameOver";
186
    //     } elseif ($computer->getTotalScore() >= 100) {
187
    //         $computer->setWinner();
188
    //         return "pig/gameOver";
189
    //     } else {
190
    //         if ($computer->getActive() == true) {
191
    //             return "pig/playC";
192
    //         } elseif ($human->getActive() == true) {
193
    //             if ($human->getDie1() !==1 && $human->getDie2() !==1) {
194
    //                 return "pig/playH";
195
    //             } else {
196
    //                 return "pig/playC";
197
    //             }
198
    //         }
199
    //     }
200
    // }
201
202
   // /**
203
   //  * mainRoll.
204
   //  *
205
   //  * @return void , void
206
   //  */
207
   //  public function mainRoll($human, $computer)
208
   //  {
209
   //      if ($computer->getActive() == true) {
210
   //          $computer->setActive(false);
211
   //          $human->setActive(true);
212
   //      } else {
213
   //          // $human->setTotalScore($human->getTotalScore + $human->getTurnScore);
214
   //          $computer->setActive(true);
215
   //          $human->setActive(false);
216
   //      }
217
   //      $computer->setTotalScore($computer->getTotalScore() + $computer->getTurnScore());
218
   //      $human->setTotalScore($human->getTotalScore() + $human->getTurnScore());
219
   //      $computer->setTurnScore(0);
220
   //      $human->setTurnScore(0);
221
   //      $computer->setRolls(0);
222
   //      $human->setRolls(0);
223
   //      $human->setDie1(null);
224
   //      $human->setDie2(null);
225
   //      $computer->setDie1(null);
226
   //      $computer->setDie2(null);
227
   //  }
228
229
230
    // /**
231
    //  * mainRoll.
232
    //  *
233
    //  * @return void , void
234
    //  */
235
    // public function mainRoll2($human, $computer)
236
    // {
237
    //     if ($computer->getActive() !== true) {
238
    //         $human->roll2();
239
    //         $human->setRolls($human->getRolls() + 1);
240
    //         if ($human->getDie1() !==1 && $human->getDie2() !==1) {
241
    //             $human->setTurnScore($human->getTurnScore() + $human->getDiceSum());
242
    //         } else {
243
    //             $human->setTurnScore(0);
244
    //         }
245
    //     }
246
    // }
247
248
249
    // /**
250
    //  * mainRoll.
251
    //  *
252
    //  * @return void , void
253
    //  */
254
    // public function computerRoll($computer, $human)
255
    // {
256
    //     $totalHuman = $human->getTotalScore();
257
    //     if ($computer->getActive() == true) {
258
    //         do {
259
    //             $computer->roll2();
260
    //             $computer->setRolls($computer->getRolls() + 1);
261
    //             $computer->setTurnScore($computer->getTurnScore() + $computer->getDiceSum());
262
    //         } while ($computer->rollOrNot($totalHuman) > 1 && ($computer->getDie1() !==1 && $computer->getDie2() !==1));
263
    //         if ($computer->getDie1() ==1 || $computer->getDie2() ==1) {
264
    //             $computer->setTurnScore(0);
265
    //         }
266
    //     }
267
    // }
268
269
270
    // /**
271
    //  * isWinner
272
    //  * @return string , isWinner
273
    //  */
274
    //
275
    // public function isWinner2($human, $computer)
276
    // {
277
    //     if ($human->isWinner() == true) {
278
    //         return $human->getName();
279
    //     } else {
280
    //         return $computer->getName();
281
    //     }
282
    // }
283
}
284