ValidAPIWeather::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
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 2
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 3
rs 9.8333
c 0
b 0
f 0
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