Skip to content

Commit

Permalink
Fix getSource() in ResourceResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
LuborRod committed Oct 28, 2022
1 parent 1d857fa commit daa5e51
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/DocScan/Session/Retrieve/FaceCaptureImageResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@

namespace Yoti\DocScan\Session\Retrieve;

use Yoti\Exception\DateTimeException;

class FaceCaptureImageResponse
{
/**
* @var MediaResponse $media
* @var null|MediaResponse $media
*/
private $media;

/**
* @return MediaResponse
* @param array<string,mixed> $image
* @throws DateTimeException
*/
public function __construct(array $image)
{
if (isset($image['media'])) {
$this->media = new MediaResponse($image['media']);
}
}

/**
* @return null|MediaResponse
*/
public function getMedia(): MediaResponse
public function getMedia(): ?MediaResponse
{
return $this->media;
}
Expand Down
16 changes: 16 additions & 0 deletions src/DocScan/Session/Retrieve/FaceCaptureResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

namespace Yoti\DocScan\Session\Retrieve;

use Yoti\Exception\DateTimeException;

class FaceCaptureResourceResponse extends ResourceResponse
{
/**
* @var FaceCaptureImageResponse $image
*/
private $image;

/**
* FaceCaptureResourceResponse constructor.
* @param array<string, mixed> $faceCaptures
* @throws DateTimeException
*/
public function __construct(array $faceCaptures)
{
parent::__construct($faceCaptures);

if (isset($faceCaptures['image'])) {
$this->image = new FaceCaptureImageResponse($faceCaptures['image']);
}
}

/**
* @return FaceCaptureImageResponse
*/
Expand Down
41 changes: 38 additions & 3 deletions src/DocScan/Session/Retrieve/ResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@

use Yoti\DocScan\Constants;
use Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\AllowedSourceResponse;
use Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\EndUserAllowedSourceResponse;
use Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\IbvAllowedSourceResponse;
use Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\RelyingBusinessAllowedSourceResponse;
use Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\UnknownAllowedSourceResponse;

class ResourceResponse
{
/**
* @var string|null
*/
private $id;

/**
* @var TaskResponse[]
*/
private $tasks = [];
/**
* @var AllowedSourceResponse
*/
private $source;

/**
* ResourceResponse constructor.
Expand All @@ -24,12 +31,15 @@ class ResourceResponse
public function __construct(array $resource)
{
$this->id = $resource['id'] ?? null;

if (isset($resource['tasks'])) {
foreach ($resource['tasks'] as $task) {
$this->tasks[] = $this->createTaskFromArray($task);
}
}

if (isset($resource['source']['type'])) {
$this->source = $this->createSourceFromType($resource['source']['type']);
}
}

/**
Expand All @@ -48,6 +58,14 @@ public function getTasks(): array
return $this->tasks;
}

/**
* @return AllowedSourceResponse
*/
public function getSource(): AllowedSourceResponse
{
return $this->source;
}

/**
* @return TaskResponse[]
*/
Expand All @@ -68,7 +86,6 @@ function ($taskResponse) use ($class): bool {
return $taskResponse instanceof $class;
}
);

return array_values($filtered);
}

Expand All @@ -87,4 +104,22 @@ private function createTaskFromArray(array $task): TaskResponse
return new TaskResponse($task);
}
}

/**
* @param string $type
* @return AllowedSourceResponse
*/
private function createSourceFromType(string $type): AllowedSourceResponse
{
switch ($type ?? null) {
case Constants::END_USER:
return new EndUserAllowedSourceResponse();
case Constants::IBV:
return new IbvAllowedSourceResponse();
case Constants::RELYING_BUSINESS:
return new RelyingBusinessAllowedSourceResponse();
default:
return new UnknownAllowedSourceResponse();
}
}
}
54 changes: 54 additions & 0 deletions tests/DocScan/Session/Retrieve/FaceCaptureResourceResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Yoti\Test\DocScan\Session\Retrieve;

use Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\AllowedSourceResponse;
use Yoti\DocScan\Session\Retrieve\FaceCaptureImageResponse;
use Yoti\DocScan\Session\Retrieve\FaceCaptureResourceResponse;
use Yoti\Test\TestCase;

/**
* @coversDefaultClass \Yoti\DocScan\Session\Retrieve\FaceCaptureResourceResponse
*/
class FaceCaptureResourceResponseTest extends TestCase
{
private const RELYING_BUSINESS = 'RELYING_BUSINESS';
private const SOME_ID = '493ru49358gh945fh305';

/**
* @test
* @covers ::__construct
* @covers ::getImage
* @covers \Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\AllowedSourceResponse::getType
* @covers \Yoti\DocScan\Session\Retrieve\ResourceResponse::getSource
* @covers \Yoti\DocScan\Session\Retrieve\MediaResponse::getId
* @covers \Yoti\DocScan\Session\Retrieve\FaceCaptureImageResponse::getMedia
* @covers \Yoti\DocScan\Session\Retrieve\FaceCaptureImageResponse::__construct
*/
public function shouldBuildCorrectly()
{
$input = [
'tasks' => [],
'source' => [
'type' => self::RELYING_BUSINESS
],
'image' => [
'media' => [
'id' => self::SOME_ID,
'type' => 'IMAGE',
'created' => '2021-06-11T11:39:24Z',
'last_updated' => '2021-06-11T11:39:24Z',
]
],

];

$result = new FaceCaptureResourceResponse($input);

$this->assertEquals(self::RELYING_BUSINESS, $result->getSource()->getType());
$this->assertInstanceOf(AllowedSourceResponse::class, $result->getSource());

$this->assertEquals(self::SOME_ID, $result->getImage()->getMedia()->getId());
$this->assertInstanceOf(FaceCaptureImageResponse::class, $result->getImage());
}
}
21 changes: 21 additions & 0 deletions tests/DocScan/Session/Retrieve/ResourceResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yoti\Test\DocScan\Session\Retrieve;

use Yoti\DocScan\Session\Retrieve\Configuration\Capture\Source\AllowedSourceResponse;
use Yoti\DocScan\Session\Retrieve\ResourceResponse;
use Yoti\DocScan\Session\Retrieve\TaskResponse;
use Yoti\DocScan\Session\Retrieve\TextExtractionTaskResponse;
Expand All @@ -18,6 +19,7 @@ class ResourceResponseTest extends TestCase
private const SUPPLEMENTARY_DOCUMENT_TEXT_DATA_EXTRACTION = 'SUPPLEMENTARY_DOCUMENT_TEXT_DATA_EXTRACTION';
private const SOME_UNKNOWN_TASK = 'someUnknownTask';
private const SOME_ID = 'someId';
private const RELYING_BUSINESS = 'RELYING_BUSINESS';

/**
* @test
Expand Down Expand Up @@ -113,4 +115,23 @@ public function shouldNotThrowExceptionWhenMissingValues()
$this->assertNull($result->getId());
$this->assertCount(0, $result->getTasks());
}


/**
* @test
* @covers ::__construct
* @covers ::createSourceFromType
*/
public function shouldCreateSourceCorrectly()
{
$input = [
'source' => [
'type' => self::RELYING_BUSINESS
]
];

$result = new ResourceResponse($input);

$this->assertInstanceOf(AllowedSourceResponse::class, $result->getSource());
}
}

0 comments on commit daa5e51

Please sign in to comment.