Completed
Push — master ( c9fac3...5ce95a )
by Carsten
11:55 queued 11s
created

HttpClientLocationCreator::setErrorLoglevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Germania\ClientIpLocation;
3
4
use Psr\Http\Client\ClientInterface;
5
use Psr\Http\Client\ClientExceptionInterface;
6
use Psr\Http\Client\RequestExceptionInterface;
7
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\RequestFactoryInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
use Psr\Log\LoggerInterface;
13
use Psr\Log\NullLogger;
14
use Psr\Log\LogLevel;
15
use Psr\Log\LoggerAwareTrait;
16
17
class HttpClientLocationCreator
18
{
19 1
    use LoggerAwareTrait;
20
21
    /**
22
     * @var ClientInterface
23
     */
24
    protected $client;
25
26
27
    /**
28
     * @var RequestFactoryInterface
29
     */
30
    public $request_factory;
31
32
33
    /**
34
     * Geocoder API endpoint.
35
     *
36
     * @var string
37
     */
38
    public $api;
39
40
41
    /**
42
     * IP adress query parameter
43
     *
44
     * @var string
45
     */
46
    public $ip_var_name = "ip";
47
48
49
    /**
50
     * @var callable
51
     */
52
    public $response_decoder;
53
54
55
    /**
56
     * @var mixed
57
     */
58
    public $default_location;
59
60
61
    /**
62
     * @var string
63
     */
64
    protected $error_loglevel = LogLevel::ERROR;
65
66
67
    /**
68
     * @param string                  $api              Geocoder API endpoint
69
     * @param ClientInterface         $client           PSR-18 HTTP Client
70
     * @param RequestFactoryInterface $request_factory  PSR-17 Request factory
71
     * @param callable|null           $response_decoder Optional: PSR-7 Response decoder callable
72
     */
73 2
    public function __construct( string $api, ClientInterface $client, RequestFactoryInterface $request_factory, callable $response_decoder = null, LoggerInterface $logger = null)
74
    {
75 2
        $this->setApiEndpoint( $api )
76 2
             ->setClient($client)
77 2
             ->setRequestFactory($request_factory)
78 2
             ->setResponseDecoder($response_decoder)
79 2
             ->setLogger( $logger ?: new NullLogger);
80 2
    }
81
82
83
84
    /**
85
     * @param  string $client_ip Client IP address
86
     */
87 2
    public function __invoke( string $client_ip )
88
    {
89 2
        $request = $this->createRequest($client_ip);
90
91
        try {
92 2
            $response = $this->client->sendRequest( $request);
93 2
            return $this->decodeResponse($response);
94
        }
95
        catch (ClientExceptionInterface $e) {
96
            $msg = sprintf("HttpClientLocationCreator: %s", $e->getMessage());
97
            $msg_location = sprintf("%s:%s", $e->getFile(), $e->getLine());
98
            $this->logger->log( $this->error_loglevel, $msg, [
99
                'type' => get_class($e),
100
                'code' => $e->getCode(),
101
                'location' => $msg_location,
102
                'apiEndpoint' => $this->api,
103
                'clientIp' => $client_ip
104
            ]);
105
106
            return $this->default_location;
107
        }
108
    }
109
110
111
112
    /**
113
     * @param  string $client_ip Client IP address
114
     * @return RequestInterface
115 4
     */
116
    public function createRequest( string $client_ip ) : RequestInterface
117 4
    {
118 4
        $client_ip_urlencoded = urlencode($client_ip);
119
        $query_parameter_field = "{{" . $this->ip_var_name . "}}";
120 4
121
        $api = str_replace($query_parameter_field, $client_ip_urlencoded, $this->api);
122 4
123
        return $this->request_factory->createRequest("GET", $api);
124
    }
125
126
127
128
    /**
129
     * @param  ResponseInterface $response
130
     * @return mixed
131 4
     */
132
    public function decodeResponse(ResponseInterface $response)
133 4
    {
134 4
        return $this->response_decoder
135 4
        ? ($this->response_decoder)($response)
136
        : json_decode($response->getBody(), "assoc");
137
    }
138
139
140
141
    /**
142
     * Sets the default location to return on error
143
     *
144
     * @param mixed $location
145 2
     */
146
    public function setDefaultLocation( $location ) : self
147 2
    {
148 2
        $this->default_location = $location;
149
        return $this;
150
    }
151
152
153
    /**
154
     * @param string $error_loglevel PSR-3 Loglevel name
155
     */
156
    public function setErrorLoglevel( string $error_loglevel ) {
157
        $this->error_loglevel = $error_loglevel;
158 4
        return $this;
159
    }
160 4
161 4
162
    /**
163
     * Sets the API endpoint
164
     *
165
     * @param string $api
166
     */
167
    public function setApiEndpoint( string $api ) : self
168
    {
169
        $this->api = $api;
170
        return $this;
171 6
    }
172
173 6
174 6
175
    /**
176
     * Sets the HTTP Client to use.
177
     *
178
     * @param ClientInterface $client
179
     */
180
    public function setClient( ClientInterface $client ) : self
181
    {
182
        $this->client = $client;
183 4
        return $this;
184
    }
185 4
186 4
187
    /**
188
     * Sets the PSR-17 Request factory
189
     *
190
     * @param RequestFactoryInterface $request_factory
191
     */
192
    public function setRequestFactory( RequestFactoryInterface $request_factory ) : self
193
    {
194
        $this->request_factory = $request_factory;
195 4
        return $this;
196
    }
197 4
198 4
199
    /**
200
     * Sets the PSR-7 Response decoder callable.
201
     *
202
     * @param callable $response_decoder
203
     */
204
    public function setResponseDecoder( callable $response_decoder = null ) : self
205
    {
206
        $this->response_decoder = $response_decoder;
207
        return $this;
208
    }
209
210
}
211