IpApiGeoController::indexActionGet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 15
nc 2
nop 0
dl 0
loc 26
c 0
b 0
f 0
cc 2
ccs 15
cts 15
cp 1
crap 2
rs 9.7666
1
<?php
2
3
namespace Blixter\IpGeolocation;
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 IpApiGeoController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
    /**
26
     * This is the index method action, it handles:
27
     * GET METHOD mountpoint
28
     * GET METHOD mountpoint/
29
     * GET METHOD mountpoint/index
30
     *
31
     * @return Array
32
     */
33 2
    public function indexActionGet(): array
34
    {
35 2
        $request = $this->di->get("request");
36 2
        $ipaddress = $request->getGet("ip");
37
        // Using ipValidation class from $di.
38 2
        $ipValidation = $this->di->get("ipvalidation");
39
        // Get ipgeo from $di
40 2
        $curlhandler = $this->di->get("curlhandler");
41 2
        $ipGeoModel = $this->di->get("ipgeo");
42 2
        $ipGeoModel->setCurl($curlhandler);
43
44 2
        $isIpValid = $ipValidation->isIpValid($ipaddress);
45
46 2
        if ($isIpValid) {
47 1
            $domain = $ipValidation->getdomain($ipaddress);
48 1
            $apiRes = $ipGeoModel->fetchData($ipaddress);
49
        }
50
51
        $data = [
52 2
            "isIpValid" => $isIpValid,
53 2
            "domain" => $domain ?? null,
54 2
            "apiRes" => $apiRes ?? null,
55
        ];
56
57
        // Deal with the action and return a response.
58 2
        return [$data];
59
    }
60
}
61