-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFacade.php
134 lines (122 loc) · 3.81 KB
/
Facade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Fruitware\WhmcsWrapper;
use Fruitware\WhmcsWrapper\Config\DefaultConfig;
use Fruitware\WhmcsWrapper\Connect\Connector;
use Fruitware\WhmcsWrapper\Exception\RuntimeException;
use GuzzleHttp\Exception\ClientException;
/**
* @package Fruitware\WhmcsWrapper
* @author Fruits Foundation <foundation@fruits.agency>
*
* @link https://developers.whmcs.com/api/
*
* Class Facade
* @property Connector|null connector
*/
class Facade
{
protected $connector;
/**
* @return Connector
*
* @throws RuntimeException
*/
protected function connector(): Connector
{
if (!$this->connector) {
throw new RuntimeException('You should call `connect` method first and pass the connection data');
}
return $this->connector;
}
/**
* Damn simple connect method:
*
* $whmscClient = Facade::run()->connect(<whmcs home url>, <identifier>, <secret>);
* $whmscClient->call('GetHealthStatus');
*
* OR since method is chained to Facade is can be a one-liner:,
*
* Facade::run()->connect(<whmcs home url>, <identifier>, <secret>)->call('GetHealthStatus');
*
* “Authenticating With API Credentials” method is the only available right now
* @link https://developers.whmcs.com/api/authentication/
*
* @param string $uri path to your WHMCS installation (HTTP_ROOT)
* @param string $key WHMCS Identifier
* @param string $secret WHMCS Identifier's Secret key
* @param array|string[] $params `form_fields` that will be used for each API-call e.g. 'responsetype' => 'json'
* @param array $config Request options to apply. See \GuzzleHttp\RequestOptions
* @return Facade
*
* @throws RuntimeException
*/
public function connect(
string $uri,
string $key,
string $secret,
array $params = [],
array $config = []
): Facade {
$this->connector = self::getConnector($uri, $key, $secret, $params, $config);
return $this;
}
/**
* Direct access to the connector object
*
* “Authenticating With API Credentials” method is the only available right now
* @link https://developers.whmcs.com/api/authentication/
*
* @param string $uri path to your WHMCS installation (HTTP_ROOT)
* @param string $key WHMCS Identifier
* @param string $secret WHMCS Identifier's Secret key
* @param array|string[] $params `form_fields` that will be used for each API-call e.g. 'responsetype' => 'json'
* @param array $config Request options to apply. See \GuzzleHttp\RequestOptions
* @return Connector
*
* @throws RuntimeException
*/
public static function getConnector(
string $uri,
string $key,
string $secret,
array $params = [],
array $config = []
): ?Connector {
return Connector::connect(DefaultConfig::i(
$uri,
$key,
$secret,
$params,
$config
));
}
/**
* Method used to access instantiated object with single connection
* @return Facade
*/
final public static function run(): Facade
{
static $self;
if (!$self) {
$self = new self();
}
return $self;
}
/**
* Suitable for any API-call
*
* @link https://developers.whmcs.com/api/api-index/
*
* @param string $action
* @param array $params
* @param bool $skipValidation
*
* @return array|null
*
* @throws RuntimeException|ClientException
*/
final public function call(string $action, array $params = [], bool $skipValidation = false): ?array
{
return self::run()->connector()->call($action, $params, $skipValidation);
}
}