WeatherJsonController::weatherjsonActionGet()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 60
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8.7479

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 35
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 60
ccs 19
cts 33
cp 0.5758
crap 8.7479
rs 8.7377

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Anax\Weather;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
use Anax\Curl\Curl as Curl;
8
9
// use Anax\Route\Exception\ForbiddenException;
10
// use Anax\Route\Exception\NotFoundException;
11
// use Anax\Route\Exception\InternalErrorException;
12
13
/**
14
 * A sample 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
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
21
 */
22
class WeatherJsonController implements ContainerInjectableInterface
23
{
24
    use ContainerInjectableTrait;
25
26
27 1
    public function indexAction() : object
28
    {
29
30 1
        $title = "Weather";
31
32 1
        $address = $_SERVER["REMOTE_ADDR"] ?? "No IP detected.";
33
34
35
36 1
        $page = $this->di->get("page");
37
38
        $data = [
39 1
            "address" => $address
40
        ];
41
42
43 1
        $page->add("weatherjson/verify", $data);
44
45 1
        return $page->render([
46 1
            "title" => $title,
47
        ]);
48
    }
49
50 1
    public function weatherjsonActionGet()
51
    {
52 1
        $request = $this->di->request;
0 ignored issues
show
Bug introduced by
Accessing request on the interface Psr\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
54 1
        $doVerify = $request->getGet("verify", null);
55 1
        $address = $request->getGet("ip", null);
56 1
        $latitude = $request->getGet("latitude", null);
57 1
        $longitude = $request->getGet("longitude", null);
58
59 1
        $check = $this->di->get("validator");
60
61 1
        $weather = new Curl();
62
63
64 1
        if ($doVerify && $address) {
65 1
            $result = $check->checkIpv($address);
66
67 1
            $geotag = $check->geoTag($address);
68
69
70 1
            $fetchWeather = $weather->weather($geotag->latitude, $geotag->longitude);
71 1
            $fetchPastWeather = $weather->pastWeather($geotag->latitude, $geotag->longitude);
72
73 1
            $weatherData = $fetchWeather->daily;
74
75 1
            $pastData = $fetchPastWeather;
76
77
            $data = [
78 1
                "valid" => $result,
79 1
                "week" => $weatherData,
80 1
                "past Month" => $pastData,
81
82
            ];
83
84 1
            return [$data];
85
        }
86
87
        if ($doVerify && $latitude) {
88
            $verifyCoord = $check->verifyGeo($latitude, $longitude);
89
90
            if ($verifyCoord) {
91
                $fetchWeather = $weather->weather($latitude, $longitude);
92
                $fetchPastWeather = $weather->pastWeather($latitude, $longitude);
93
94
                $weatherData = $fetchWeather->daily;
95
                $pastData = $fetchPastWeather;
96
97
                $valid = "The coordinates $latitude, $longitude are valid.";
98
99
                $data = [
100
                    "valid" => $valid,
101
                    "week" => $weatherData,
102
                    "past Month" => $pastData,
103
                ];
104
105
                return [$data];
106
            } else {
107
                $invalid = "The coordinates are invalid. Try again.";
108
109
                return [$invalid];
110
            }
111
        }
112
    }
113
}
114