Weather::processRequest()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 9
nop 4
dl 0
loc 32
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace H4MSK1\Weather;
4
5
use H4MSK1\Curl\Api;
6
use H4MSK1\Map\OpenLayers;
7
8
class Weather
9
{
10
    public function processRequest($iplocation, $coords, $type, Api $curl)
11
    {
12
        $payload = ['weather' => [], 'map' => null];
13
        $lat = 0;
14
        $lng = 0;
15
16
        if (empty($coords) && $iplocation['ip'] === 'Invalid') {
17
            $payload['error'] = 'Missing input data';
18
            return $payload;
19
        }
20
21
        if (empty($coords)) {
22
            if (isset($iplocation['latitude']) && isset($iplocation['longitude'])) {
23
                $lat = $iplocation['latitude'];
24
                $lng = $iplocation['longitude'];
25
            } else {
26
                $payload['error'] = 'Ip not valid';
27
            }
28
        } else {
29
            if (strpos($coords, ',') !== false) {
30
                list($lat, $lng) = explode(',', $coords);
31
            } else {
32
                $payload['error'] = 'Coordinates not valid';
33
            }
34
        }
35
36
        if (! isset($payload['error'])) {
37
            $payload['weather'] = $curl->getWeatherData($lat, $lng, $type);
38
            $payload['map'] = (new OpenLayers)->getHtml($lat, $lng);
39
        }
40
41
        return $payload;
42
    }
43
}
44