Skip to content

Commit

Permalink
Merge pull request #3 from nysos3/master
Browse files Browse the repository at this point in the history
Update to latest docusign/esign-client && PHPUnit, Support DocuSign's new dynamic host URL requirements
  • Loading branch information
Tucker-Eric authored Jul 10, 2019
2 parents e64813f + ef8ebb9 commit ffa5020
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
}
],
"require": {
"php": ">=5.5.9",
"docusign/esign-client": "^3.0"
"php": "^7.2",
"docusign/esign-client": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "5.3.*"
"phpunit/phpunit": "8.2.*"
},
"autoload": {
"psr-4": {
"DocuSign\\Rest\\": "src/"
}
},
"minimum-stability": "dev"
}
}
18 changes: 14 additions & 4 deletions src/Api/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ abstract class BaseApi
public function __construct(ApiClient $apiClient)
{
$this->apiClient = $apiClient;
$this->initClient();
}

private function initClient()
{
$docusignClass = str_replace(__NAMESPACE__, "DocuSign\\eSign\\Api", get_class($this)) . 'Api';
$this->client = new $docusignClass($apiClient->getClient());
$this->client = new $docusignClass($this->apiClient->getClient());
}

/**
* Magic method to construct an options model or call an apimethod
* Magic method to construct an options model or call an api method
* @param $method
* @param $args
* @return mixed
Expand All @@ -56,7 +61,12 @@ public function __call($method, $args)
}

if ($method !== 'login' && !$this->apiClient->isAuthenticated()) {
$host = $this->apiClient->getHost();
$this->apiClient->authenticate();
// If the host has changed, update host on client config
if ($host !== $this->apiClient->getHost()) {
$this->initClient();
}
}

if ($this->usesAccountId) {
Expand All @@ -68,7 +78,7 @@ public function __call($method, $args)

/**
* Get an options object or all of them for current Api class
*
*
* @param null $method
* @return array|mixed
* @throws ClassNotFoundException
Expand Down Expand Up @@ -119,4 +129,4 @@ public function setOptionsObject($method, $args)

return $optionClass;
}
}
}
30 changes: 24 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DocuSign\Rest;

use DocuSign\eSign\ApiClient;
use DocuSign\eSign\Client\ApiClient;
use DocuSign\eSign\Configuration;


Expand Down Expand Up @@ -67,6 +67,11 @@ public function __construct($params = [])
$this->{$key} = $val;
}

$this->initApiClient();
}

private function initApiClient()
{
$this->client = new ApiClient($this->setConfiguration());
}

Expand All @@ -80,6 +85,11 @@ public function setConfiguration()
]));
}

public function getHost()
{
return $this->host;
}

/**
* This magic method is to instantiate all classes in the \DocuSign\eSign\Model namespace
*
Expand Down Expand Up @@ -123,7 +133,7 @@ public function __get($name)
if (array_key_exists($name, $this->_api_container)) {
return $this->_api_container[$name];
}

if (!class_exists($apiClass = "DocuSign\\Rest\\Api\\" . ucfirst($name))) {
throw new Exceptions\ClassNotFoundException("Cannot Find Api Class $apiClass");
}
Expand All @@ -132,17 +142,25 @@ public function __get($name)
}

/**
* Authenticates api client and stores account_id
* Authenticates api client, stores account_id, and updates host if changed by docusign
*
* @return $this
*/
public function authenticate()
{
if (!isset($this->account_id)) {
$accounts = $this->authentication->login();
$allAccounts = $accounts->getLoginAccounts();
$account = $allAccounts[0];
$login_accounts = $accounts->getLoginAccounts();
$account = $login_accounts[0];
$this->account_id = $account->getAccountId();
$base_url = $account->getBaseUrl();
$base_url = strtolower(substr($base_url, 0, strpos($base_url,'/restapi') + 8));
// If the host has changed, update host on client config
if ($this->host !== $base_url) {
$this->host = $base_url;
$this->_api_container = [];
$this->initApiClient();
}
}

$this->authenticated = true;
Expand Down Expand Up @@ -177,4 +195,4 @@ public function getClient()
{
return $this->client;
}
}
}
6 changes: 4 additions & 2 deletions tests/LoginTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

class LoginTest extends PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class LoginTest extends TestCase
{
public function testLogin()
{
Expand All @@ -17,4 +19,4 @@ public function testLogin()

$this->assertStringMatchesFormat('%d', $docusign->getAccountId());
}
}
}
6 changes: 4 additions & 2 deletions tests/SignatureRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

class SignatureRequestTest extends PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class SignatureRequestTest extends TestCase
{
public function testSignatureRequest()
{
Expand Down Expand Up @@ -48,4 +50,4 @@ public function testSignatureRequest()

$this->assertInstanceOf(\DocuSign\eSign\Model\EnvelopeSummary::class, $envelopeSummary);
}
}
}

0 comments on commit ffa5020

Please sign in to comment.