Coordinates   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
eloc 10
c 2
b 1
f 0
dl 0
loc 36
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getCoordinates() 0 12 3
1
<?php
2
3
namespace Linder\Model;
4
5
use OpenCage\Geocoder\AbstractGeocoder;
6
7
class Coordinates
8
{
9
    private $geocoder;
10
11
    /**
12
     * Constructor, allow for $geocoder to be injected.
13
     *
14
     * @param AbstractGeocoder $geocoder a service that fetches coordinates from a query
15
     *
16
     * @return string "latitude,longitude" for example 56.0505,124.100101
17
     */
18 2
    public function __construct(AbstractGeocoder $geocoder)
19
    {
20 2
        $this->geocoder = $geocoder;
21 2
    }
22
23
    /**
24
     * This method checks if a input is a valid lat long
25
     * If not it does a search on it and returns the answer.
26
     *
27
     * @param string $value
28
     *
29
     * @return string
30
     */
31 2
    public function getCoordinates(String $search) : String
32
    {
33 2
        $valid = preg_match('/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?),[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/', $search);
34 2
        if (!$valid) {
35 2
            $result = $this->geocoder->geocode($search);
36 2
            if ($result["status"]["code"] == 400) {
37 1
                return "";
38
            }
39 2
            $search = $result["results"][0]["geometry"]["lat"] . "," . $result["results"][0]["geometry"]["lng"];
40
        }
41
42 2
        return $search;
43
    }
44
}
45