HistoricalWeatherCheckRestController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 82
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A dumpDiActionGet() 0 9 1
A initialize() 0 4 1
A forbiddenAction() 0 7 1
A indexActionGet() 0 5 1
A indexActionPost() 0 10 1
1
<?php
2
3
namespace Anax\Controller;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Ipstack\Ipstack;
8
9
// use Anax\Route\Exception\ForbiddenException;
10
// use Anax\Route\Exception\NotFoundException;
11
// use Anax\Route\Exception\InternalErrorException;
12
13
/**
14
 * A sample JSON controller to show how a controller class can be implemented.
15
 * The controller will be injected with $di if implementing the interface
16
 * ContainerInjectableInterface, like this sample class does.
17
 * The controller is mounted on a particular route and can then handle all
18
 * requests for that mount point.
19
 */
20
class HistoricalWeatherCheckRestController implements ContainerInjectableInterface
21
{
22
    use ContainerInjectableTrait;
23
24
25
26
    /**
27
     * @var string $db a sample member variable that gets initialised
28
     */
29
    private $db = "not active";
30
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 4
    public function initialize() : void
40
    {
41
        // Use to initialise member variables.
42 4
        $this->db = "active";
43 4
    }
44
45
    /**
46
     * This is the index method action, it handles:
47
     * GET METHOD mountpoint
48
     * GET METHOD mountpoint/
49
     * GET METHOD mountpoint/index
50
     *
51
     * @return array
52
     */
53 1
    public function indexActionGet() : object
54
    {
55 1
        return $this->di->get("page")
56 1
            ->add("historical_weather_check_rest")
57 1
            ->render(["title" => "Weather Check"]);
58
    }
59
    
60 1
    public function indexActionPost()
61
    {
62 1
        $ip = $this->di->get("request")->getPost("ip") ?? null;
63 1
        $lat = $this->di->get("request")->getPost("lat") ?? null;
64 1
        $lon = $this->di->get("request")->getPost("lon") ?? null;
65 1
        $service = $this->di->get("weatherservice");
66
        $data = [
67 1
            "content" => $service->getWeatherThroughMultiCurl($ip, $lat, $lon),
68
        ];
69 1
        return [$data];
70
    }
71
72
    /**
73
     * This sample method dumps the content of $di.
74
     * GET mountpoint/dump-app
75
     *
76
     * @return array
77
     */
78 1
    public function dumpDiActionGet() : array
79
    {
80
        // Deal with the action and return a response.
81 1
        $services = implode(", ", $this->di->getServices());
82
        $json = [
83 1
            "message" => __METHOD__ . "<p>\$di contains: $services",
84 1
            "di" => $this->di->getServices(),
85
        ];
86 1
        return [$json];
87
    }
88
89
    /**
90
     * Try to access a forbidden resource.
91
     * ANY mountpoint/forbidden
92
     *
93
     * @return array
94
     */
95 1
    public function forbiddenAction() : array
96
    {
97
        // Deal with the action and return a response.
98
        $json = [
99 1
            "message" => __METHOD__ . ", forbidden to access.",
100
        ];
101 1
        return [$json, 403];
102
    }
103
}
104