MaxMind::getLocationByResponse()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 22
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 34
ccs 0
cts 23
cp 0
crap 20
rs 9.568
1
<?php
2
3
namespace CodeblogPro\GeoLocation\Application\GeoIpRemoteServices;
4
5
use CodeblogPro\GeoLocation\Application\Interfaces\IpAddressInterface;
6
use CodeblogPro\GeoLocation\Application\Interfaces\LanguageInterface;
7
use CodeblogPro\GeoCoordinates\Coordinates;
8
use CodeblogPro\GeoLocationAddress\Country;
9
use CodeblogPro\GeoLocationAddress\LocationInterface;
10
use CodeblogPro\GeoLocationAddress\Location;
11
use CodeblogPro\GeoLocationAddress\Region;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use GuzzleHttp;
15
16
class MaxMind extends TemplateOfWorkingWithRemoteServiceApi
17
{
18
    protected function prepareRequest(IpAddressInterface $ipAddress): RequestInterface
19
    {
20
        return new GuzzleHttp\Psr7\Request(
21
            'GET',
22
            $this->options->getUrl() . $ipAddress->getValue(),
23
            [
24
                'Authorization' => $this->options->getKey(),
25
                'Accept' => 'application/json',
26
                'Content-Type' => 'application/json',
27
            ]
28
        );
29
    }
30
31
    protected function getLocationByResponse(
32
        ResponseInterface $response,
33
        LanguageInterface $language
34
    ): LocationInterface {
35
        $responseContentJson = $response->getBody()->getContents();
36
        $responseContent = $this->getContentFromJson($responseContentJson);
37
        $coordinates = null;
38
39
        if (isset($responseContent->location->latitude) && isset($responseContent->location->longitude)) {
40
            $coordinates = new Coordinates(
41
                (float)$responseContent->location->latitude,
42
                (float)$responseContent->location->longitude
43
            );
44
        }
45
46
        $languagePostfix = $language->getCode();
47
        $firstSubdivision = (empty($responseContent->subdivisions)) ? [] : current($responseContent->subdivisions);
48
49
        return new Location(
50
            $coordinates,
51
            new Country(
52
                $responseContent->country->names->$languagePostfix ?? '',
53
                $responseContent->country->iso_code ?? ''
54
            ),
55
            new Region(
56
                $firstSubdivision->names->$languagePostfix ?? '',
57
                $this->getRegionIsoCodeByCountryIsoCodeAndRegionCode(
58
                    $responseContent->country->iso_code ?? '',
59
                    $firstSubdivision->iso_code ?? ''
60
                )
61
            ),
62
            $responseContent->city->names->$languagePostfix ?? '',
63
            '',
64
            $responseContent->postal->code ?? ''
65
        );
66
    }
67
68
    private function getRegionIsoCodeByCountryIsoCodeAndRegionCode(
69
        string $countryIsoCode = '',
70
        string $regionCode = ''
71
    ): string {
72
        $regionIsoCode = $countryIsoCode;
73
74
        if (!empty($regionIsoCode)) {
75
            $regionIsoCode .= '-';
76
        }
77
78
        $regionIsoCode .= $regionCode;
79
80
        return $regionIsoCode;
81
    }
82
}
83