Completed
Push — master ( 362a13...64ee51 )
by WEBEWEB
01:29
created

ApiProvider::getDebug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\Network\CURL\Configuration\CurlConfiguration;
16
use WBW\Library\Core\Network\CURL\Request\CurlPostRequest;
17
use WBW\Library\Core\ThirdParty\Adoria\API\ApiProviderInterface;
18
use WBW\Library\Core\ThirdParty\Adoria\Exception\ApiException;
19
use WBW\Library\Core\ThirdParty\Adoria\Model\RequestData;
20
use WBW\Library\Core\ThirdParty\Adoria\Model\Result;
21
use WBW\Library\Core\ThirdParty\Adoria\Normalizer\RequestNormalizer;
22
use WBW\Library\Core\ThirdParty\Adoria\Normalizer\ResponseNormalizer;
23
24
/**
25
 * API provider.
26
 *
27
 * @author webeweb <https://github.com/webeweb/>
28
 * @package WBW\Library\Core\ThirdParty\Adoria\Provider
29
 */
30
class ApiProvider implements ApiProviderInterface {
31
32
    /**
33
     * Debug.
34
     *
35
     * @var bool
36
     */
37
    private $debug;
38
39
    /**
40
     * Constructor.
41
     */
42
    public function __construct() {
43
        $this->setDebug(false);
44
    }
45
46
    /**
47
     * Call API.
48
     *
49
     * @param string $resourcePath The resource path.
50
     * @param array $postData The post data.
51
     * @return string Returns the response.
52
     * @throws ApiException Throws an API exception exception if an error occurs.
53
     */
54
    protected function callAPI($resourcePath, array $postData) {
55
56
        try {
57
58
            $cURLRequest = new CurlPostRequest(new CurlConfiguration(), $resourcePath);
59
            $cURLRequest->getConfiguration()->addHeader("Accept", "application/json");
60
            $cURLRequest->getConfiguration()->addHeader("Content-Type", "application/json");
61
            $cURLRequest->getConfiguration()->setDebug($this->getDebug());
62
            $cURLRequest->getConfiguration()->setHost(self::ENDPOINT_PATH);
63
            $cURLRequest->getConfiguration()->setUserAgent("webeweb/adoria-library");
64
65
            // Handle each parameter.
66
            foreach ($postData as $name => $value) {
67
                $cURLRequest->addPostData($name, $value);
68
            }
69
70
            $cURLResponse = $cURLRequest->call();
71
72
            return $cURLResponse->getResponseBody();
73
        } catch (Exception $ex) {
74
75
            throw new ApiException("Failed to call Adoria API", $ex);
76
        }
77
    }
78
79
    /**
80
     * Get the debug.
81
     *
82
     * @return bool Returns the debug.
83
     */
84
    public function getDebug() {
85
        return $this->debug;
86
    }
87
88
    /**
89
     * Request data.
90
     *
91
     * @param RequestData $requestData The request data.
92
     * @return Result Returns the result.
93
     * @throws ApiException Throws an API exception exception if an error occurs.
94
     */
95
    public function requestData(RequestData $requestData) {
96
97
        $parameters = RequestNormalizer::normalizeRequestData($requestData);
98
99
        $rawResponse = $this->callAPI(self::REQUEST_DATA_RESOURCE_PATH, $parameters);
100
101
        return ResponseNormalizer::denormalizeResult($rawResponse);
102
    }
103
104
    /**
105
     * Set the debug.
106
     *
107
     * @param bool $debug The debug.
108
     * @return ApiProvider Returns this API provider.
109
     */
110
    public function setDebug($debug) {
111
        $this->debug = $debug;
112
        return $this;
113
    }
114
}
115