Skip to content
Closed
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
55 changes: 55 additions & 0 deletions src/Envelopes/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions src/Envelopes/MediaStorageValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Covery\Client\Envelopes;

class MediaStorageValidator
{
public static $dataValuesForContentType = array(
'image/jpeg', 'image/png', 'image/gif'
);

public static $dataValuesForContentDescription = array(
'personal_photo', 'linked_document', 'general_document'
);
}
24 changes: 24 additions & 0 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,28 @@ public static function sendCardId(CardIdInterface $cardId)
{
return self::getClient()->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);
}
}
32 changes: 32 additions & 0 deletions src/Media/MediaUploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Covery\Client\Media;

use Covery\Client\Requests\MediaStorageUploadStream;
use Covery\Client\Transport\Curl;
use GuzzleHttp\Psr7\Response;

class MediaUploader
{
/**
* @param $url
* @param $content
* @param $filename
* @return Response
* @throws \Covery\Client\IoException
* @throws \Covery\Client\TimeoutException
*/
public function uploadFile($url, $content, $filename)
{
if (!is_string($url)) {
throw new \InvalidArgumentException('Url must be string');
}

if (!is_string($content)) {
throw new \InvalidArgumentException('Content must be string');
}

$transport = new Curl(60);
return $transport->send(new MediaStorageUploadStream($url, $content, $filename));
}
}
17 changes: 17 additions & 0 deletions src/MediaConnectionBaseField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Covery\Client;

class MediaConnectionBaseField
{
const REQUEST_ID = 'requestId';
const MEDIA_ID = 'mediaId';

public static function getAll()
{
return [
self::REQUEST_ID,
self::MEDIA_ID
];
}
}
54 changes: 54 additions & 0 deletions src/MediaConnectionResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Covery\Client;

class MediaConnectionResult
{
/**
* @var int
*/
private $requestId;

/**
* @var string|array
*/
private $mediaId;

/**
* MediaConnectionResult constructor.
*
* @param $requestId
* @param $mediaId
*/
public function __construct(
$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->requestId = $requestId;
$this->mediaId = $mediaId;
}

/**
* @return int
*/
public function getRequestId()
{
return $this->requestId;
}

/**
* @return int|array
*/
public function getMediaId()
{
return $this->mediaId;
}
}
19 changes: 19 additions & 0 deletions src/MediaStorageBaseField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Covery\Client;

class MediaStorageBaseField
{
const CREATED_AT = 'createdAt';
const MEDIA_ID = 'mediaId';
const UPLOAD_URL = 'uploadUrl';

public static function getAll()
{
return [
self::CREATED_AT,
self::MEDIA_ID,
self::UPLOAD_URL
];
}
}
75 changes: 75 additions & 0 deletions src/MediaStorageResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Covery\Client;

class MediaStorageResult
{
/**
* @var string
*/
private $uploadUrl;

/**
* @var int
*/
private $mediaId;

/**
* @var int
*/
private $createdAt;

/**
* MediaStorageResult constructor.
*
* @param $uploadUrl
* @param $mediaId
* @param $createdAt
*/
public function __construct(
$uploadUrl,
$mediaId,
$createdAt

) {
if (!is_int($mediaId)) {
throw new \InvalidArgumentException('media ID must be integer');
}

if (!is_string($uploadUrl)) {
throw new \InvalidArgumentException('Upload Url must be string');
}

if (!is_int($createdAt)) {
throw new \InvalidArgumentException('Created At must be integer');
}

$this->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;
}
}
61 changes: 61 additions & 0 deletions src/PublicAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions src/Requests/AbstractEnvelopeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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)
Expand Down
Loading