Skip to content

Commit

Permalink
Merge pull request #69 from fcastilloes/master
Browse files Browse the repository at this point in the history
Refactor and test runtime-call mappers
  • Loading branch information
fcastilloes committed Sep 8, 2017
2 parents 8e04cd8 + ce36e4c commit 8c166e5
Show file tree
Hide file tree
Showing 14 changed files with 699 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## Changed
- Refactor and test runtime-call mappers

## [1.2.2] - 2017-09-07
## Fixed
Expand Down
13 changes: 12 additions & 1 deletion src/Api/Factory/ServiceApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Katana\Sdk\Api\ActionApi;
use Katana\Sdk\Api\TypeCatalog;
use Katana\Sdk\Console\CliInput;
use Katana\Sdk\Mapper\CompactRuntimeCallMapper;
use Katana\Sdk\Mapper\CompactTransportMapper;
use Katana\Sdk\Mapper\ExtendedTransportMapper;
use Katana\Sdk\Messaging\MessagePackSerializer;
use Katana\Sdk\Messaging\RuntimeCaller\ZeroMQRuntimeCaller;
use Katana\Sdk\Schema\Mapping;
Expand Down Expand Up @@ -50,10 +52,19 @@ public function build(
$socket = new ZMQSocket($context, ZMQ::SOCKET_REQ);
$socket->setSockOpt(ZMQ::SOCKOPT_LINGER, 0);

if ($input->getMapping() === 'compact') {
$transportMapper = new CompactTransportMapper();
$runtimeCallMapper = new CompactRuntimeCallMapper($transportMapper);
} else {
$transportMapper = new ExtendedTransportMapper();
$runtimeCallMapper = new ExtendedTransportMapper($transportMapper);
}

$caller = new ZeroMQRuntimeCaller(
new MessagePackSerializer(),
new CompactTransportMapper(),
$socket
$socket,
$runtimeCallMapper
);

return new ActionApi(
Expand Down
3 changes: 2 additions & 1 deletion src/Api/Mapper/CompactPayloadMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
use Katana\Sdk\Api\Value\ReturnValue;
use Katana\Sdk\Api\Value\VersionString;
use Katana\Sdk\Mapper\CompactTransportMapper;
use Katana\Sdk\Mapper\TransportWriterInterface;

class CompactPayloadMapper implements PayloadMapperInterface
{
/**
* @var CompactTransportMapper
* @var TransportWriterInterface
*/
private $transportMapper;

Expand Down
102 changes: 102 additions & 0 deletions src/Mapper/CompactRuntimeCallMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* PHP 7 SDK for the KATANA(tm) Framework (http://katana.kusanagi.io)
* Copyright (c) 2016-2017 KUSANAGI S.L. All rights reserved.
*
* Distributed under the MIT license
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*
* @link https://github.com/kusanagi/katana-sdk-php7
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @copyright Copyright (c) 2016-2017 KUSANAGI S.L. (http://kusanagi.io)
*/

namespace Katana\Sdk\Mapper;

use Katana\Sdk\Api\Value\ActionTarget;
use Katana\Sdk\Api\Transport;
use Katana\Sdk\File;
use Katana\Sdk\Param;

class CompactRuntimeCallMapper implements RuntimeCallWriterInterface
{
/**
* @var TransportWriterInterface
*/
private $transportMapper;

/**
* @param TransportWriterInterface $transportMapper
*/
public function __construct(TransportWriterInterface $transportMapper)
{
$this->transportMapper = $transportMapper;
}

/**
* @param Param $param
* @return array
*/
private function writeParam(Param $param)
{
return [
'n' => $param->getName(),
'v' => $param->getValue(),
't' => $param->getType(),
];
}

/**
* @param File $file
* @return array
*/
private function writeFile(File $file)
{
return [
'n' => $file->getName(),
'p' => $file->getPath(),
't' => $file->getToken(),
'f' => $file->getFilename(),
's' => $file->getSize(),
'm' => $file->getMime()
];
}

/**
* @param string $action
* @param Transport $transport
* @param ActionTarget $target
* @param Param[] $params
* @param File[] $files
* @return array
*/
public function writeRuntimeCall(
string $action,
Transport $transport,
ActionTarget $target,
array $params = [],
array $files = []
): array {
return [
'c' => [
'n' => 'runtime-call',
'a' => [
'a' => $action,
'c' => [
$target->getService(),
$target->getVersion()->getVersion(),
$target->getAction()
],
'T' => $this->transportMapper->writeTransport($transport),
'p' => array_map([$this, 'writeParam'], $params),
'f' => array_map([$this, 'writeFile'], $files),
],
],
'm' => [
's' => 'service',
],
];
}
}
2 changes: 1 addition & 1 deletion src/Mapper/CompactTransportMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use Katana\Sdk\Api\TransportTransactions;
use Katana\Sdk\Api\Value\VersionString;

class CompactTransportMapper
class CompactTransportMapper implements TransportWriterInterface, TransportReaderInterface
{
/**
* @param array $param
Expand Down
102 changes: 102 additions & 0 deletions src/Mapper/ExtendedRuntimeCallMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* PHP 7 SDK for the KATANA(tm) Framework (http://katana.kusanagi.io)
* Copyright (c) 2016-2017 KUSANAGI S.L. All rights reserved.
*
* Distributed under the MIT license
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*
* @link https://github.com/kusanagi/katana-sdk-php7
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @copyright Copyright (c) 2016-2017 KUSANAGI S.L. (http://kusanagi.io)
*/

namespace Katana\Sdk\Mapper;

use Katana\Sdk\Api\Value\ActionTarget;
use Katana\Sdk\Api\Transport;
use Katana\Sdk\File;
use Katana\Sdk\Param;

class ExtendedRuntimeCallMapper implements RuntimeCallWriterInterface
{
/**
* @var TransportWriterInterface
*/
private $transportMapper;

/**
* @param TransportWriterInterface $transportMapper
*/
public function __construct(TransportWriterInterface $transportMapper)
{
$this->transportMapper = $transportMapper;
}

/**
* @param Param $param
* @return array
*/
private function writeParam(Param $param)
{
return [
'name' => $param->getName(),
'value' => $param->getValue(),
'type' => $param->getType(),
];
}

/**
* @param File $file
* @return array
*/
private function writeFile(File $file)
{
return [
'name' => $file->getName(),
'path' => $file->getPath(),
'token' => $file->getToken(),
'filename' => $file->getFilename(),
'size' => $file->getSize(),
'mime' => $file->getMime()
];
}

/**
* @param string $action
* @param Transport $transport
* @param ActionTarget $target
* @param Param[] $params
* @param File[] $files
* @return array
*/
public function writeRuntimeCall(
string $action,
Transport $transport,
ActionTarget $target,
array $params = [],
array $files = []
): array {
return [
'command' => [
'name' => 'runtime-call',
'arguments' => [
'action' => $action,
'callee' => [
$target->getService(),
$target->getVersion()->getVersion(),
$target->getAction()
],
'transport' => $this->transportMapper->writeTransport($transport),
'params' => array_map([$this, 'writeParam'], $params),
'files' => array_map([$this, 'writeFile'], $files),
],
],
'meta' => [
'scope' => 'service',
],
];
}
}
2 changes: 1 addition & 1 deletion src/Mapper/ExtendedTransportMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use Katana\Sdk\Api\TransportTransactions;
use Katana\Sdk\Api\Value\VersionString;

class ExtendedTransportMapper
class ExtendedTransportMapper implements TransportWriterInterface, TransportReaderInterface
{
/**
* @param array $param
Expand Down
40 changes: 40 additions & 0 deletions src/Mapper/RuntimeCallWriterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* PHP 7 SDK for the KATANA(tm) Framework (http://katana.kusanagi.io)
* Copyright (c) 2016-2017 KUSANAGI S.L. All rights reserved.
*
* Distributed under the MIT license
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code
*
* @link https://github.com/kusanagi/katana-sdk-php7
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @copyright Copyright (c) 2016-2017 KUSANAGI S.L. (http://kusanagi.io)
*/

namespace Katana\Sdk\Mapper;

use Katana\Sdk\Api\Transport;
use Katana\Sdk\Api\Value\ActionTarget;
use Katana\Sdk\File;
use Katana\Sdk\Param;

interface RuntimeCallWriterInterface
{
/**
* @param string $action
* @param Transport $transport
* @param ActionTarget $target
* @param Param[] $params
* @param File[] $files
* @return array
*/
public function writeRuntimeCall(
string $action,
Transport $transport,
ActionTarget $target,
array $params = [],
array $files = []
): array;
}
Loading

0 comments on commit 8c166e5

Please sign in to comment.