Skip to content

Commit

Permalink
Merge branch 'release/0.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
keriat committed Jul 18, 2020
2 parents 0a53786 + c3c17f3 commit 83a6f8b
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 314 deletions.
20 changes: 20 additions & 0 deletions demo/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/** @noinspection ALL */
require_once '../vendor/autoload.php';

use Fruitware\WhmcsWrapper\Facade;

$apiUrl = 'https://example.com';
$apiIdentifier = '';
$apiSecret = '';

try {
$client = Facade::run()->connect(
$apiUrl,
$apiIdentifier,
$apiSecret
);
var_dump($client->call('GetClients'));
} catch (Exception $exception) {
var_dump('Error: ', $exception->getMessage(), $exception->getTraceAsString());
}
100 changes: 38 additions & 62 deletions src/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,83 @@
namespace Fruitware\WhmcsWrapper\Config;

/**
* @todo End uo with the configuration architecture
* @package Fruitware\WhmcsWrapper
* @author Fruits Foundation <foundation@fruits.agency>
*/
interface ConfigInterface
{
/**
* Constructor expected to be protected but omitted to comply
*
* ConfigInterface constructor.
* Protected to disable instancing but static i() method
*
*
* @param string $apiBaseUri
* @param string $apiIdentifier
* @param string $apiSecret
* @param string $key
* @param string $secret
* @param array $postDefaultParams
* @param array $requestOptions
* @param array $clientOptions
*/
public function __construct(
string $apiBaseUri,
string $apiIdentifier,
string $apiSecret,
string $key,
string $secret,
array $postDefaultParams,
array $requestOptions
array $clientOptions
);

/**
* Singleton config initialization
*
* @link https://developers.whmcs.com/api/authentication/
*
* @param string $baseUri path to your WHMCS installation (HTTP_ROOT)
*
* @param string $identifier WHMCS Identifier
* @param string $secret WHMCS Identifier's Secret key
*
* Members are used as `form_fields` for each API-call as POST-request
* @param array|string[] $defaultConfig default: array['responsetype' => 'json']
* @param array $clientOptions Request options to apply. See \GuzzleHttp\RequestOptions
*
* @param array $requestOptions Request options to apply. See \GuzzleHttp\RequestOptions
* @return ConfigInterface
*/
public static function i(
string $baseUri,
string $identifier,
string $secret,
array $defaultConfig = ['responsetype' => 'json'],
array $requestOptions = ['timeout' => 3.0]
array $defaultConfig = [],
array $clientOptions = []
):
ConfigInterface;

public function getRequestUrl(): string;

/**
* Transmitting securely default form_field POST-params to the Connector
*
* GuzzleHttp\ClientInterface client's config (used during initialization)
* @return array
*/
public function getDefaultParams(): array;
public function prepareConfig(): array;

/**
* Transmitting securely API POST request url
* Base64 encoded serialized array of custom field values
* @link https://developers.whmcs.com/api-reference/addorder/
* @link http://mahmudulruet.blogspot.com/2011/10/adding-and-posting-custom-field-values.html
*
* @return string
*/
public function getRequestUrl(): string;

/**
* Transmitting given authorization `identifier` to configure http-client
* 1. Login to your WHMCS admin panel.
* 2. Navigate Setup->Client Custom Fields
* 3. If there is no custom fields yet create a new one; say named "VAT".
* 4. After creating the field, see the HTML source from the page by right clicking on page and click view source (in Firefox).
* 5. Find the text "VAT".
* 6. You will find something like this line of HTML code (<input type="text" size="30" value="VAT" name="fieldname[11]">)
* 7. This fieldname[] with id may vary by users admin panel. So track the id from fieldname[11] and yes it's 11. If you find something like <input type="text" size="30" value="VAT" name="fieldname[xx]"> then 'xx' will be the id you have to use in your $customfields array. The array now should look like:
*
* @return string
*/
public function getAuthIdentifier(): string;

/**
* Transmitting `base_uri` value to configure http-client
* $customfields = [
* "11" => "123456",
* "12" => "678945"
* ];
* $postfields["customfields"] = base64_encode(serialize($customfields));
*
* @return string
*/
public function getBaseUri(): string;

/**
* Transmitting given authorization `secret` to configure http-client
* Preprocessing the request params.
* Main purpose of the method is to encode `customfields` param.
*
* @return string
*/
public function getAuthSecret(): string;

/**
* Setting default http-client options
* @param string $action method name
* @param array $params query params
* @param bool $skipValidation cancel validation of the required fields
*
* @return array
*/
public function getRequestOptions(): array;


/**
* @param array $params
* @return ConfigInterface
*/
public function updateDefaultParams(array $params = []): ConfigInterface;


/**
* @param array $options
* @return ConfigInterface
*/
public function updateRequestOptions(array $options = []): ConfigInterface;
public function prepareRequestOptions(string $action, array $params, bool $skipValidation): array;
}
149 changes: 45 additions & 104 deletions src/Config/DefaultConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,49 @@

