IpTestJSONController::indexActionGet()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 30
ccs 19
cts 19
cp 1
crap 4
rs 9.6
1
<?php
2
3
namespace Lioo19\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
// use Anax\Route\Exception\ForbiddenException;
9
// use Anax\Route\Exception\NotFoundException;
10
// use Anax\Route\Exception\InternalErrorException;
11
12
/**
13
 *
14
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
15
 */
16
class IpTestJSONController implements ContainerInjectableInterface
17
{
18
    use ContainerInjectableTrait;
19
20
    /**
21
     * This is the index method action, it handles:
22
     * ANY METHOD mountpoint
23
     * ANY METHOD mountpoint/
24
     * ANY METHOD mountpoint/index
25
     *
26
     * @return array
27
     */
28 2
    public function indexActionGet() : array
29
    {
30 2
        $request = $this->di->get("request");
31
        //request to get GET-info
32 2
        $userip = $request->getGet("ip", "Ingen ip angiven");
33
34 2
        if ($userip) {
35 2
            $validation = $this->di->get("iptest");
36 2
            $validation->setInput($userip);
37 2
            $ip4 = $validation->ip4test();
38 2
            $ip6 = $validation->ip6test();
39
        }
40
41 2
        if ($ip6 || $ip4) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ip6 does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $ip4 does not seem to be defined for all execution paths leading up to this point.
Loading history...
42 1
            $hostname = gethostbyaddr($userip);
43 1
            $geoInfo = $this->getGeo($userip);
44
        } else {
45 1
            $hostname = "Ej korrekt ip";
46 1
            $geoInfo = "Inget att visa";
47
        }
48
49
        $data = [
50 2
            "ip" => $userip,
51 2
            "ip4" => $ip4,
52 2
            "ip6" => $ip6,
53 2
            "host" => $hostname,
54 2
            "geoInfo" => $geoInfo,
55
        ];
56
57 2
        return [$data];
58
    }
59
60
    public function getGeo($userip)
61
    {
62
        $geo = $this->di->get("ipgeo");
63
        $geo->setInput($userip);
64
        $geoInfo = $geo->fetchGeo();
65
66
        return $geoInfo;
67
    }
68
}
69