IpRestfulController::indexActionPost()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 5
nop 0
crap 4
1
<?php
2
3
namespace KW\KWcontroller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
class IpRestfulController implements ContainerInjectableInterface
9
{
10
    use ContainerInjectableTrait;
11
12
    /**
13
     * This is the index method action, it handles:
14
     * POST METHOD mountpoint
15
     * POST METHOD mountpoint/
16
     * POST METHOD mountpoint/index
17
     * Return status of given ip-number
18
     *
19
     * @return array
20
     */
21 2
    public function indexActionPost() : array
22
    {
23
        //$ipn = $this->di->request->getPost("ipnummer");
24 2
        $ipn = $this->di->get("request")->getPost("ipnummer");
25 2
        if (!filter_var($ipn, FILTER_VALIDATE_IP)) {
26
            $json = [
27 1
                    "ip-number" => $ipn,
28 1
                    "message" => "Not a valid ip-number"
29
                ];
30
        } else {
31 1
            $type = (filter_var($ipn, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? "ipv4" : "ipv6" );
32 1
            $domainname = gethostbyaddr($ipn);
33
34 1
            $domainname = ($domainname == $ipn ? "none" : $domainname);
35
36
            $json = [
37 1
                    "ip-number" => $ipn,
38 1
                    "message" => "Valid ip-number",
39 1
                    "type" => $type,
40 1
                    "domain name" => $domainname
41
                ];
42
        }
43 2
        return [$json];
44
    }
45
}
46