namespace Fruitware\WhmcsWrapper\Config;

use function filter_var;
use function rtrim;

/**
* Class DefaultConfig
*
* @package Fruitware\WhmcsWrapper\Config
* @package Fruitware\WhmcsWrapper
* @author Fruits Foundation <foundation@fruits.agency>
*/
final class DefaultConfig implements ConfigInterface
{
/**
* @var string
*/
protected string $apiRequestUrl = '/includes/api.php';
protected string $key;
protected string $secret;

/**
* @var string
*/
protected string $apiBaseUri;

/**
* @var string
*/
protected string $apiIdentifier;

/**
* @var string
*/
protected string $apiSecret;

/**
* @var array
*/
protected array $postDefaultParams;
protected array $postDefaultParams = [
'responsetype' => 'json'
];

/**
* @var array
*/
protected array $requestOptions;
protected array $clientOptions = [
'base_uri' => '',
];

/**
* @inheritDoc
*/
protected function __construct(
public function __construct(
string $apiBaseUri,
string $apiIdentifier,
string $apiSecret,
string $key,
string $secret,
array $postDefaultParams,
array $requestOptions
array $clientOptions
) {
/**
* Filtering trailing slash
*/
$this->apiBaseUri = rtrim($apiBaseUri, '/');
$this->key = $key;
$this->secret = $secret;

$this->apiIdentifier = $apiIdentifier;
$this->apiSecret = $apiSecret;
$this->postDefaultParams = $postDefaultParams;
$this->requestOptions = $requestOptions;
if ($postDefaultParams) {
$this->postDefaultParams = $postDefaultParams;
}
if ($clientOptions) {
$this->clientOptions = $clientOptions;
}
$this->clientOptions['base_uri'] = filter_var(rtrim($apiBaseUri, "/"), FILTER_SANITIZE_URL);
}

/**
Expand All @@ -69,19 +54,18 @@ public static function i(
string $baseUri,
string $identifier,
string $secret,
array $defaultConfig = ['responsetype' => 'json'],
array $requestOptions = ['timeout' => 3.0]
array $defaultConfig = [],
array $clientOptions = []
): ConfigInterface {
return new self(
$baseUri,
$identifier,
$secret,
$defaultConfig,
$requestOptions
$clientOptions
);
}


/**
* Transmitting securely API POST request url
*
Expand All @@ -93,77 +77,34 @@ public function getRequestUrl(): string
}

/**
* Transmitting given authorization `identifier` to configure http-client
*
* @return string
*/
public function getAuthIdentifier(): string
{
return $this->apiIdentifier;
}


/**
* Transmitting `base_uri` value to configure http-client
*
* @return string
*/
public function getBaseUri(): string
{
return $this->apiBaseUri;
}

/**
* @return string `secret` to configure http-client
*/
public function getAuthSecret(): string
{
return $this->apiSecret;
}

/**
* @return array default http-client options
* @inheritDoc
*/
public function getRequestOptions(): array
public function prepareConfig(): array
{
return $this->requestOptions;
return $this->clientOptions;
}

/**
* @param array $options
* @return ConfigInterface
* @inheritDoc
*/
public function updateRequestOptions(array $options = []): ConfigInterface
public function prepareRequestOptions(string $action, array $params, bool $skipValidation): array
{
if ($options) {
$this->requestOptions = array_merge($this->requestOptions, $options);
if ($skipValidation) {
$params['skipvalidation'] = true;
}
return $this;
}
$params['action'] = $action;

$result = array_merge(
$this->postDefaultParams, [
'identifier' => $this->key,
'secret' => $this->secret,
], $params);

/**
* @param array $params
* @return ConfigInterface
*/
public function updateDefaultParams(array $params = []): ConfigInterface
{
if ($params) {
$this->postDefaultParams = array_merge($this->postDefaultParams, $params);
if (!empty($result['customfields'])) {
$result['customfields'] = base64_encode(serialize($result['customfields']));
}
return $this;
}

/**
* Transmitting securely default form_field POST-params to the Connector
*
* @return array
*/
public function getDefaultParams(): array
{
return $this->postDefaultParams + [
'identifier' => $this->apiIdentifier,
'secret' => $this->apiSecret,
];
return [
'form_params' => $result
];
}
}
Loading

0 comments on commit 83a6f8b

Please sign in to comment.