Completed
Push — master ( cac190...51e389 )
by WEBEWEB
01:21
created

ApiProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 7
dl 0
loc 58
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 WBW\Library\Core\Exception\ApiException;
16
use WBW\Library\Core\Network\CURL\Configuration\CurlConfiguration;
17
use WBW\Library\Core\Network\CURL\Request\CurlPostRequest;
18
use WBW\Library\Core\Provider\AbstractProvider;
19
use WBW\Library\Core\ThirdParty\Adoria\API\ApiProviderInterface;
20
use WBW\Library\Core\ThirdParty\Adoria\Model\RequestData;
21
use WBW\Library\Core\ThirdParty\Adoria\Model\Result;
22
use WBW\Library\Core\ThirdParty\Adoria\Serializer\RequestSerializer;
23
use WBW\Library\Core\ThirdParty\Adoria\Serializer\ResponseDeserializer;
24
25
/**
26
 * API provider.
27
 *
28
 * @author webeweb <https://github.com/webeweb/>
29
 * @package WBW\Library\Core\ThirdParty\Adoria\Provider
30
 */
31
class ApiProvider extends AbstractProvider implements ApiProviderInterface {
32
33
    /**
34
     * Constructor.
35
     */
36
    public function __construct() {
37
        $this->setDebug(false);
38
    }
39
40
    /**
41
     * Call API.
42
     *
43
     * @param string $resourcePath The resource path.
44
     * @param array $postData The post data.
45
     * @return string Returns the response.
46
     * @throws ApiException Throws an API exception exception if an error occurs.
47
     */
48
    protected function callAPI($resourcePath, array $postData) {
49
50
        try {
51
52
            $cURLRequest = new CurlPostRequest(new CurlConfiguration(), $resourcePath);
53
            $cURLRequest->getConfiguration()->addHeader("Accept", "application/json");
54
            $cURLRequest->getConfiguration()->addHeader("Content-Type", "application/json");
55
            $cURLRequest->getConfiguration()->setDebug($this->getDebug());
56
            $cURLRequest->getConfiguration()->setHost(self::ENDPOINT_PATH);
57
            $cURLRequest->getConfiguration()->setUserAgent("webeweb/adoria-library");
58
59
            // Handle each parameter.
60
            foreach ($postData as $name => $value) {
61
                $cURLRequest->addPostData($name, $value);
62
            }
63
64
            $cURLResponse = $cURLRequest->call();
65
66
            return $cURLResponse->getResponseBody();
67
        } catch (Exception $ex) {
68
69
            throw new ApiException("Failed to call Adoria API", $ex);
0 ignored issues
show
Documentation introduced by
$ex is of type object<Exception>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
        }
71
    }
72
73
    /**
74
     * Request data.
75
     *
76
     * @param RequestData $requestData The request data.
77
     * @return Result Returns the result.
78
     * @throws ApiException Throws an API exception exception if an error occurs.
79
     */
80
    public function requestData(RequestData $requestData) {
81
82
        $parameters = RequestSerializer::serializeRequestData($requestData);
83
84
        $rawResponse = $this->callAPI(self::REQUEST_DATA_RESOURCE_PATH, $parameters);
85
86
        return ResponseDeserializer::deserializeResult($rawResponse);
87
    }
88
}
89