ApiProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 7
dl 0
loc 60
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A callAPI() 0 24 3
A requestData() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2019 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\Core\ThirdParty\Adoria\Provider;
13
14
use Exception;
15
use Psr\Log\LoggerInterface;
16
use WBW\Library\Core\Exception\ApiException;
17
use WBW\Library\Core\Network\CURL\Configuration\CurlConfiguration;
18
use WBW\Library\Core\Network\CURL\Request\CurlPostRequest;
19
use WBW\Library\Core\Provider\AbstractProvider;
20
use WBW\Library\Core\ThirdParty\Adoria\API\ApiProviderInterface;
21
use WBW\Library\Core\ThirdParty\Adoria\Model\RequestData;
22
use WBW\Library\Core\ThirdParty\Adoria\Model\Result;
23
use WBW\Library\Core\ThirdParty\Adoria\Serializer\RequestSerializer;
24
use WBW\Library\Core\ThirdParty\Adoria\Serializer\ResponseDeserializer;
25
26
/**
27
 * API provider.
28
 *
29
 * @author webeweb <https://github.com/webeweb/>
30
 * @package WBW\Library\Core\ThirdParty\Adoria\Provider
31
 */
32
class ApiProvider extends AbstractProvider implements ApiProviderInterface {
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param LoggerInterface|null $logger The logger.
38
     */
39
    public function __construct(LoggerInterface $logger = null) {
40
        parent::__construct($logger);
41
    }
42
43
    /**
44
     * Call API.
45
     *
46
     * @param string $resourcePath The resource path.
47
     * @param array $postData The post data.
48
     * @return string Returns the response.
49
     * @throws ApiException Throws an API exception exception if an error occurs.
50
     */
51
    protected function callAPI(string $resourcePath, array $postData): string {
52
53
        try {
54
55
            $cURLRequest = new CurlPostRequest(new CurlConfiguration(), $resourcePath);
56
            $cURLRequest->getConfiguration()->addHeader("Accept", "application/json");
57
            $cURLRequest->getConfiguration()->addHeader("Content-Type", "application/json");
58
            $cURLRequest->getConfiguration()->setDebug($this->getDebug());
59
            $cURLRequest->getConfiguration()->setHost(self::ENDPOINT_PATH);
60
            $cURLRequest->getConfiguration()->setUserAgent("webeweb/adoria-library");
61
62
            // Handle each parameter.
63
            foreach ($postData as $name => $value) {
64
                $cURLRequest->addPostData($name, $value);
65
            }
66
67
            $cURLResponse = $cURLRequest->call();
68
69
            return $cURLResponse->getResponseBody();
70
        } catch (Exception $ex) {
71
72
            throw new ApiException("Failed to call Adoria API", 500, $ex);
73
        }
74
    }
75
76
    /**
77
     * Request data.
78
     *
79
     * @param RequestData $requestData The request data.
80
     * @return Result Returns the result.
81
     * @throws ApiException Throws an API exception exception if an error occurs.
82
     */
83
    public function requestData(RequestData $requestData): Result {
84
85
        $parameters = RequestSerializer::serializeRequestData($requestData);
86
87
        $rawResponse = $this->callAPI(self::REQUEST_DATA_RESOURCE_PATH, $parameters);
88
89
        return ResponseDeserializer::deserializeResult($rawResponse);
90
    }
91
}
92