Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/DocScan/Session/Retrieve/GetSessionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,25 @@ public function getImportToken(): ?ImportTokenResponse
{
return $this->importToken;
}

/**
* Returns the resources used by the check matching the given check ID.
*
* @param string $checkId
* @return ResourceContainer
*
* @throws \InvalidArgumentException if no check exists with the given ID
*/
public function getResourcesForCheck(string $checkId): ResourceContainer
{
foreach ($this->checks as $check) {
if ($check->getId() === $checkId) {
return isset($this->resources)
? $this->resources->filterForCheck($check)
: new ResourceContainer([]);
}
}

throw new \InvalidArgumentException('Check not found');
}
}
39 changes: 39 additions & 0 deletions src/DocScan/Session/Retrieve/ResourceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,43 @@ function ($resourceResponse) use ($class): bool {

return array_values($filtered);
}

/**
* Returns a new ResourceContainer containing only the resources referenced
* by the given check's "resources used" list.
*
* @param CheckResponse $checkResponse
* @return ResourceContainer
*/
public function filterForCheck(CheckResponse $checkResponse): ResourceContainer
{
$resourceIds = $checkResponse->getResourcesUsed();

$filtered = new ResourceContainer([]);
$filtered->idDocuments = $this->filterResourcesByIds($this->idDocuments, $resourceIds);
$filtered->supplementaryDocuments = $this->filterResourcesByIds($this->supplementaryDocuments, $resourceIds);
$filtered->livenessCapture = $this->filterResourcesByIds($this->livenessCapture, $resourceIds);
$filtered->faceCapture = $this->filterResourcesByIds($this->faceCapture, $resourceIds);
$filtered->shareCodes = $this->filterResourcesByIds($this->shareCodes, $resourceIds);
$filtered->applicantProfiles = $this->filterResourcesByIds($this->applicantProfiles, $resourceIds);

return $filtered;
}

/**
* @param ResourceResponse[] $resources
* @param string[] $resourceIds
* @return mixed[]
*/
private function filterResourcesByIds(array $resources, array $resourceIds): array
{
$filtered = array_filter(
$resources,
function (ResourceResponse $resource) use ($resourceIds): bool {
return in_array($resource->getId(), $resourceIds, true);
}
);

return array_values($filtered);
}
}
72 changes: 72 additions & 0 deletions tests/DocScan/Session/Retrieve/GetSessionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Yoti\DocScan\Session\Retrieve\IdentityProfileResponse;
use Yoti\DocScan\Session\Retrieve\ImportTokenResponse;
use Yoti\DocScan\Session\Retrieve\MediaResponse;
use Yoti\DocScan\Session\Retrieve\ResourceContainer;
use Yoti\DocScan\Session\Retrieve\ThirdPartyIdentityFraudOneCheckResponse;
use Yoti\Test\TestCase;
use Yoti\Util\DateTime;
Expand Down Expand Up @@ -354,4 +355,75 @@ public function shouldReturnNullWhenAdvancedIdentityProfileNotPresent()
$this->assertNull($result->getAdvancedIdentityProfile());
$this->assertNull($result->getAdvancedIdentityProfilePreview());
}

/**
* @test
* @covers ::getResourcesForCheck
*/
public function shouldReturnResourcesForCheck()
{
$input = [
'checks' => [
[
'id' => 'some-check-id',
'type' => self::ID_DOCUMENT_AUTHENTICITY,
'resources_used' => ['some-id-document-id'],
],
],
'resources' => [
'id_documents' => [
['id' => 'some-id-document-id'],
['id' => 'some-other-id-document-id'],
],
],
];

$result = new GetSessionResult($input);

$resourcesForCheck = $result->getResourcesForCheck('some-check-id');

$this->assertInstanceOf(ResourceContainer::class, $resourcesForCheck);
$this->assertCount(1, $resourcesForCheck->getIdDocuments());
$this->assertEquals('some-id-document-id', $resourcesForCheck->getIdDocuments()[0]->getId());
}

/**
* @test
* @covers ::getResourcesForCheck
*/
public function shouldReturnEmptyResourceContainerWhenSessionHasNoResources()
{
$input = [
'checks' => [
['id' => 'some-check-id', 'type' => self::ID_DOCUMENT_AUTHENTICITY],
],
];

$result = new GetSessionResult($input);

$resourcesForCheck = $result->getResourcesForCheck('some-check-id');

$this->assertInstanceOf(ResourceContainer::class, $resourcesForCheck);
$this->assertCount(0, $resourcesForCheck->getIdDocuments());
}

