Skip to content
Merged
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
56 changes: 56 additions & 0 deletions Tests/Unit/TerminalsBrowseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,60 @@ public function testStartWrongConfig()
$this->assertEquals('Something went wrong', $e->getFriendlyMessage());
}
}

/**
* @return void
*/
public function testPathParametersWithMerchantCode()
{
$request = new TerminalsBrowseRequest();
$request->setMerchantCode('M-1111-2222');

$params = $request->getPathParameters();

$this->assertArrayHasKey('merchant[eq]', $params);
$this->assertEquals('M-1111-2222', $params['merchant[eq]']);
$this->assertArrayNotHasKey('merchant[neq]', $params);
}

/**
* @return void
*/
public function testPathParametersWithExcludeMerchantCode()
{
$request = new TerminalsBrowseRequest();
$request->setExcludeMerchantCode('M-9999-8888');

$params = $request->getPathParameters();

$this->assertArrayHasKey('merchant[neq]', $params);
$this->assertEquals('M-9999-8888', $params['merchant[neq]']);
$this->assertArrayNotHasKey('merchant[eq]', $params);
}

/**
* @return void
*/
public function testPathParametersWithBoth()
{
$request = new TerminalsBrowseRequest();
$request
->setMerchantCode('M-1111-2222')
->setExcludeMerchantCode('M-9999-8888');

$params = $request->getPathParameters();

$this->assertEquals('M-1111-2222', $params['merchant[eq]']);
$this->assertEquals('M-9999-8888', $params['merchant[neq]']);
}

/**
* @return void
*/
public function testPathParametersEmpty()
{
$request = new TerminalsBrowseRequest();
$params = $request->getPathParameters();
$this->assertEmpty($params);
}
}
36 changes: 11 additions & 25 deletions src/Mapper/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ class Manager extends AbstractPluginManager
*/
protected $mapping = [];


