Completed
Push — master ( 2546e0...ad929d )
by Alexey
12:49 queued 02:29
created

DaData::getLocationByResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 28
rs 9.6666
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\LocationInterface;
9
use CodeblogPro\GeoLocationAddress\Country;
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 DaData 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
            ]
27
        );
28
    }
29
30
    protected function getLocationByResponse(
31
        ResponseInterface $response,
32
        LanguageInterface $language
33
    ): LocationInterface {
34
        $responseContentJson = $response->getBody()->getContents();
35
        $responseContent = $this->getContentFromJson($responseContentJson);
36
        $coordinates = null;
37
38
        if (isset($responseContent->location->data->geo_lat) && isset($responseContent->location->data->geo_lon)) {
39
            $coordinates = new Coordinates(
40
                (float)$responseContent->location->data->geo_lat,
41
                (float)$responseContent->location->data->geo_lon
42
            );
43
        }
44
45
        return new Location(
46
            $coordinates,
47
            new Country(
48
                $responseContent->location->data->country ?? '',
49
                $responseContent->location->data->country_iso_code ?? ''
50
            ),
51
            new Region(
52
                $responseContent->location->data->region_with_type ?? '',
53
                $responseContent->location->data->region_iso_code ?? ''
54
            ),
55
            $responseContent->location->data->city ?? '',
56
            '',
57
            $responseContent->location->data->postal_code ?? '',
58
        );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
59
    }
60
}
61