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

SypexGeo::getLocationByResponse()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 27
rs 8.8333
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\Location;
10
use CodeblogPro\GeoLocationAddress\LocationInterface;
11
use CodeblogPro\GeoLocationAddress\Region;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use GuzzleHttp;
15
16
class SypexGeo 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
                'Accept' => 'application/json'
25
            ]
26
        );
27
    }
28
29
    protected function getLocationByResponse(
30
        ResponseInterface $response,
31
        LanguageInterface $language
32
    ): LocationInterface {
33
        $responseContentJson = $response->getBody()->getContents();
34
        $responseContent = $this->getContentFromJson($responseContentJson);
35
        $coordinates = null;
36
37
        if ((isset($responseContent->city->lat) && isset($responseContent->city->lon))
38
            || (isset($responseContent->region->lat) && isset($responseContent->region->lon))
39
            || (isset($responseContent->country->lat) && isset($responseContent->country->lon))
40
        ) {
41
            $coordinates = new Coordinates(
42
                (float)($responseContent->city->lat ?? $responseContent->region->lat ?? $responseContent->country->lat),
43
                (float)($responseContent->city->lon ?? $responseContent->region->lon ?? $responseContent->country->lon),
44
            );
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...
45
        }
46
47
        $languagePostfix = 'name_' . $language->getCode();
48
49
        return new Location(
50
            $coordinates,
51
            new Country($responseContent->country->$languagePostfix ?? '', $responseContent->country->iso ?? ''),
52
            new Region($responseContent->region->$languagePostfix ?? '', $responseContent->region->iso ?? ''),
53
            $responseContent->city->$languagePostfix ?? '',
54
            '',
55
            $responseContent->city->post ?? '',
56
        );
57
    }
58
}
59