Skip to content

Commit

Permalink
implemented response value formatters and mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Oct 27, 2024
1 parent b845a28 commit 63b68a5
Show file tree
Hide file tree
Showing 78 changed files with 3,181 additions and 896 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ parameters:
count: 1
path: src/DataMapper/ResponseDataMapper/PayFlexV4PosResponseDataMapper.php

-
message: "#^Property Mews\\\\Pos\\\\DataMapper\\\\ResponseDataMapper\\\\AbstractResponseDataMapper\\:\\:\\$secureTypeMappings \\(array\\<string, '3d'\\|'3d_host'\\|'3d_pay'\\|'3d_pay_hosting'\\|'regular'\\>\\) does not accept non\\-empty\\-array\\<1\\|2\\|3\\|string, '3d'\\|'3d_host'\\|'3d_pay'\\|'3d_pay_hosting'\\|'regular'\\>\\.$#"
count: 1
path: src/DataMapper/ResponseDataMapper/PayFlexV4PosResponseDataMapper.php

-
message: "#^Method Mews\\\\Pos\\\\Factory\\\\PosFactory\\:\\:createPosGateway\\(\\) should return Mews\\\\Pos\\\\PosInterface but returns object\\.$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class KuveytPosRequestValueFormatter implements RequestValueFormatterInterface
* 0 => '0'
* 1 => '0'
* 2 => '2'
*
* @inheritDoc
*/
public function formatInstallment(int $installment): string
Expand All @@ -19,12 +20,9 @@ public function formatInstallment(int $installment): string


/**
* Amount Formatter
* converts 100 to 10000, or 10.01 to 1001
*
* @param float $amount
* example: 100 to 10000, or 10.01 to 1001
*
* @return int
* @inheritDoc
*/
public function formatAmount(float $amount): int
{
Expand All @@ -36,14 +34,14 @@ public function formatAmount(float $amount): int
*/
public function formatCardExpDate(\DateTimeInterface $expDate, string $fieldName): string
{
if ('CardExpireDateMonth' === $fieldName) {
return $expDate->format('m');
}

if ('CardExpireDateYear' === $fieldName) {
return $expDate->format('y');
}

if ('CardExpireDateMonth' === $fieldName) {
return $expDate->format('m');
}

throw new \InvalidArgumentException('Unsupported field name');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class KuveytPosRequestValueMapper extends AbstractRequestValueMapper
PosInterface::CURRENCY_TRY => '0949',
PosInterface::CURRENCY_USD => '0840',
PosInterface::CURRENCY_EUR => '0978',
PosInterface::CURRENCY_GBP => '0826',
PosInterface::CURRENCY_JPY => '0392',
PosInterface::CURRENCY_RUB => '0810',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,4 @@ class VakifKatilimPosRequestValueMapper extends AbstractRequestValueMapper
PosInterface::MODEL_3D_SECURE => '3',
PosInterface::MODEL_NON_SECURE => '5',
];

/**
* @inheritDoc
*
* @return string
*/
public function mapCurrency(string $currency): string
{
return (string) $this->currencyMappings[$currency];
}
}
101 changes: 15 additions & 86 deletions src/DataMapper/ResponseDataMapper/AbstractResponseDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Mews\Pos\DataMapper\ResponseDataMapper;

use Mews\Pos\DataMapper\ResponseValueFormatter\ResponseValueFormatterInterface;
use Mews\Pos\DataMapper\ResponseValueMapper\ResponseValueMapperInterface;
use Mews\Pos\PosInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -13,108 +15,35 @@ abstract class AbstractResponseDataMapper implements ResponseDataMapperInterface
/** @var string */
public const PROCEDURE_SUCCESS_CODE = '00';

protected LoggerInterface $logger;

/** @var array<string|int, PosInterface::CURRENCY_*> */
protected array $currencyMappings;
protected ResponseValueFormatterInterface $valueFormatter;

/** @var array<PosInterface::TX_TYPE_*, string|array<PosInterface::MODEL_*, string>> */
protected array $txTypeMappings;
protected ResponseValueMapperInterface $valueMapper;

/** @var array<string, PosInterface::MODEL_*> */
protected array $secureTypeMappings;
protected LoggerInterface $logger;

/**
* @param array<PosInterface::CURRENCY_*, string|int> $currencyMappings
* @param array<PosInterface::TX_TYPE_*, string|array<PosInterface::MODEL_*, string>> $txTypeMappings
* @param array<PosInterface::MODEL_*, string> $secureTypeMappings
* @param LoggerInterface $logger
* @param ResponseValueFormatterInterface $valueFormatter
* @param ResponseValueMapperInterface $valueMapper
* @param LoggerInterface $logger
*/
public function __construct(array $currencyMappings, array $txTypeMappings, array $secureTypeMappings, LoggerInterface $logger)
public function __construct(
ResponseValueFormatterInterface $valueFormatter,
ResponseValueMapperInterface $valueMapper,
LoggerInterface $logger
)
{
$this->logger = $logger;
$this->currencyMappings = \array_flip($currencyMappings);
$this->txTypeMappings = $txTypeMappings;
$this->secureTypeMappings = \array_flip($secureTypeMappings);
}

/**
* @return array<PosInterface::TX_TYPE_*, string|array<PosInterface::MODEL_*, string>>
*/
public function getTxTypeMappings(): array
{
return $this->txTypeMappings;
}

/**
* @param string|int $txType
*
* @return PosInterface::TX_*|null
*/
public function mapTxType($txType): ?string
{
foreach ($this->txTypeMappings as $mappedTxType => $mapping) {
if (\is_array($mapping) && \in_array($txType, $mapping, true)) {
return $mappedTxType;
}

if ($mapping === $txType) {
return $mappedTxType;
}
}

return null;
}

/**
* @param string|int $securityType
*
* @return PosInterface::MODEL_*|null
*/
public function mapSecurityType($securityType): ?string
{
return $this->secureTypeMappings[$securityType] ?? null;
$this->valueFormatter = $valueFormatter;
$this->valueMapper = $valueMapper;
}


/**
* @param string $mdStatus
*
* @return string
*/
abstract protected function mapResponseTransactionSecurity(string $mdStatus): string;

/**
* "1000.01" => 1000.01
* @param string $amount
*
* @return float
*/
protected function formatAmount(string $amount): float
{
return (float) $amount;
}

/**
* @param string $currency currency code that is accepted by bank
*
* @return PosInterface::CURRENCY_*|string
*/
protected function mapCurrency(string $currency): string
{
return $this->currencyMappings[$currency] ?? $currency;
}

/**
* @param string|null $installment
*
* @return int
*/
protected function mapInstallment(?string $installment): int
{
return (int) $installment;
}

/**
* if 2 arrays has common keys, then non-null value preferred,
* if both arrays has the non-null values for the same key then value of $arr2 is preferred.
Expand Down
Loading

0 comments on commit 63b68a5

Please sign in to comment.