Skip to content

Commit 5ad19cb

Browse files
Moussa Sidibesidibos
authored andcommitted
SDK-603: Add the ability to retrieve different path in the sandbox client
1 parent c9a0e88 commit 5ad19cb

File tree

6 files changed

+70
-26
lines changed

6 files changed

+70
-26
lines changed

sandbox/src/Http/RequestBuilder.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
namespace YotiSandbox\Http;
33

44
use Yoti\Entity\Profile;
5-
use YotiSandbox\Entity\SandboxAgeVerification;
65
use YotiSandbox\Entity\SandboxAnchor;
76
use YotiSandbox\Entity\SandboxAttribute;
7+
use YotiSandbox\Entity\SandboxAgeVerification;
88

99
class RequestBuilder
1010
{
@@ -59,11 +59,11 @@ public function setGivenNames($value, $optional = 'false', array $anchors = [])
5959
));
6060
}
6161

62-
public function setDateOfBirth($value, $optional = 'false', array $anchors = [])
62+
public function setDateOfBirth(\DateTime $dateTime, $optional = 'false', array $anchors = [])
6363
{
6464
$this->addAttribute($this->createAttribute(
6565
Profile::ATTR_DATE_OF_BIRTH,
66-
$value,
66+
$dateTime->format('d-m-Y'),
6767
'',
6868
$optional,
6969
$anchors
@@ -164,13 +164,9 @@ public function setDocumentDetails($value, $optional = 'true', array $anchors =
164164
));
165165
}
166166

167-
public function setAgeVerification(\DateTime $dateObj, $derivation, array $anchors = [])
167+
public function setAgeVerification(SandboxAgeVerification $ageVerification)
168168
{
169-
$this->addAttribute(new SandboxAgeVerification(
170-
$dateObj,
171-
$derivation,
172-
$anchors
173-
));
169+
$this->addAttribute($ageVerification);
174170
}
175171

176172
private function addAttribute(SandboxAttribute $attribute)
@@ -202,6 +198,17 @@ private function formatAnchors(array $anchors)
202198
return $anchorsList;
203199
}
204200

