Issues (28)

src/IP/Controller.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace Faxity\IP;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * Controller for the /ip routes
10
 */
11
class Controller implements ContainerInjectableInterface
12
{
13
    use ContainerInjectableTrait;
14
15
    /** List of IP addresses to have available as test */
16
    private const TEST_ADDRS = [
17
        "2001:db8::1",
18
        "2a03:2880:f00a:8:face:b00c:0:2",
19
        "1200::ABYX:1234::2552:7777:1313",
20
        "194.47.150.9",
21
        "192.168.1.20",
22
        "83.230.116.044",
23
    ];
24
25
    /** API response example for a failed response */
26
    private const API_EXAMPLE_ERR = [
27
        "message" => "Ingen IP address skickades.",
28
    ];
29
30
    /** API response example for a successfull response */
31
    private const API_EXAMPLE_OK = [
32
        "ip" => "194.47.150.9",
33
        "valid" => true,
34
        "domain" => "dbwebb.se",
35
        "type" => "ipv4",
36
        "region" => "Blekinge",
37
        "country" => "Sweden",
38
        "location" => [
39
            "latitude"  => 56.16122055053711,
40
            "longitude" => 15.586899757385254,
41
        ],
42
    ];
43
44
    /**
45
     * @var object $examples API response examples
46
     */
47
    private $examples;
48
49
50
    /**
51
     * Initializer for the class
52
     */
53 3
    public function initialize()
54
    {
55 3
        $this->examples = (object) [
56 3
            "err" => json_encode(self::API_EXAMPLE_ERR, JSON_PRETTY_PRINT),
57 3
            "ok" => json_encode(self::API_EXAMPLE_OK, JSON_PRETTY_PRINT),
58
        ];
59 3
    }
60
61
62
    /**
63
     * Handles / for the controller
64
     *
65
     * @return object
66
     */
67 3
    public function indexActionGet() : object
68
    {
69
        // Deal with the action and return a response.
70 3
        $ip = $this->di->request->getGet("ip");
0 ignored issues
show
Accessing request on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
71
72 3
        if (empty($ip)) {
73 1
            $ip = $this->di->ip->getAddress();
0 ignored issues
show
Accessing ip on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
74
        }
75
76 3
        $res = (array) $this->di->ip->validate($ip);
77
78 3
        $this->di->page->add("faxity/ip/index", $res);
0 ignored issues
show
Accessing page on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
79 3
        $this->di->page->add("faxity/ip/api", [
80 3
            "ip"       => $ip,
81 3
            "addrs"    => $this::TEST_ADDRS,
82 3
            "apiUrl"   => $this->di->url->create("ip-api"),
0 ignored issues
show
Accessing url on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
83 3
            "examples" => $this->examples,
84
        ]);
85
86 3
        return $this->di->page->render([
87 3
            "title" => "IP validerare",
88
        ]);
89
    }
90
}
91