Issues (18)

src/Application/Services/GeoLocationService.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace CodeblogPro\GeoLocation\Application\Services;
4
5
use CodeblogPro\GeoLocation\Application\GeoIpRemoteServices\GeoIpRemoteServices;
6
use CodeblogPro\GeoLocation\Application\Interfaces\CurrentIpResolverInterface;
7
use CodeblogPro\GeoLocation\Application\Interfaces\GeoLocationServiceInterface;
8
use CodeblogPro\GeoLocation\Application\Models\IpAddress;
9
use CodeblogPro\GeoLocation\Application\Models\Language;
10
use CodeblogPro\GeoLocation\Application\Exceptions\GeoLocationAppException;
11
use CodeblogPro\GeoLocationAddress\LocationInterface;
12
13
class GeoLocationService implements GeoLocationServiceInterface
14
{
15
    public function __construct()
16
    {
17
    }
18
19
    public function getLocationByIpResolverAndLanguageResultCode(
20
        CurrentIpResolverInterface $currentIpResolver,
21
        string $languageResultCode
22
    ): LocationInterface {
23
        return $this->getLocationByIpAndLanguageResultCode(
24
            $currentIpResolver->getCurrentIp()->getValue(),
25
            $languageResultCode
26
        );
27
    }
28
29
    public function getLocationArrayByIpAndLanguageResultCode(string $ip, string $languageResultCode = ''): array
30
    {
31
        return $this->getLocationByIpAndLanguageResultCode($ip, $languageResultCode)->toArray();
32
    }
33
34
    public function getLocationByIpAndLanguageResultCode(string $ip, string $languageResultCode): LocationInterface
35
    {
36
        $ipAddress = new IpAddress($ip);
37
        $language = new Language($languageResultCode);
38
39
        $sortedServices = (GeoIpRemoteServices::getInstance())->getSortedServices();
40
        $location = $exception = null;
41
42
        foreach ($sortedServices as $service) {
43
            try {
44
                $location = $service->getLocation($ipAddress, $language);
45
                if ($location instanceof LocationInterface) {
46
                    break;
47
                }
48
            } catch (\Exception $exception) {
49
                continue;
50
            }
51
        }
52
53
        if (!isset($location)) {
54
            throw new GeoLocationAppException(
55
                'An error occurred while executing the program. ' . $exception->getMessage()
0 ignored issues
show
The method getMessage() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
                'An error occurred while executing the program. ' . $exception->/** @scrutinizer ignore-call */ getMessage()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            );
57
        }
58
59
        return $location;
60
    }
61
62
    public function getCurrentIpByIpResolver(CurrentIpResolverInterface $currentIpResolver): IpAddress
63
    {
64
        return $currentIpResolver->getCurrentIp();
65
    }
66
}
67