AbstractProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 70
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A buildConfiguration() 0 10 1
A callApi() 0 25 3
getEndpointPath() 0 1 ?
1
<?php
2
3
/*
4
 * This file is part of the geo-api-library package.
5
 *
6
 * (c) 2020 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\GeoAPI\Provider;
13
14
use Exception;
15
use GuzzleHttp\Client;
16
use GuzzleHttp\Exception\GuzzleException;
17
use Psr\Log\LoggerInterface;
18
use WBW\Library\Core\Exception\ApiException;
19
use WBW\Library\Core\Provider\AbstractProvider as BaseProvider;
20
use WBW\Library\GeoAPI\Request\AbstractRequest;
21
22
/**
23
 * Abstract provider.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Library\GeoAPI\Provider
27
 * @abstract
28
 */
29
abstract class AbstractProvider extends BaseProvider {
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param LoggerInterface|null $logger The logger.
35
     */
36 65
    public function __construct(LoggerInterface $logger = null) {
37 65
        parent::__construct($logger);
38 65
    }
39
40
    /**
41
     * Build the configuration.
42
     *
43
     * @return array Returns the configuration.
44
     */
45 50
    private function buildConfiguration(): array {
46
        return [
47 50
            "base_uri"    => $this->getEndpointPath() . "/",
48 50
            "debug"       => $this->getDebug(),
49
            "headers"     => [
50
                "User-Agent" => "webeweb/geo-api-library",
51
            ],
52
            "synchronous" => true,
53
        ];
54
    }
55
56
    /**
57
     * Call the API.
58
     *
59
     * @param AbstractRequest $request The request.
60
     * @param array $queryData The query data.
61
     * @param array $postData The post data.
62
     * @return string Returns the raw response.
63
     * @throws GuzzleException Throws a Guzzle exception if an error occurs.
64
     * @throws ApiException Throws an API exception if an error occurs.
65
     */
66 50
    protected function callApi(AbstractRequest $request, array $queryData, array $postData = []): string {
67
68
        try {
69
70 50
            $config = $this->buildConfiguration();
71
72 50
            $client = new Client($config);
73
74 50
            $method  = 0 === count($postData) ? "GET" : "POST";
75 50
            $uri     = substr($request->getResourcePath(), 1);
76 10
            $options = [
77 50
                "query"     => $queryData,
78 50
                "multipart" => $postData,
79
            ];
80
81 50
            $this->logInfo(sprintf("Call API provider %s %s", $method, $uri), ["config" => $config]);
82
83 50
            $response = $client->request($method, $uri, $options);
84
85 50
            return $response->getBody()->getContents();
86
        } catch (Exception $ex) {
87
88
            throw new ApiException(sprintf("Call API provider failed"), 500, $ex);
89
        }
90
    }
91
92
    /**
93
     * Get the endpoint path.
94
     *
95
     * @return string Returns the endpoint path.
96
     */
97
    abstract public function getEndpointPath(): string;
98
}