Skip to content

Commit

Permalink
CS fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly committed Jan 10, 2024
1 parent c38730d commit b16636b
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 24 deletions.
8 changes: 2 additions & 6 deletions .dev-tools/.php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

require __DIR__.'/vendor/tpay-com/coding-standards/bootstrap.php';

$config = Tpay\CodingStandards\PhpCsFixerConfigFactory::createWithLegacyRules()
$config = Tpay\CodingStandards\PhpCsFixerConfigFactory::createWithNonRiskyRules()
->setFinder(
PhpCsFixer\Finder::create()
->ignoreDotFiles(false)
->in(__DIR__.'/..')
);

$rules = $config->getRules();

$rules['nullable_type_declaration_for_default_null_value'] = false;

return $config->setRules($rules);
return $config;
1 change: 0 additions & 1 deletion .dev-tools/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ parameters:
paths:
- ../
excludePaths:
- ../Controller/tpay/Notification.php
- ../vendor/
2 changes: 1 addition & 1 deletion Model/ApiFacade/CardTransaction/CardOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private function createNewCardPayment(array $additionalPaymentInformation): arra
private function validateNon3dsSign(array $tpayResponse)
{
$testMode = isset($tpayResponse['test_mode']) ? '1' : '';
$cliAuth = isset($tpayResponse['cli_auth']) ? $tpayResponse['cli_auth'] : '';
$cliAuth = $tpayResponse['cli_auth'] ?? '';
$localHash = hash(
$this->tpay->getHashType(),
$testMode
Expand Down
22 changes: 20 additions & 2 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace tpaycom\magento2basic\Model\ApiFacade;

use Magento\Payment\Model\InfoInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\Dto\Channel;
use tpaySDK\Api\TpayApi;

class OpenApi extends TpayApi
Expand All @@ -25,12 +26,29 @@ public function createWithInstantRedirect(array $data): array

public function makeRefund(InfoInterface $payment, float $amount): array
{
return $this->transactions()->createRefundByTransactionId(['amount' => $amount], $payment->getAdditionalInformation('transaction_id'));
return $this->transactions()->createRefundByTransactionId(
['amount' => $amount],
$payment->getAdditionalInformation('transaction_id')
);
}

public function channels(): array
{
return $this->transactions()->getChannels();
$result = $this->transactions()->getChannels();

return array_map(function (array $channel) {
return new Channel(
$channel['id'],
$channel['name'],
$channel['fullName'],
$channel['image']['url'],
$channel['available'],
$channel['onlinePayment'],
$channel['instantRedirection'],
$channel['groups'],
$channel['constraints']
);
}, $result['channels'] ?? []);
}

private function handleDataStructure(array $data): array
Expand Down
57 changes: 57 additions & 0 deletions Model/ApiFacade/Transaction/Dto/Channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace tpaycom\magento2basic\Model\ApiFacade\Transaction\Dto;

class Channel
{
/** @var int */
public $id;

/** @var string */
public $name;

/** @var string */
public $fullName;

/** @var string */
public $image;

/** @var bool */
public $available;

/** @var bool */
public $onlinePayment;

/** @var bool */
public $instantRedirection;

/** @var array */
public $groups;

/** @var array */
public $constraints;

public function __construct(
int $id,
string $name,
string $fullName,
string $image,
bool $available,
bool $onlinePayment,
bool $instantRedirection,
array $groups,
array $constraints
) {
$this->id = $id;
$this->name = $name;
$this->fullName = $fullName;
$this->image = $image;
$this->available = $available;
$this->onlinePayment = $onlinePayment;
$this->instantRedirection = $instantRedirection;
$this->groups = $groups;
$this->constraints = $constraints;
}
}
10 changes: 6 additions & 4 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Tpay\OpenApi\Utilities\TpayException;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\OpenApi;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\Dto\Channel;

class TransactionApiFacade
{
Expand Down Expand Up @@ -56,23 +57,24 @@ public function blik($blikTransactionId, $blikCode): array
return $this->originApi->blik($blikTransactionId, $blikCode);
}

/** @return list<Channel> */
public function channels(): array
{
$channels = $this->cache->load(self::CHANNELS_CACHE_KEY);

if ($channels) {
return json_decode($channels, true);
return unserialize($channels);
}

if (false === $this->useOpenApi) {
return [];
}

$channels = array_filter($this->openApi->channels()['channels'], function (array $channel) {
return true === $channel['available'] && true === empty($channel['constraints']);
$channels = array_filter($this->openApi->channels(), function (Channel $channel) {
return true === $channel->available && true === empty($channel->constraints);
});

$this->cache->save(json_encode($channels), self::CHANNELS_CACHE_KEY, [], self::CACHE_LIFETIME);
$this->cache->save(serialize($channels), self::CHANNELS_CACHE_KEY, [], self::CACHE_LIFETIME);

return $channels;
}
Expand Down
2 changes: 1 addition & 1 deletion Model/ApiFacade/Transaction/TransactionOriginApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TransactionOriginApi extends PaymentBlik
{
const BLIK_CHANNEL = 150;
public const BLIK_CHANNEL = 150;

/**
* @param string $apiPassword
Expand Down
5 changes: 3 additions & 2 deletions Model/Config/Source/OnsiteChannels.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Data\OptionSourceInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\Dto\Channel;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;

class OnsiteChannels implements OptionSourceInterface
Expand All @@ -31,8 +32,8 @@ public function getLabelFromValue(int $value): ?string
/** @return array{array{value: int, label: string}} */
public function toOptionArray(): array
{
return array_map(function (array $channel) {
return ['value' => (int) $channel['id'], 'label' => $channel['fullName']];
return array_map(function (Channel $channel) {
return ['value' => $channel->id, 'label' => $channel->fullName];
}, $this->transactions->channels());
}
}
3 changes: 2 additions & 1 deletion Model/GenericPaymentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class GenericPaymentPlugin
{
public function beforeImportData(Payment $compiled, array $data): array
{
if (str_contains($data['method'], 'generic')) {
if ('generic' === substr($data['method'], 0, 7)) {
$data['channel'] = explode('-', $data['method'])[1];
$data['method'] = TpayInterface::CODE;
}

if ('tpaycom_magento2basic_cards' == $data['method']) {
$data['method'] = TpayInterface::CODE;
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function assignData(DataObject $data)
}

// KARTY
$info->setAdditionalInformation(static::CARDDATA, isset($additionalData[static::CARDDATA]) ? $additionalData[static::CARDDATA] : '');
$info->setAdditionalInformation(static::CARDDATA, $additionalData[static::CARDDATA] ?? '');
$info->setAdditionalInformation(static::CARD_VENDOR, isset($additionalData[static::CARD_VENDOR]) && in_array($additionalData[static::CARD_VENDOR], $this->supportedVendors) ? $additionalData[static::CARD_VENDOR] : 'undefined');
$info->setAdditionalInformation(static::CARD_SAVE, isset($additionalData[static::CARD_SAVE]) ? '1' === $additionalData[static::CARD_SAVE] : false);
$info->setAdditionalInformation(static::CARD_ID, isset($additionalData[static::CARD_ID]) && is_numeric($additionalData[static::CARD_ID]) ? $additionalData[static::CARD_ID] : false);
Expand Down
8 changes: 4 additions & 4 deletions Model/TpayConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public function getConfig(): array
$channels = $this->transactionApi->channels();

foreach ($channels as $channel) {
$config['generic'][$channel['id']] = [
'id' => $channel['id'],
'name' => $channel['fullName'],
'logoUrl' => $channel['image']['url'],
$config['generic'][$channel->id] = [
'id' => $channel->id,
'name' => $channel->fullName,
'logoUrl' => $channel->image,
];
}

Expand Down
2 changes: 1 addition & 1 deletion Service/TpayTokensService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TpayTokensService extends Tokens
/** @var ResourceConnection */
private $resourceConnection;

public function __construct(Context $context, Registry $registry, ResourceConnection $resourceConnection, $resource = null, AbstractDb $resourceCollection = null, array $data = [])
public function __construct(Context $context, Registry $registry, ResourceConnection $resourceConnection, $resource = null, ?AbstractDb $resourceCollection = null, array $data = [])
{
$this->resourceConnection = $resourceConnection;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
Expand Down

0 comments on commit b16636b

Please sign in to comment.