/**
* @inheritDoc
*
* @throws ServiceNotCreatedException
* @throws ServiceNotFoundException
* @throws MapperSourceServiceNotFoundException
* @throws MapperTargetServiceNotFoundException
*
* @SuppressWarnings(PHPMD.StaticAccess)
* @param array $config
* @return ServiceManager
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function configure(array $config): ServiceManager
{
Expand Down Expand Up @@ -71,7 +68,7 @@ public function configure(array $config): ServiceManager
$mappingConfig[$mapper] = $map;
unset($mappingConfig[$mapperAlias]);

// determine the necessary managers
# Determine the necessary managers
preg_match_all('/((?:^|[A-Z])[a-z]+)/', Misc::getClassNameByFQN($mapper), $matches);
if (2 > count($matches[1])) {
throw new ServiceNotFoundException(
Expand All @@ -82,7 +79,7 @@ public function configure(array $config): ServiceManager
);
}

// determine the name of the source and target
# Determine the name of the source and target
$sourceManagerName = lcfirst(current($matches[1]));
next($matches[1]);
$targetManagerName = lcfirst(current($matches[1]));
Expand All @@ -93,22 +90,10 @@ public function configure(array $config): ServiceManager
# Check the map
foreach ($map as $source => $target) {
if ($sourceManager->has($source) === false) {
throw new MapperSourceServiceNotFoundException(
sprintf(
'Mapping source service with name "%s" not found in %s',
$source,
$sourceManagerName
)
);
throw new MapperSourceServiceNotFoundException(sprintf('Mapping source service with name "%s" not found in %s', $source, $sourceManagerName));
}
if ($targetManager->has($target) === false) {
throw new MapperTargetServiceNotFoundException(
sprintf(
'Mapping target service with name "%s" not found in %s',
$target,
$targetManagerName
)
);
throw new MapperTargetServiceNotFoundException(sprintf('Mapping target service with name "%s" not found in %s', $target, $targetManagerName));
}
}

Expand All @@ -121,7 +106,8 @@ public function configure(array $config): ServiceManager
}

/**
* @inheritDoc
* @param array $config
* @return void
*/
protected function validateOverrides(array $config): void
{
Expand Down
46 changes: 42 additions & 4 deletions src/Model/Request/TerminalsBrowseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,55 @@
*/
class TerminalsBrowseRequest extends RequestData
{
public function __construct()
protected string $merchantCode;
protected string $excludeMerchantCode;

/**
* @param string $merchantCode
* @param string $excludeMerchantCode
*/
public function __construct(string $merchantCode = '', string $excludeMerchantCode = '')
{
$this->setMerchantCode($merchantCode);
$this->setExcludeMerchantCode($excludeMerchantCode);
parent::__construct('TerminalsBrowse', '/terminals', RequestInterface::METHOD_GET);
}

/**
* @return array
* @param string $merchantCode
* @return $this
*/
public function setMerchantCode(string $merchantCode): self
{
$this->merchantCode = $merchantCode;
return $this;
}

/**
* @param string $excludeMerchantCode
* @return $this
*/
public function setExcludeMerchantCode(string $excludeMerchantCode): self
{
$this->excludeMerchantCode = $excludeMerchantCode;
return $this;
}

/**
* @return array|null[]
*/
public function getPathParameters(): array
{
return [];
$parameters = [];

if (!empty($this->merchantCode)) {
$parameters['merchant[eq]'] = $this->merchantCode;
}
if (!empty($this->excludeMerchantCode)) {
$parameters['merchant[neq]'] = $this->excludeMerchantCode;
}

return $parameters;
}

/**
Expand All @@ -47,4 +85,4 @@ public function start(): Terminals
$this->config->setVersion(2);
return parent::start();
}
}
}
39 changes: 13 additions & 26 deletions src/Request/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,44 +160,31 @@ public function hasParam($name): bool

/**
* @param array $params
*
* @throws MissingParamException
* @throws InvalidArgumentException
*
* @return static
* @return $this
*/
public function setParams(array $params): self
{
$this->params = $params;

foreach ($this->getRequiredParams() as $paramName => $paramDefinition) {
if (false === $this->hasParam($paramName)) {
throw new MissingParamException(sprintf('Missing param "%s"', $paramName));
}

if (true === is_string($paramDefinition) && '' !== $paramDefinition && 1 !== preg_match("/^{$paramDefinition}$/", $this->getParam($paramName))) {
throw new InvalidArgumentException(sprintf('Required param %s is not valid. It must match "%s"', $paramName, $paramDefinition));
}
$queryParams = [];
$uri = $this->getUri();

# Set it in the array
$this->setUri(str_replace("%{$paramName}%", $this->getParam($paramName), $this->getUri()));
}
foreach ($params as $key => $value) {
$placeholder = "%{$key}%";

$optionalParams = [];
foreach ($this->getOptionalParams() as $paramName => $paramDefinition) {
# If optional paramater is provided...
if (isset($params[$paramName])) {
if (true === is_string($paramDefinition) && '' !== $paramDefinition && 1 !== preg_match("/^{$paramDefinition}$/", $this->getParam($paramName))) {
throw new InvalidArgumentException(sprintf('Optional param %s is not valid. It must match "%s"', $paramName, $paramDefinition));
}
$optionalParams[$paramName] = $this->params[$paramName];
if (strpos($uri, $placeholder) !== false) {
$uri = str_replace($placeholder, $value, $uri);
} else {
$queryParams[$key] = $value;
}
}

if (!empty($optionalParams)) {
$this->setUri($this->getUri() . '?' . http_build_query($optionalParams));
if (!empty($queryParams)) {
$uri .= '?' . http_build_query($queryParams);
}

$this->setUri($uri);

return $this;
}

Expand Down
25 changes: 10 additions & 15 deletions src/Request/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class ConfigProvider implements ConfigProviderInterface
{
/**
* @inheritDoc
* @return array
*/
public function __invoke(): array
{
Expand All @@ -38,7 +38,7 @@ public function __invoke(): array
}

/**
* @inheritDoc
* @return array
*/
public function getDependencyConfig(): array
{
Expand All @@ -65,10 +65,10 @@ public function getRequestConfig(): array
DebugAwareInitializer::class,
],
'services' => array_merge(
$this->getIsPayServicesConfig(),
$this->getPinServicesConfig(),
$this->getServiceServicesConfig(),
$this->getTransactionServicesConfig(),
$this->getIsPayServicesConfig(),
$this->getPinServicesConfig(),
$this->getServiceServicesConfig(),
$this->getTransactionServicesConfig(),
),
'factories' => [
Request::class => Factory::class,
Expand Down Expand Up @@ -108,11 +108,7 @@ protected function getPinServicesConfig(): array
'method' => RequestInterface::METHOD_GET,
'requiredParams' => ['terminalCode' => ''],
],
'TerminalsBrowse' => [
'uri' => '/terminals',
'method' => RequestInterface::METHOD_GET,
'requiredParams' => [],
],
'TerminalsBrowse' => [],
'ConfirmTerminalTransaction' => [
'uri' => '/pin/%terminalTransactionId%/confirm',
'method' => RequestInterface::METHOD_PATCH,
Expand Down Expand Up @@ -206,13 +202,13 @@ protected function getTransactionServicesConfig(): array
'transactionId' => '',
],
],
'OrderUpdate' => [
'OrderUpdate' => [
'uri' => '/',
'method' => RequestInterface::METHOD_PATCH,
'requiredParams' => [
'transactionId' => '',
],
],
],
'OrderCapture' => [
'uri' => '',
'requiredParams' => [
Expand Down Expand Up @@ -241,7 +237,7 @@ protected function getTransactionServicesConfig(): array
'method' => RequestInterface::METHOD_GET,
'requiredParams' => [
'transactionId' => '',
],
],
],
'OrderStatus' => [
'uri' => '/transactions/%transactionId%/status',
Expand Down Expand Up @@ -280,5 +276,4 @@ protected function getTransactionServicesConfig(): array
]
];
}

}