Passed
Push — master ( ad8280...fa79aa )
by Theo
02:39
created

WHMClient::sendRequest()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 12
nop 3
dl 0
loc 33
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Chase\cPanelWHM;
4
5
use GuzzleHttp\Psr7\Request;
6
use Http\Adapter\Guzzle6\Client;
7
use Http\Client\HttpClient;
8
use Chase\cPanelWHM\Exceptions\ClientExceptions;
9
10
class WHMClient
11
{
12
    /**
13
     *
14
     * @var string
15
     */
16
    protected $whmUser;
17
    /**
18
     *
19
     * @var string
20
     */
21
    protected $apiToken;
22
23
    /**
24
     *
25
     * @var string
26
     */
27
    protected $whmHost;
28
29
    /**
30
     *
31
     * @var int
32
     */
33
    protected $whmPort;
34
35
    /**
36
     *
37
     * @var HttpClient
38
     */
39
    protected $httpClient;
40
41
    /**
42
     * Client constructor.
43
     *
44
     * @param string $whmUser
45
     * @param $apiToken
46
     * @param $whmHost
47
     * @param int $whmPort
48
     */
49
    public function __construct($whmUser, $apiToken, $whmHost, $whmPort = 2087)
50
    {
51
        $this->whmUser = $whmUser;
52
        $this->apiToken = $apiToken;
53
        $this->whmHost = $whmHost;
54
        $this->whmPort = $whmPort;
55
56
        $client = Client::createWithConfig(['timeout' => 120]);
57
        $this->setHttpClient($client);
58
        $this->httpClient = $this->getHttpClient();
59
    }
60
61
    /**
62
     *
63
     * @param HttpClient $client
64
     *
65
     * @return WHMClient
66
     */
67
    public function setHttpClient(HttpClient $client)
68
    {
69
        $this->httpClient = $client;
70
71
        return $this;
72
    }
73
74
    /**
75
     *
76
     * @return HttpClient
77
     */
78
    public function getHttpClient()
79
    {
80
        return $this->httpClient;
81
    }
82
83
    /**
84
     *
85
     * @param $endpoint
86
     * @param $method
87
     * @param array $params
88
     *
89
     * @return mixed|\Psr\Http\Message\ResponseInterface
90
     * @throws ClientExceptions
91
     * @throws \Http\Client\Exception
92
     */
93
    public function sendRequest($endpoint, $method, array $params = [])
94
    {
95
        $params = array_merge(['api.version' => 1], $params);
96
        $queryParams = http_build_query($params);
97
98
        $url = sprintf("https://%s:%s%s", $this->whmHost, $this->whmPort, $endpoint);
99
100
        $request = new Request($method, $url . "?" . $queryParams);
101
        $request = $request->withHeader("Authorization", "whm {$this->whmUser}:{$this->apiToken}");
102
103
        $response = $this->httpClient->sendRequest($request);
104
105
        $data = [];
106
        if (strpos($response->getHeaderLine("Content-Type"), "application/json") === 0) {
107
            $data = json_decode((string)$response->getBody(), true);
108
        } elseif (strpos($response->getHeaderLine("Content-Type"), "text/plain") === 0) {
109
            $data = json_decode((string)$response->getBody(), true);
110
        }
111
112
        if (array_key_exists("status", $data) && $data['status'] === 0) {
113
            throw ClientExceptions::accessDenied(!empty($data['statusmsg']) ? $data['statusmsg'] : null);
114
        }
115
116
        if ($response->getStatusCode() === 403) {
117
            if (!empty($data['cpanelresult']['error'])) {
118
                throw ClientExceptions::accessDenied(
119
                    $data['cpanelresult']['error'],
120
                    $data['cpanelresult']['data']['reason']
121
                );
122
            }
123
        }
124
125
        return $data;
126
    }
127
}
128