Completed
Push — master ( 93fa4e...dc02e9 )
by WEBEWEB
04:24
created

APIProvider::requestData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\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);
0 ignored issues
show
Deprecated Code introduced by
The class WBW\Library\Core\Network...Request\CURLPostRequest has been deprecated with message: since 6.0.0 use {http://docs.guzzlephp.org/en/stable/} instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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