1 | <?php |
||
2 | |||
3 | namespace Anax\Controller; |
||
4 | |||
5 | use Anax\Commons\ContainerInjectableInterface; |
||
6 | use Anax\Commons\ContainerInjectableTrait; |
||
7 | |||
8 | use Anax\models\ipAdress; |
||
9 | |||
10 | // use Anax\Route\Exception\ForbiddenException; |
||
11 | // use Anax\Route\Exception\NotFoundException; |
||
12 | // use Anax\Route\Exception\InternalErrorException; |
||
13 | |||
14 | /** |
||
15 | * A sample controller to show how a controller class can be implemented. |
||
16 | * The controller will be injected with $di if implementing the interface |
||
17 | * ContainerInjectableInterface, like this sample class does. |
||
18 | * The controller is mounted on a particular route and can then handle all |
||
19 | * requests for that mount point. |
||
20 | * |
||
21 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
||
22 | */ |
||
23 | class IpController implements ContainerInjectableInterface |
||
24 | { |
||
25 | use ContainerInjectableTrait; |
||
26 | |||
27 | /** |
||
28 | * @var string $db a sample member variable that gets initialised |
||
29 | */ |
||
30 | private $db = "not active"; |
||
31 | |||
32 | /** |
||
33 | * The initialize method is optional and will always be called before the |
||
34 | * target method/action. This is a convienient method where you could |
||
35 | * setup internal properties that are commonly used by several methods. |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | 9 | public function initialize() : void |
|
40 | { |
||
41 | // Use to initialise member variables. |
||
42 | 9 | $this->db = "active"; |
|
43 | 9 | } |
|
44 | |||
45 | /** |
||
46 | * This is the index method action, it handles: |
||
47 | * ANY METHOD mountpoint |
||
48 | * ANY METHOD mountpoint/ |
||
49 | * ANY METHOD mountpoint/index |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | 1 | public function indexAction() : string |
|
54 | { |
||
55 | // Deal with the action and return a response. |
||
56 | 1 | return __METHOD__ . ", \$db is {$this->db}"; |
|
57 | } |
||
58 | |||
59 | 1 | public function jsonActionGet() : array |
|
60 | { |
||
61 | 1 | $services = implode(", ", $this->di->getServices()); |
|
62 | $json = [ |
||
63 | "message" => __METHOD__ . "<p>\$di contains: |
||
64 | 1 | $services", |
|
65 | 1 | "di" => $this->di->getServices(), |
|
66 | ]; |
||
67 | 1 | return [$json]; |
|
68 | } |
||
69 | |||
70 | 1 | public function pageActionGet() : object |
|
71 | { |
||
72 | 1 | $page = $this->di->get("page"); |
|
73 | |||
74 | 1 | $ipModel = new ipAdress(); |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
75 | // $userIp = $ipModel->getUserIp($this->di->get("request")); |
||
76 | |||
77 | $data = [ |
||
78 | 1 | "content" => "<h3>IP-adress validator</h3>", |
|
79 | "contentJSON" => "<h3>IP-adress validator (JSON)", |
||
80 | ]; |
||
81 | |||
82 | 1 | $title = "IP validator"; |
|
83 | |||
84 | 1 | $page->add("ip/ip", $data); |
|
85 | 1 | return $page->render([ |
|
86 | 1 | "title" => $title |
|
87 | ]); |
||
88 | } |
||
89 | |||
90 | 3 | public function checkIPActionPost() : object |
|
91 | { |
||
92 | 3 | $ipAdress = $this->di->get("request")->getPost("ipadress"); |
|
93 | 3 | $ipModel = new ipAdress($ipAdress); |
|
94 | 3 | $valRes = $ipModel->validateIp(); |
|
95 | |||
96 | 3 | $page = $this->di->get("page"); |
|
97 | $data = [ |
||
98 | 3 | "content" => "<h3>IP-adress validator</h3>", |
|
99 | 3 | "contentJSON" => "<h3>IP-adress validator (JSON)", |
|
100 | 3 | "result" => "<p>" . $valRes[0] . "</p>", |
|
101 | 3 | "host" => $valRes[1] |
|
102 | ]; |
||
103 | |||
104 | 3 | $title = "IP validator"; |
|
105 | |||
106 | 3 | $page->add("ip/ip", $data); |
|
107 | 3 | return $page->render([ |
|
108 | 3 | "title" => $title |
|
109 | ]); |
||
110 | } |
||
111 | |||
112 | 3 | public function checkIPJSONActionGet() : array |
|
113 | { |
||
114 | 3 | $ipJSON = $this->di->get("request")->getGet("ipadressJSON"); |
|
115 | 3 | $ipModel = new ipAdress($ipJSON); |
|
116 | 3 | $valRes = $ipModel->validateIp(); |
|
117 | |||
118 | $json = [ |
||
119 | 3 | "ipAdress" => $ipJSON, |
|
120 | 3 | "ipRes" => $valRes[0], |
|
121 | 3 | "hostJSON" => $valRes[1] |
|
122 | ]; |
||
123 | 3 | return [$json]; |
|
124 | } |
||
125 | } |
||
126 |