GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

IpGeolocalizationHelper   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 50
dl 0
loc 139
rs 10
c 3
b 1
f 0
wmc 20

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getContinentCode() 0 14 3
A getCountryCode() 0 14 3
A getCityName() 0 14 3
A __construct() 0 8 1
A getPostalCode() 0 14 3
A getCityRecord() 0 10 3
A getGeoParameter() 0 9 2
A setGeoParameter() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusGeoPlugin\Helper;
6
7
use GeoIp2\Database\Reader;
8
use GeoIp2\Exception\AddressNotFoundException;
9
use GeoIp2\Model\City;
10
use MaxMind\Db\Reader\InvalidDatabaseException;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\RequestStack;
13
use Symfony\Component\HttpFoundation\Session\Session;
14
15
final class IpGeolocalizationHelper implements IpGeolocalizationHelperInterface
16
{
17
    /** @var RequestStack */
18
    private $requestStack;
19
20
    /** @var Session */
21
    private $session;
22
23
    /** @var Reader */
24
    private $reader;
25
26
    public function __construct(
27
        RequestStack $requestStack,
28
        Session $session,
29
        Reader $reader
30
    ) {
31
        $this->requestStack = $requestStack;
32
        $this->session = $session;
33
        $this->reader = $reader;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getContinentCode(): ?string
40
    {
41
        $continentCode = $this->getGeoParameter('continentCode');
42
        if (null === $continentCode) {
43
            try {
44
                $continentCode = $this->getCityRecord()->continent->code;
45
            } catch (\Exception $e) {
46
                $continentCode = null;
47
            }
48
49
            $this->setGeoParameter('continentCode', $continentCode);
50
        }
51
52
        return $continentCode;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getCountryCode(): ?string
59
    {
60
        $countryCode = $this->getGeoParameter('countryCode');
61
        if (null === $countryCode) {
62
            try {
63
                $countryCode = $this->getCityRecord()->country->isoCode;
64
            } catch (\Exception $e) {
65
                $countryCode = null;
66
            }
67
68
            $this->setGeoParameter('countryCode', $countryCode);
69
        }
70
71
        return $countryCode;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getCityName(): ?string
78
    {
79
        $cityName = $this->getGeoParameter('cityName');
80
        if (null === $cityName) {
81
            try {
82
                $cityName = $this->getCityRecord()->city->name;
83
            } catch (\Exception $e) {
84
                $cityName = null;
85
            }
86
87
            $this->setGeoParameter('cityName', $cityName);
88
        }
89
90
        return $cityName;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getPostalCode(): ?string
97
    {
98
        $postalCode = $this->getGeoParameter('postalCode');
99
        if (null === $postalCode) {
100
            try {
101
                $postalCode = $this->getCityRecord()->postal->code;
102
            } catch (\Exception $e) {
103
                $postalCode = null;
104
            }
105
106
            $this->setGeoParameter('postalCode', $postalCode);
107
        }
108
109
        return $postalCode;
110
    }
111
112
    /**
113
     * @return City
114
     * @throws AddressNotFoundException
115
     * @throws InvalidDatabaseException
116
     */
117
    private function getCityRecord(): City
118
    {
119
        /** @var Request $request */
120
        $request = $this->requestStack->getMasterRequest();
121
122
        $ip = $request->getClientIp() === '127.0.0.1' ? '110.33.122.75' : $request->getClientIp();
123
124
        $ip = $ip === null ? '' : $ip;
125
126
        return $this->reader->city($ip);
127
    }
128
129
    /**
130
     * @param string $parameter
131
     * @return string|null
132
     */
133
    private function getGeoParameter(string $parameter): ?string
134
    {
135
        $sessionParameter = '_geo_'.$parameter;
136
137
        if ($this->session->has($sessionParameter)) {
138
            return $this->session->get($sessionParameter);
139
        }
140
141
        return null;
142
    }
143
144
    /**
145
     * @param string $parameter
146
     * @param string|null $geo
147
     */
148
    private function setGeoParameter(string $parameter, ?string $geo): void
149
    {
150
        $sessionParameter = '_geo_'.$parameter;
151
152
        if (null !== $geo) {
153
            $this->session->set($sessionParameter, $geo);
154
        }
155
    }
156
}
157