JsonIpValidatorController1::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
nc 1
nop 0
dl 0
loc 11
c 1
b 0
f 0
cc 1
ccs 0
cts 9
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Moody\ControllerIP;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * A sample controller to show how a controller class can be implemented.
10
 * The controller will be injected with $di if implementing the interface
11
 * ContainerInjectableInterface, like this sample class does.
12
 * The controller is mounted on a particular route and can then handle all
13
 * requests for that mount point.
14
 *
15
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
16
 */
17
class JsonIpValidatorController1 extends IpValidate implements ContainerInjectableInterface
18
{
19
    use ContainerInjectableTrait;
20
21
    /**
22
     * @var string
23
     */
24
    private $enteredIp;
25
    private $ipValidatorClass;
26
27
    /**
28
     * This is the index method action, it handles:
29
     * ANY METHOD mountPoint
30
     * ANY METHOD mountPoint/
31
     * ANY METHOD mountPoint/index
32
     *
33
     * @return string
34
     */
35
    public function indexAction() : array
36
    {
37
        $title = "IP validator with JSON response";
0 ignored issues
show
Unused Code introduced by
The assignment to $title is dead and can be removed.
Loading history...
38
        $page = $this->di->get("page");
0 ignored issues
show
Unused Code introduced by
The assignment to $page is dead and can be removed.
Loading history...
39
        $request = $this->di->get("request");
40
        $this->enteredIp = $request->getGet("ip");
41
        $this->ipValidatorClass = new IpValidate();
42
        $json = $this->ipToJson($this->enteredIp, $this->ipValidatorClass);
43
        $data['json'] = $json;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
44
45
        return $data['json'];
46
    }
47
48
        /**
49
     * This is the index method action, it handles:
50
     * ANY METHOD mountPoint
51
     * ANY METHOD mountPoint/
52
     * ANY METHOD mountPoint/index
53
     *
54
     * @return string
55
     */
56
    public function jsonIpActionGet() : array
57
    {
58
        $title = "IP validator with JSON response";
0 ignored issues
show
Unused Code introduced by
The assignment to $title is dead and can be removed.
Loading history...
59
        $page = $this->di->get("page");
0 ignored issues
show
Unused Code introduced by
The assignment to $page is dead and can be removed.
Loading history...
60
        $request = $this->di->get("request");
61
        $this->enteredIp = $request->getGet("ip");
62
        $this->ipValidatorClass = new IpValidate();
63
        $json = $this->ipToJson($this->enteredIp, $this->ipValidatorClass);
64
        $data['json'] = $json;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
65
66
        return $data['json'];
67
    }
68
69
70
    /**
71
     * Check if IP is valid or not and return with host.
72
     * GET ip
73
     *
74
     * @return array
75
     */
76
    public function ipToJson($ipAddress, $object) : array
77
    {
78
        $json = [
79
            "Protocol" => $object->getProtocol($ipAddress) ?? null,
80
            "Domain" => $object->getDomain($ipAddress) ?? null,
81
            "address" => $object->getAddress($ipAddress) ?? null,
82
        ];
83
        return [$json];
84
    }
85
}
86