Passed
Push — master ( 82039d...77fef7 )
by Shaharia
01:39
created

WHMClient::sendRequest()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 12
nop 3
dl 0
loc 31
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace PreviewTechs\cPanelWHM;
4
5
use GuzzleHttp\Psr7\Request;
6
use Http\Adapter\Guzzle6\Client;
7
use Http\Client\HttpClient;
8
use PreviewTechs\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
        $queryParams = http_build_query($params);
96
        $url         = sprintf("https://%s:%s%s", $this->whmHost, $this->whmPort, $endpoint);
97
98
        $request = new Request($method, $url . "?" . $queryParams);
99
        $request = $request->withHeader("Authorization", "whm {$this->whmUser}:{$this->apiToken}");
100
101
        $response = $this->httpClient->sendRequest($request);
102
103
        $data = [];
104
        if (strpos($response->getHeaderLine("Content-Type"), "application/json") === 0) {
105
            $data = json_decode((string)$response->getBody(), true);
106
        } elseif (strpos($response->getHeaderLine("Content-Type"), "text/plain") === 0) {
107
            $data = json_decode((string)$response->getBody(), true);
108
        }
109
110
        if(array_key_exists("status", $data) && $data['status'] === 0){
111
            throw ClientExceptions::accessDenied(!empty($data['statusmsg']) ? $data['statusmsg'] : null);
112
        }
113
114
        if ($response->getStatusCode() === 403) {
115
            if (! empty($data['cpanelresult']['error'])) {
116
                throw ClientExceptions::accessDenied(
117
                    $data['cpanelresult']['error'],
118
                    $data['cpanelresult']['data']['reason']
119
                );
120
            }
121
        }
122
123
        return $data;
124
    }
125
}
126