diff --git a/src/Envelopes/Builder.php b/src/Envelopes/Builder.php index f02684b..e346c7a 100644 --- a/src/Envelopes/Builder.php +++ b/src/Envelopes/Builder.php @@ -4627,4 +4627,59 @@ public function addKycSubmitData( return $this; } + + /** + * Provides Media Storage value to envelope + * + * @param $contentType + * @param $contentDescription + * @param $fileName + * @return $this + */ + public function addMediaStorageData($contentType, $contentDescription, $fileName) + { + if (!is_string($contentType) and !in_array($contentType, MediaStorageValidator::$dataValuesForContentType)) { + throw new \InvalidArgumentException('contentType must be string and + must be one of options: "image/jpeg", "image/png", "image/gif"'); + } + + if (!is_string($contentDescription) and + !in_array($contentDescription, MediaStorageValidator::$dataValuesForContentDescription)) { + throw new \InvalidArgumentException('contentDescription must be string and + must be one of options: "personal_photo", "linked_document", "general_document"'); + } + + if (!is_string($fileName)) { + throw new \InvalidArgumentException('fileName must be string'); + } + + $this->replace('content_type', $contentType); + $this->replace('content_description', $contentDescription); + $this->replace('file_name', $fileName); + + return $this; + } + + /** + * Provides Media Connection value to envelope + * + * @param int $requestId + * @param int|array $mediaId + * @return $this + */ + public function addMediaConnectionData($requestId, $mediaId) + { + if (!is_int($requestId)) { + throw new \InvalidArgumentException('Request ID must be integer'); + } + + if (!is_int($mediaId) and !is_array($mediaId)) { + throw new \InvalidArgumentException('media ID must be integer or array'); + } + + $this->replace('request_id', $requestId); + $this->replace('media_id', $mediaId); + + return $this; + } } diff --git a/src/Envelopes/MediaStorageValidator.php b/src/Envelopes/MediaStorageValidator.php new file mode 100644 index 0000000..e31fe32 --- /dev/null +++ b/src/Envelopes/MediaStorageValidator.php @@ -0,0 +1,14 @@ +sendCardId($cardId); } + + /** + * Sends envelope to Covery for analysis + * + * @param EnvelopeInterface $envelope + * @return Result + * @throws Exception + */ + public static function sendMediaStorage(EnvelopeInterface $envelope) + { + return self::getClient()->sendMediaStorage($envelope); + } + + /** + * Sends envelope to Covery for analysis + * + * @param EnvelopeInterface $envelope + * @return Result + * @throws Exception + */ + public static function sendMediaConnection(EnvelopeInterface $envelope) + { + return self::getClient()->sendMediaConnection($envelope); + } } diff --git a/src/Media/MediaUploader.php b/src/Media/MediaUploader.php new file mode 100644 index 0000000..6500165 --- /dev/null +++ b/src/Media/MediaUploader.php @@ -0,0 +1,32 @@ +send(new MediaStorageUploadStream($url, $content, $filename)); + } +} diff --git a/src/MediaConnectionBaseField.php b/src/MediaConnectionBaseField.php new file mode 100644 index 0000000..74feb0f --- /dev/null +++ b/src/MediaConnectionBaseField.php @@ -0,0 +1,17 @@ +requestId = $requestId; + $this->mediaId = $mediaId; + } + + /** + * @return int + */ + public function getRequestId() + { + return $this->requestId; + } + + /** + * @return int|array + */ + public function getMediaId() + { + return $this->mediaId; + } +} diff --git a/src/MediaStorageBaseField.php b/src/MediaStorageBaseField.php new file mode 100644 index 0000000..7cfb64b --- /dev/null +++ b/src/MediaStorageBaseField.php @@ -0,0 +1,19 @@ +uploadUrl = $uploadUrl; + $this->mediaId = $mediaId; + $this->createdAt = $createdAt; + } + + /** + * @return int + */ + public function getMediaId() + { + return $this->mediaId; + } + + /** + * @return string + */ + public function getUploadUrl() + { + return $this->uploadUrl; + } + + /** + * @return int + */ + public function getCreatedAt() + { + return $this->createdAt; + } +} diff --git a/src/PublicAPIClient.php b/src/PublicAPIClient.php index a083a55..e83568c 100644 --- a/src/PublicAPIClient.php +++ b/src/PublicAPIClient.php @@ -7,6 +7,8 @@ use Covery\Client\Requests\Decision; use Covery\Client\Requests\Event; use Covery\Client\Requests\KycProof; +use Covery\Client\Requests\mediaStorage; +use Covery\Client\Requests\mediaСonnection; use Covery\Client\Requests\Ping; use Covery\Client\Requests\Postback; use Psr\Http\Message\RequestInterface; @@ -343,4 +345,63 @@ public function sendCardId(CardIdInterface $cardId) $data[CardIdResultBaseField::CREATED_AT] ); } + + /** + * Sends envelope to Covery for analysis + * + * @param EnvelopeInterface $envelope + * @return MediaStorageResult + * @throws Exception + */ + public function sendMediaStorage(EnvelopeInterface $envelope) + { + // Validating + $this->validator->validate($envelope); + + // Sending + $data = $this->readJson($this->send(new mediaСonnection($envelope))); + + if (!is_array($data)) { + throw new Exception("Malformed response"); + } + + try { + return new MediaStorageResult( + $data[MediaStorageBaseField::UPLOAD_URL], + $data[MediaStorageBaseField::MEDIA_ID], + $data[MediaStorageBaseField::CREATED_AT] + ); + } catch (\Exception $error) { + throw new Exception('Malformed response', 0, $error); + } + } + + /** + * Sends envelope to Covery for analysis + * + * @param EnvelopeInterface $envelope + * @return MediaConnectionResult + * @throws Exception + */ + public function sendMediaConnection(EnvelopeInterface $envelope) + { + // Validating + $this->validator->validate($envelope); + + // Sending + $data = $this->readJson($this->send(new mediaСonnection($envelope))); + + if (!is_array($data)) { + throw new Exception("Malformed response"); + } + + try { + return new MediaConnectionResult( + $data[MediaConnectionBaseField::REQUEST_ID], + $data[MediaConnectionBaseField::MEDIA_ID] + ); + } catch (\Exception $error) { + throw new Exception('Malformed response', 0, $error); + } + } } diff --git a/src/Requests/AbstractEnvelopeRequest.php b/src/Requests/AbstractEnvelopeRequest.php index 7ca1d38..edc64db 100644 --- a/src/Requests/AbstractEnvelopeRequest.php +++ b/src/Requests/AbstractEnvelopeRequest.php @@ -14,7 +14,7 @@ */ abstract class AbstractEnvelopeRequest extends Request { - public function __construct($apiPath, EnvelopeInterface $envelope) + public function __construct($apiPath, EnvelopeInterface $envelope, $method = 'POST') { if (!is_string($apiPath) || empty($apiPath)) { throw new \InvalidArgumentException('API path must be non-empty string'); @@ -29,7 +29,7 @@ public function __construct($apiPath, EnvelopeInterface $envelope) $packet = array('type' => $envelope->getType(), 'sequence_id' => $envelope->getSequenceId()); parent::__construct( - 'POST', + $method, $apiPath, array( 'X-Identities' => implode('&', $ids) diff --git a/src/Requests/MediaStorage.php b/src/Requests/MediaStorage.php new file mode 100644 index 0000000..c4a64c1 --- /dev/null +++ b/src/Requests/MediaStorage.php @@ -0,0 +1,13 @@ + 'application/octet-stream', + 'fileName' => $filename + ), + $stream + ); + } +} diff --git "a/src/Requests/Media\320\241onnection.php" "b/src/Requests/Media\320\241onnection.php" new file mode 100644 index 0000000..0a9e26b --- /dev/null +++ "b/src/Requests/Media\320\241onnection.php" @@ -0,0 +1,13 @@ +hasHeader('X-Auth-Nonce')); self::assertSame('/api/kycProof', strval($req->getUri())); } + + public function testMediaStorage() + { + $contentType = 'image/jpeg'; + $contentDescription = 'personal_photo'; + $fileContent = "test.jpeg"; + + $noIdentities = new \Covery\Client\Envelopes\Builder('foo', 'bar'); + $noIdentities = $noIdentities->addMediaStorageData($contentType, $contentDescription, $fileContent) + ->build(); + $withStub = new \Covery\Client\Envelopes\Builder('baz', 'yolo'); + $stub = new \Covery\Client\Identities\Stub(); + $withStub = $withStub->addIdentity($stub) + ->addWebsiteData('google.com') + ->addMediaStorageData($contentType, $contentDescription, $fileContent) + ->build(); + + $req = new \Covery\Client\Requests\MediaStorage($noIdentities); + self::assertInstanceOf('Psr\Http\Message\RequestInterface', $req); + self::assertSame( + '{"type":"foo","sequence_id":"bar","content_type":"image\/jpeg","content_description":"personal_photo","file_name":"test.jpeg"}', + $req->getBody()->getContents()); + self::assertSame('POST', $req->getMethod()); + self::assertSame('', $req->getHeaderLine('X-Identities')); + self::assertFalse($req->hasHeader('X-Auth-Token')); + self::assertFalse($req->hasHeader('X-Auth-Signature')); + self::assertFalse($req->hasHeader('X-Auth-Nonce')); + self::assertSame('/api/mediaStorage', strval($req->getUri())); + + $req = new \Covery\Client\Requests\MediaStorage($withStub); + self::assertInstanceOf('Psr\Http\Message\RequestInterface', $req); + self::assertSame( + '{"type":"baz","sequence_id":"yolo","website_url":"google.com","content_type":"image\/jpeg","content_description":"personal_photo","file_name":"test.jpeg"}', + $req->getBody()->getContents()); + self::assertSame('POST', $req->getMethod()); + self::assertSame($stub->getType() . '=' . $stub->getId(), $req->getHeaderLine('X-Identities')); + self::assertFalse($req->hasHeader('X-Auth-Token')); + self::assertFalse($req->hasHeader('X-Auth-Signature')); + self::assertFalse($req->hasHeader('X-Auth-Nonce')); + self::assertSame('/api/mediaStorage', strval($req->getUri())); + } + + public function testMediaConnection() + { + $requestId = 22222; + $mediaId = [11111]; + + $noIdentities = new \Covery\Client\Envelopes\Builder('foo', 'bar'); + $noIdentities = $noIdentities->addMediaConnectionData($requestId, $mediaId)->build(); + $withStub = new \Covery\Client\Envelopes\Builder('baz', 'yolo'); + $stub = new \Covery\Client\Identities\Stub(); + $withStub = $withStub->addIdentity($stub) + ->addMediaConnectionData($requestId, $mediaId) + ->addWebsiteData('google.com') + ->build(); + + $req = new \Covery\Client\Requests\MediaСonnection($noIdentities); + self::assertInstanceOf('Psr\Http\Message\RequestInterface', $req); + self::assertSame('{"type":"foo","sequence_id":"bar","request_id":22222,"media_id":[11111]}', + $req->getBody()->getContents()); + self::assertSame('PUT', $req->getMethod()); + self::assertSame('', $req->getHeaderLine('X-Identities')); + self::assertFalse($req->hasHeader('X-Auth-Token')); + self::assertFalse($req->hasHeader('X-Auth-Signature')); + self::assertFalse($req->hasHeader('X-Auth-Nonce')); + self::assertSame('/api/mediaConnection', strval($req->getUri())); + + $req = new \Covery\Client\Requests\MediaСonnection($withStub); + self::assertInstanceOf('Psr\Http\Message\RequestInterface', $req); + self::assertSame( + '{"type":"baz","sequence_id":"yolo","request_id":22222,"media_id":[11111],"website_url":"google.com"}', + $req->getBody()->getContents()); + self::assertSame('PUT', $req->getMethod()); + self::assertSame($stub->getType() . '=' . $stub->getId(), $req->getHeaderLine('X-Identities')); + self::assertFalse($req->hasHeader('X-Auth-Token')); + self::assertFalse($req->hasHeader('X-Auth-Signature')); + self::assertFalse($req->hasHeader('X-Auth-Nonce')); + self::assertSame('/api/mediaConnection', strval($req->getUri())); + } }