ValidAPIWeather   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 49
ccs 25
cts 25
cp 1
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
A missinginput() 0 6 4
A errormsg() 0 10 4
1
<?php
2
3
namespace artes\Weather;
4
5
/**
6
  * A class for validating weather parameters.
7
  *
8
  * @SuppressWarnings(PHPMD)
9
  */
10
class ValidAPIWeather extends ValidWeather
11
{
12
    private $latitud;
13
    private $longitud;
14
    private $userip;
15
    private $ip;
16
17
    /**
18
     * Constructor to initiate an ValidWeather object,
19
     *
20
     * @param string $userinput
21
     *
22
     */
23 6
    public function __construct(object $userinput, object $ip)
24
    {
25 6
        if ($userinput->getMethod() === "GET") {
26 2
            $this->latitud = $userinput->getGet("lat", null);
27 2
            $this->longitud = $userinput->getGet("lon", null);
28 2
            $this->userip = $userinput->getGet("ip", null);
29 5
        } elseif ($userinput->getMethod() === "POST") {
30 3
            $this->latitud = $userinput->getPost("latitud", null);
31 3
            $this->longitud = $userinput->getPost("longitud", null);
32 3
            $this->userip = $userinput->getPost("userip", null);
33
        } else {
34 3
            $this->latitud = "";
35 3
            $this->longitud = "";
36 3
            $this->userip = "";
37
        }
38 6
        $this->ip = $ip;
39 6
    }
40
41 6
    private function missinginput() : bool
42
    {
43 6
        if ($this->userip || ($this->latitud && $this->longitud)) {
44 4
            return false;
45
        }
46 4
        return true;
47
    }
48
49 6
    public function errormsg() : string
50
    {
51 6
        if ($this->missinginput()) {
52 4
            $msg = "Missing input. Try again";
53 4
        } elseif (!$this->ip->validip($this->userip) && !$this->validcoord($this->longitud, $this->latitud)) {
54 1
            $msg = "Invalid query parameters. Try again";
55
        } else {
56 3
            $msg = "";
57
        }
58 6
        return $msg;
59
    }
60
}
61