201+
/**
202+
* @param string $name
203+
* @param string $value
204+
* @param string $derivation
205+
* Empty value means there is no derivation for this attribute
206+
* @param string $optional
207+
* 'false' value means this attribute is required
208+
* @param array $anchors
209+
*
210+
* @return SandboxAttribute
211+
*/
205212
private function createAttribute($name, $value, $derivation, $optional, array $anchors)
206213
{
207214
return new SandboxAttribute($name, $value, $derivation, $optional, $anchors);
@@ -210,7 +217,7 @@ private function createAttribute($name, $value, $derivation, $optional, array $a
210217
/**
211218
* @return TokenRequest
212219
*/
213-
public function getRequest()
220+
public function createRequest()
214221
{
215222
return new TokenRequest($this->rememberMeId, $this->sandboxAttributes);
216223
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace YotiSandbox\Http;
4+
5+
class SandboxPathManager
6+
{
7+
/**
8+
* This path is for profile request
9+
*/
10+
const DEFAULT_PROFILE_API_PATH = 'https://dev0.api.yoti.com/sandbox/v1';
11+
/**
12+
* This path is for creating a Sandbox Application and for a token request
13+
*/
14+
const DEFAULT_TOKEN_API_PATH = 'https://dev0.api.yoti.com:11443/sandbox/v1';
15+
16+
private $tokenApiPath;
17+
private $profileApiPath;
18+
19+
public function __construct(
20+
$tokenApiPath = self::DEFAULT_TOKEN_API_PATH,
21+
$profileApiPath = self::DEFAULT_PROFILE_API_PATH
22+
)
23+
{
24+
$this->tokenApiPath = $tokenApiPath;
25+
$this->profileApiPath = $profileApiPath;
26+
}
27+
28+
public function getTokenApiPath()
29+
{
30+
return $this->tokenApiPath;
31+
}
32+
33+
public function getProfileApiPath()
34+
{
35+
return $this->profileApiPath;
36+
}
37+
}

sandbox/src/SandboxClient.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use YotiSandbox\Http\Response;
66
use Yoti\Http\CurlRequestHandler;
77
use YotiSandbox\Http\RequestBuilder;
8+
use YotiSandbox\Http\SandboxPathManager;
89

910
class SandboxClient
1011
{
1112
const TOKEN_REQUEST_ENDPOINT_FORMAT = "/apps/%s/tokens";
12-
const DEFAULT_SANDBOX_API_URL = "https://api.yoti.com/sandbox/v1";
1313

1414
/**
1515
* @var string
@@ -31,23 +31,23 @@ class SandboxClient
3131
*
3232
* @param string $sdkId
3333
* @param string $pem
34-
* @param string $sandboxApi
34+
* @param SandboxPathManager $sandboxPathManager
3535
* @param string $sdkIdentifier
3636
*
3737
* @throws \Yoti\Exception\RequestException
3838
* @throws \Yoti\Exception\YotiClientException
3939
*/
40-
public function __construct($sdkId, $pem, $sandboxApi = self::DEFAULT_SANDBOX_API_URL, $sdkIdentifier = 'PHP')
40+
public function __construct($sdkId, $pem, SandboxPathManager $sandboxPathManager, $sdkIdentifier = 'PHP')
4141
{
4242
$this->sdkId = $sdkId;
4343
$pem = $this->includePemWrapper($pem);
4444
$this->requestHandler = new CurlRequestHandler(
45-
$sandboxApi,
45+
$sandboxPathManager->getTokenApiPath(),
4646
$pem,
4747
$sdkId,
4848
$sdkIdentifier
4949
);
50-
$this->yotiClient = new YotiClient($sdkId, $pem);
50+
$this->yotiClient = new YotiClient($sdkId, $pem, $sandboxPathManager->getProfileApiPath());
5151
}
5252

5353
/**
@@ -96,7 +96,7 @@ public function getToken(RequestBuilder $requestBuilder, $httpMethod)
9696
*/
9797
protected function sendRequest(RequestBuilder $requestBuilder, $endpoint, $httpMethod)
9898
{
99-
$payload = $requestBuilder->getRequest()->getPayload();
99+
$payload = $requestBuilder->createRequest()->getPayload();
100100

101101
$resultArr = $this->requestHandler->sendRequest(
102102
$endpoint,
@@ -118,5 +118,4 @@ private function includePemWrapper($pem)
118118
}
119119
return $pem;
120120
}
121-
122121
}

sandbox/tests/Http/RequestBuilderTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ public function setUp()
2727

2828
public function testGetRequest()
2929
{
30-
$tokenRequest = $this->requestBuilder->getRequest();
30+
$tokenRequest = $this->requestBuilder->createRequest();
3131
$this->assertInstanceOf(TokenRequest::class, $tokenRequest);
3232
}
3333

3434
public function testGetRememberMeId()
3535
{
36-
$tokenRequest = $this->requestBuilder->getRequest();
36+
$tokenRequest = $this->requestBuilder->createRequest();
3737
$this->assertEquals('fake_remember_me_id', $tokenRequest->getRememberMeId());
3838
}
3939

4040
public function testShouldReturnFamilyNameAttr()
4141
{
42-
$tokenRequest = $this->requestBuilder->getRequest();
42+
$tokenRequest = $this->requestBuilder->createRequest();
4343
$sandboxAttributes = $tokenRequest->getSandboxAttributes();
44-
$attrs = $sandboxAttributes['profile_attributes'];
45-
$this->assertEquals($attrs[0]['name'], 'family_name');
44+
$this->assertEquals($sandboxAttributes[0]['name'], 'family_name');
4645
}
4746
}

sandbox/tests/Http/TokenRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function setUp()
3434

3535
public function testGetSandboxAttributes()
3636
{
37-
$expectedAttrs['profile_attributes'] = $this->sandboxAttrs;
37+
$expectedAttrs= $this->sandboxAttrs;
3838
$this->assertEquals(
3939
json_encode($expectedAttrs),
4040
json_encode($this->tokenRequest->getSandboxAttributes())

sandbox/tests/SandboxClientTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SandboxTest;
44

55
use YotiSandbox\Http\RequestBuilder;
6+
use YotiSandbox\Http\SandboxPathManager;
67
use YotiTest\TestCase;
78

89
class SandboxClientTest extends TestCase
@@ -16,19 +17,20 @@ class SandboxClientTest extends TestCase
1617
public function setUp()
1718
{
1819
$this->pem = file_get_contents(PEM_FILE);
20+
$sandboxPathManager = new SandboxPathManager();
1921

2022
$this->sandboxClient = $this->getMockBuilder('YotiSandbox\SandboxClient')
21-
->setConstructorArgs([SDK_ID, $this->pem])
23+
->setConstructorArgs([SDK_ID, $this->pem, $sandboxPathManager])
2224
->setMethods(['sendRequest'])
2325
->getMock();
2426
}
2527

2628
public function testGetToken()
2729
{
2830
$expectedToken = 'fake_token_xxx';
29-
$result['response'] = [
31+
$result['response'] = json_encode([
3032
'token' => $expectedToken
31-
];
33+
]);
3234
$result['http_code'] = 201;
3335

3436
// Stub the method sendRequest to return the result we want

0 commit comments

Comments
 (0)