/**
* @test
* @covers ::getResourcesForCheck
*/
public function shouldThrowExceptionWhenCheckIdNotFound()
{
$input = [
'checks' => [
['id' => 'some-check-id', 'type' => self::ID_DOCUMENT_AUTHENTICITY],
],
];

$result = new GetSessionResult($input);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Check not found');

$result->getResourcesForCheck('some-unknown-check-id');
}
}
111 changes: 111 additions & 0 deletions tests/DocScan/Session/Retrieve/ResourceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yoti\Test\DocScan\Session\Retrieve;

use Yoti\DocScan\Session\Retrieve\ApplicantProfileResourceResponse;
use Yoti\DocScan\Session\Retrieve\CheckResponse;
use Yoti\DocScan\Session\Retrieve\ResourceContainer;
use Yoti\DocScan\Session\Retrieve\ShareCodeResourceResponse;
use Yoti\DocScan\Session\Retrieve\StaticLivenessResourceResponse;
Expand Down Expand Up @@ -219,4 +220,114 @@ public function shouldParseApplicantProfiles(): void
$result->getApplicantProfiles()[0]->getId()
);
}

/**
* @test
* @covers ::filterForCheck
* @covers ::filterResourcesByIds
*/
public function shouldFilterForCheckAndReturnFilteredCollections(): void
{
$input = [
'id_documents' => [
['id' => 'id-document-1'],
['id' => 'id-document-2'],
],
'supplementary_documents' => [
['id' => 'supplementary-document-1'],
['id' => 'supplementary-document-2'],
],
'liveness_capture' => [
['id' => 'liveness-1', 'liveness_type' => 'ZOOM'],
['id' => 'liveness-2', 'liveness_type' => 'ZOOM'],
],
'face_capture' => [
['id' => 'face-capture-1'],
['id' => 'face-capture-2'],
],
'share_codes' => [
['id' => 'share-code-1'],
['id' => 'share-code-2'],
],
'applicant_profiles' => [
['id' => 'applicant-profile-1'],
['id' => 'applicant-profile-2'],
],
];

$checkResponse = new CheckResponse([
'resources_used' => [
'id-document-1',
'supplementary-document-1',
'liveness-1',
'face-capture-1',
'share-code-1',
'applicant-profile-1',
],
]);

$result = (new ResourceContainer($input))->filterForCheck($checkResponse);

$this->assertCount(1, $result->getIdDocuments());
$this->assertEquals('id-document-1', $result->getIdDocuments()[0]->getId());

$this->assertCount(1, $result->getSupplementaryDocuments());
$this->assertEquals('supplementary-document-1', $result->getSupplementaryDocuments()[0]->getId());

$this->assertCount(1, $result->getLivenessCapture());
$this->assertEquals('liveness-1', $result->getLivenessCapture()[0]->getId());

$this->assertCount(1, $result->getFaceCapture());
$this->assertEquals('face-capture-1', $result->getFaceCapture()[0]->getId());

$this->assertCount(1, $result->getShareCodes());
$this->assertEquals('share-code-1', $result->getShareCodes()[0]->getId());

$this->assertCount(1, $result->getApplicantProfiles());
$this->assertEquals('applicant-profile-1', $result->getApplicantProfiles()[0]->getId());
}

/**
* @test
* @covers ::filterForCheck
* @covers ::filterResourcesByIds
*/
public function shouldFilterForCheckAndReturnEmptyCollectionsWhenNoMatches(): void
{
$input = [
'id_documents' => [
['id' => 'id-document-1'],
],
];

$checkResponse = new CheckResponse(['resources_used' => ['someUnrelatedResourceId']]);

$result = (new ResourceContainer($input))->filterForCheck($checkResponse);

$this->assertCount(0, $result->getIdDocuments());
$this->assertCount(0, $result->getSupplementaryDocuments());
$this->assertCount(0, $result->getLivenessCapture());
$this->assertCount(0, $result->getFaceCapture());
$this->assertCount(0, $result->getShareCodes());
$this->assertCount(0, $result->getApplicantProfiles());
}

/**
* @test
* @covers ::filterForCheck
* @covers ::filterResourcesByIds
*/
public function shouldFilterForCheckAndReturnEmptyCollectionsWhenContainerHasNoResources(): void
{
$checkResponse = new CheckResponse(['resources_used' => ['someResourceId']]);

$result = (new ResourceContainer([]))->filterForCheck($checkResponse);

$this->assertCount(0, $result->getIdDocuments());
$this->assertCount(0, $result->getSupplementaryDocuments());
$this->assertCount(0, $result->getLivenessCapture());
$this->assertCount(0, $result->getFaceCapture());
$this->assertCount(0, $result->getShareCodes());
$this->assertCount(0, $result->getApplicantProfiles());
}
}
Loading