Skip to content

Commit ffa5020

Browse files
authored
Merge pull request #3 from nysos3/master
Update to latest docusign/esign-client && PHPUnit, Support DocuSign's new dynamic host URL requirements
2 parents e64813f + ef8ebb9 commit ffa5020

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

composer.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=5.5.9",
17-
"docusign/esign-client": "^3.0"
16+
"php": "^7.2",
17+
"docusign/esign-client": "^4.0"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "5.3.*"
20+
"phpunit/phpunit": "8.2.*"
2121
},
2222
"autoload": {
2323
"psr-4": {
2424
"DocuSign\\Rest\\": "src/"
2525
}
26-
},
27-
"minimum-stability": "dev"
26+
}
2827
}

src/Api/BaseApi.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ abstract class BaseApi
3838
public function __construct(ApiClient $apiClient)
3939
{
4040
$this->apiClient = $apiClient;
41+
$this->initClient();
42+
}
43+
44+
private function initClient()
45+
{
4146
$docusignClass = str_replace(__NAMESPACE__, "DocuSign\\eSign\\Api", get_class($this)) . 'Api';
42-
$this->client = new $docusignClass($apiClient->getClient());
47+
$this->client = new $docusignClass($this->apiClient->getClient());
4348
}
4449

4550
/**
46-
* Magic method to construct an options model or call an apimethod
51+
* Magic method to construct an options model or call an api method
4752
* @param $method
4853
* @param $args
4954
* @return mixed
@@ -56,7 +61,12 @@ public function __call($method, $args)
5661
}
5762

5863
if ($method !== 'login' && !$this->apiClient->isAuthenticated()) {
64+
$host = $this->apiClient->getHost();
5965
$this->apiClient->authenticate();
66+
// If the host has changed, update host on client config
67+
if ($host !== $this->apiClient->getHost()) {
68+
$this->initClient();
69+
}
6070
}
6171

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

6979
/**
7080
* Get an options object or all of them for current Api class
71-
*
81+
*
7282
* @param null $method
7383
* @return array|mixed
7484
* @throws ClassNotFoundException
@@ -119,4 +129,4 @@ public function setOptionsObject($method, $args)
119129

120130
return $optionClass;
121131
}
122-
}
132+
}

src/Client.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace DocuSign\Rest;
44

5-
use DocuSign\eSign\ApiClient;
5+
use DocuSign\eSign\Client\ApiClient;
66
use DocuSign\eSign\Configuration;
77

88

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

70+
$this->initApiClient();
71+
}
72+
73+
private function initApiClient()
74+
{
7075
$this->client = new ApiClient($this->setConfiguration());
7176
}
7277

@@ -80,6 +85,11 @@ public function setConfiguration()
8085
]));
8186
}
8287

88+
public function getHost()
89+
{
90+
return $this->host;
91+
}
92+
8393
/**
8494
* This magic method is to instantiate all classes in the \DocuSign\eSign\Model namespace
8595
*
@@ -123,7 +133,7 @@ public function __get($name)
123133
if (array_key_exists($name, $this->_api_container)) {
124134
return $this->_api_container[$name];
125135
}
126-
136+
127137
if (!class_exists($apiClass = "DocuSign\\Rest\\Api\\" . ucfirst($name))) {
128138
throw new Exceptions\ClassNotFoundException("Cannot Find Api Class $apiClass");
129139
}
@@ -132,17 +142,25 @@ public function __get($name)
132142
}
133143

134144
/**
135-
* Authenticates api client and stores account_id
145+
* Authenticates api client, stores account_id, and updates host if changed by docusign
136146
*
137147
* @return $this
138148
*/
139149
public function authenticate()
140150
{
141151
if (!isset($this->account_id)) {
142152
$accounts = $this->authentication->login();
143-
$allAccounts = $accounts->getLoginAccounts();
144-
$account = $allAccounts[0];
153+
$login_accounts = $accounts->getLoginAccounts();
154+
$account = $login_accounts[0];
145155
$this->account_id = $account->getAccountId();
156+
$base_url = $account->getBaseUrl();
157+
$base_url = strtolower(substr($base_url, 0, strpos($base_url,'/restapi') + 8));
158+
// If the host has changed, update host on client config
159+
if ($this->host !== $base_url) {
160+
$this->host = $base_url;
161+
$this->_api_container = [];
162+
$this->initApiClient();
163+
}
146164
}
147165

148166
$this->authenticated = true;
@@ -177,4 +195,4 @@ public function getClient()
177195
{
178196
return $this->client;
179197
}
180-
}
198+
}

tests/LoginTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
class LoginTest extends PHPUnit_Framework_TestCase
3+
use PHPUnit\Framework\TestCase;
4+
5+
class LoginTest extends TestCase
46
{
57
public function testLogin()
68
{
@@ -17,4 +19,4 @@ public function testLogin()
1719

1820
$this->assertStringMatchesFormat('%d', $docusign->getAccountId());
1921
}
20-
}
22+
}

tests/SignatureRequestTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
class SignatureRequestTest extends PHPUnit_Framework_TestCase
3+
use PHPUnit\Framework\TestCase;
4+
5+
class SignatureRequestTest extends TestCase
46
{
57
public function testSignatureRequest()
68
{
@@ -48,4 +50,4 @@ public function testSignatureRequest()
4850

4951
$this->assertInstanceOf(\DocuSign\eSign\Model\EnvelopeSummary::class, $envelopeSummary);
5052
}
51-
}
53+
}

0 commit comments

Comments
 (0)