Issues (26)

src/Controller/IpController.php (3 issues)

1
<?php
2
3
namespace Anax\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
 * A sample controller to show how a controller class can be implemented.
14
 * The controller will be injected with $di if implementing the interface
15
 * ContainerInjectableInterface, like this sample class does.
16
 * The controller is mounted on a particular route and can then handle all
17
 * requests for that mount point.
18
 *
19
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
20
 */
21
class IpController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
26
27
    /**
28
     * @var string $db a sample member variable that gets initialised
29
     */
30
    private $db = "not active";
31
32
33
34
    /**
35
     * The initialize method is optional and will always be called before the
36
     * target method/action. This is a convienient method where you could
37
     * setup internal properties that are commonly used by several methods.
38
     *
39
     * @return void
40
     */
41
    public function initialize() : void
42
    {
43
        // Use to initialise member variables.
44
        $this->db = "active";
45
    }
46
47
48
49
    /**
50
     * This is the index method action, it handles:
51
     * ANY METHOD mountpoint
52
     * ANY METHOD mountpoint/
53
     * ANY METHOD mountpoint/index
54
     *
55
     * @return string
56
     */
57
    public function indexAction() : object
58
    {
59
        $title = "Ip Validator";
60
61
        $page = $this->di->get("page");
62
        // $session = $this->di->get("session");
63
64
        // $active = $session->get(self::$key, null);
65
66
        $page->add("anax/ipvalidator/index");
67
68
        return $page->render([
69
            "title" => $title,
70
        ]);
71
    }
72
73
74
     /**
75
      * Update current selected style.
76
      *
77
      * @return object
78
      */
79
    public function indexActionPost() : object
80
    {
81
        $response = $this->di->get("response");
0 ignored issues
show
The assignment to $response is dead and can be removed.
Loading history...
82
        $request = $this->di->get("request");
83
        $session = $this->di->get("session");
0 ignored issues
show
The assignment to $session is dead and can be removed.
Loading history...
84
        $ip = $request->getPost("ip");
85
        $domain = "Not found";
86
87
        if (filter_var($ip, FILTER_VALIDATE_IP)) {
88
            if (gethostbyaddr($ip) != $ip) {
89
                $domain = gethostbyaddr($ip);
90
                $res = "`$ip` is a valid IP Adress. Domainname: $domain";
91
            }
92
        } else {
93
            $res = "`$ip` is not a valid IP Adress. Domainname: $domain";
94
        }
95
96
97
        $title = "Ip Validator";
98
99
        $page = $this->di->get("page");
100
101
        $page->add("anax/ipvalidator/result", [
102
            "res" => $res,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.
Loading history...
103
        ]);
104
105
        return $page->render([
106
            "title" => $title,
107
        ]);
108
    }
109
}
110