WHMClient::getHttpClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 3
    public function __construct($whmUser, $apiToken, $whmHost, $whmPort = 2087)
50
    {
51 3
        $this->whmUser = $whmUser;
52 3
        $this->apiToken = $apiToken;
53 3
        $this->whmHost = $whmHost;
54 3
        $this->whmPort = $whmPort;
55
56 3
        $client = Client::createWithConfig(['timeout' => 120]);
57 3
        $this->setHttpClient($client);
58 3
        $this->httpClient = $this->getHttpClient();
59 3
    }
60
61
    /**
62
     *
63
     * @param HttpClient $client
64
     *
65
     * @return WHMClient
66
     */
67 3
    public function setHttpClient(HttpClient $client)
68
    {
69 3
        $this->httpClient = $client;
70
71 3
        return $this;
72
    }
73
74
    /**
75
     *
76
     * @return HttpClient
77
     */
78 3
    public function getHttpClient()
79
    {
80 3
        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 2
    public function sendRequest($endpoint, $method, array $params = [])
94
    {
95 2
        $params = array_merge(['api.version' => 1], $params);
96 2
        $queryParams = http_build_query($params);
97
98 2
        $url = sprintf("https://%s:%s%s", $this->whmHost, $this->whmPort, $endpoint);
99
100 2
        $request = new Request($method, $url . "?" . $queryParams);
101 2
        $request = $request->withHeader("Authorization", "whm {$this->whmUser}:{$this->apiToken}");
102
103 2
        $response = $this->httpClient->sendRequest($request);
104
105 2
        $data = [];
106 2
        if (strpos($response->getHeaderLine("Content-Type"), "application/json") === 0) {
107 1
            $data = json_decode((string)$response->getBody(), true);
108 2
        } elseif (strpos($response->getHeaderLine("Content-Type"), "text/plain") === 0) {
109 2
            $data = json_decode((string)$response->getBody(), true);
110
        }
111
112 2
        if (array_key_exists("status", $data) && $data['status'] === 0) {
113
            throw ClientExceptions::accessDenied(!empty($data['statusmsg']) ? $data['statusmsg'] : null);
114
        }
115
116 2
        if ($response->getStatusCode() === 403) {
117 1
            if (!empty($data['cpanelresult']['error'])) {
118 1
                throw ClientExceptions::accessDenied(
119 1
                    $data['cpanelresult']['error'],
120 1
                    $data['cpanelresult']['data']['reason']
121
                );
122
            }
123
        }
124
125 1
        return $data;
126
    }
127
}
128