Completed
Push — profiles-entity ( 44ff50...635457 )
by jelmer
21:26 queued 14:01
created

Geolocation::getCoordinates()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 16
nop 5
dl 0
loc 27
ccs 0
cts 24
cp 0
crap 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace ForkCMS\Utility;
4
5
use Backend\Core\Language\Language;
6
use Common\ModulesSettings;
7
use JeroenDesloovere\Geolocation\Geolocation as API;
8
use JeroenDesloovere\Geolocation\Result\Coordinates;
9
use JeroenDesloovere\Geolocation\Exception;
10
use Symfony\Component\Intl\Intl;
11
12
class Geolocation
13
{
14
    /** @var API */
15
    private $api;
16
17
    public function __construct(ModulesSettings $settings)
18
    {
19
        $this->api = new API($settings->get('Core', 'google_maps_key'));
20
    }
21
22
    /**
23
     * @param string|null $street
24
     * @param string|null $streetNumber
25
     * @param string|null $city
26
     * @param string|null $zip
27
     * @param string|null $country
28
     * @return array - Example: ['latitude' => 50.8864, 'longitude' => 3.42928]
29
     */
30
    public function getCoordinates(
31
        string $street = null,
32
        string $streetNumber = null,
33
        string $city = null,
34
        string $zip = null,
35
        string $country = null
36
    ): array {
37
        if (!empty($country)) {
38
            $country = Intl::getRegionBundle()->getCountryName($country, Language::getInterfaceLanguage());
39
        }
40
41
        try {
42
            /** @var Coordinates $coordinates */
43
            $coordinates = $this->api->getCoordinates(
44
                $street,
45
                $streetNumber,
46
                $city,
47
                $zip,
48
                $country
49
            );
50
        } catch (Exception $e) {
51
            $coordinates = null;
52
        }
53
54
        return [
55
            'latitude' => ($coordinates instanceof Coordinates) ? $coordinates->getLatitude() : null,
56
            'longitude' => ($coordinates instanceof Coordinates) ? $coordinates->getLongitude() : null,
57
        ];
58
    }
59
}
60