WeatherController::getInfoAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 0
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 3
rs 9.8333
c 0
b 0
f 0
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 WeatherController implements ContainerInjectableInterface
22
{
23
    use ContainerInjectableTrait;
24
25
    /**
26
     * @var string $db a sample member variable that gets initialised
27
     */
28
    private $db = "not active";
29
30
    /**
31
     * The initialize method is optional and will always be called before the
32
     * target method/action. This is a convienient method where you could
33
     * setup internal properties that are commonly used by several methods.
34
     *
35
     * @return void
36
     */
37 9
    public function initialize() : void
38
    {
39
        // Use to initialise member variables.
40 9
        $this->db = "active";
41 9
    }
42
43
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 object
52
     */
53 1
    public function indexAction() : object
54
    {
55
        // Deal with the action and return a response.
56 1
        $page = $this->di->get("page");
57
58 1
        $page->add("anax/v2/weather/index");
59 1
        return $page->render(
60
            [
61 1
                "title" => "Få väderinformation",
62
                "baseTitle" => " | Anax development utilities"
63
            ]
64
        );
65
    }
66
67
    /**
68
     * [getInfoAction description]
69
     *
70
     * @return object [description]
71
     */
72 5
    public function getInfoAction() : object
73
    {
74 5
        $request = $this->di->get("request");
75 5
        $page = $this->di->get("page");
76
77 5
        if ($request->getGet("data") == "json") {
78 1
            return $this->di->get("response")->redirect("weather/getjson?pos=" . $request->getGet("pos"));
79
        }
80 4
        $position = $request->getGet("pos");
81 4
        $ipm = $this->di->get("callurlmodel");
82 4
        if ($position) {
83 4
            $ipm->setIpAddress($position);
84 4
            $ipm->fetchGeoInfo();
85
        }
86
87 4
        $page->add("anax/v2/weather/weather-info", $ipm->fetchWeatherInfo());
88 4
        return $page->render(
89
            [
90 4
                "title" => "Få väderinformation",
91
                "baseTitle" => " | Anax development utilities"
92
            ]
93
        );
94
    }
95
96
97
98
    /**
99
     * This is the index method action, it handles:
100
     * GET METHOD mountpoint
101
     * GET METHOD mountpoint/
102
     * GET METHOD mountpoint/index
103
     *
104
     * @return array
105
     */
106 2
    public function getjsonActionGet() : array
107
    {
108 2
        $request = $this->di->get("request");
109 2
        $position = $request->getGet("pos");
110 2
        $ipm = $this->di->get("callurlmodel");
111 2
        $ipm->setIpAddress($position);
112 2
        $ipm->fetchGeoInfo();
113 2
        return [$ipm->fetchWeatherInfo()];
114
    }
115
116
117
118
    /**
119
     * This is the index method action, it handles:
120
     * GET METHOD mountpoint
121
     * GET METHOD mountpoint/
122
     * GET METHOD mountpoint/index
123
     *
124
     * @return array
125
     */
126 1
    public function getJsonActionPost() : array
127
    {
128 1
        $request = $this->di->get("request");
129 1
        $position = $request->getPost("pos");
130 1
        $ipm = $this->di->get("callurlmodel");
131 1
        $ipm->setIpAddress($position);
132 1
        $ipm->fetchGeoInfo();
133 1
        return [$ipm->fetchWeatherInfo()];
134
    }
135
}
136