Coordinates   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 62.07%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 63
ccs 18
cts 29
cp 0.6207
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getLongitude() 0 3 1
A getLatitude() 0 3 1
A toString() 0 3 1
A toArray() 0 3 1
A setLatitudeIsValid() 0 15 5
A setLongitudeIsValid() 0 15 5
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CodeblogPro\GeoCoordinates;
6
7
use CodeblogPro\GeoCoordinates\Exceptions\InvalidArgumentException;
8
9
class Coordinates implements CoordinatesInterface
10
{
11
    private float $latitude;
12
    private float $longitude;
13
14 5
    public function __construct($latitude, $longitude)
15
    {
16 5
        $this->setLatitudeIsValid($latitude);
17 3
        $this->setLongitudeIsValid($longitude);
18 3
    }
19
20
    public function getLatitude(): float
21
    {
22
        return $this->latitude;
23
    }
24
25
    public function getLongitude(): float
26
    {
27
        return $this->longitude;
28
    }
29
30
    public function toArray(): array
31
    {
32
        return [$this->getLongitude(), $this->getLatitude()];
33
    }
34
35
    public function toString(): string
36
    {
37
        return $this->getLongitude() . ', ' . $this->getLatitude();
38
    }
39
40 5
    private function setLatitudeIsValid($latitude): void
41
    {
42 5
        $isValid = true;
43
44 5
        if (!is_numeric($latitude)) {
45
            $isValid = false;
46
        }
47
48 5
        $latitude = (float)$latitude;
49
50 5
        if (!$isValid || $latitude < -90 || $latitude > 90) {
51 2
            throw new InvalidArgumentException('Invalid latitude. It must be a number between -90 and 90');
52
        }
53
54 3
        $this->latitude = $latitude;
55 3
    }
56
57 3
    private function setLongitudeIsValid($longitude): void
58
    {
59 3
        $isValid = true;
60
61 3
        if (!is_numeric($longitude)) {
62
            $isValid = false;
63
        }
64
65 3
        $longitude = (float)$longitude;
66
67 3
        if (!$isValid || $longitude < -180 || $longitude > 180) {
68
            throw new InvalidArgumentException('Invalid longitude. It must be a number between -90 and 90');
69
        }
70
71 3
        $this->longitude = $longitude;
72 3
    }
73
}
74