Skip to content

Commit

Permalink
Fix type errors and support for PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Apr 17, 2022
1 parent 14678ab commit 2b4f8ea
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 78 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"baraja-core/bank-transaction-authorizator": "^2.0"
},
"require-dev": {
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
includes:
- vendor/spaze/phpstan-disallowed-calls/disallowed-dangerous-calls.neon
- vendor/spaze/phpstan-disallowed-calls/disallowed-execution-calls.neon

parameters:
ignoreErrors:
- '#^Short ternary operator is not allowed#'
36 changes: 16 additions & 20 deletions src/FioPaymentAuthorizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public function __construct(

public function process(): TransactionResult
{
return new TransactionResult($this->loadData());
return new TransactionResult($this->loadApiResult());
}


/**
* @return Transaction[]
* @return array<int, Transaction>
*/
public function getTransactions(): array
{
Expand All @@ -49,7 +49,7 @@ public function getDefaultCurrency(): string
}


private function loadData(): string
private function loadApiResult(): string
{
static $staticCache = [];

Expand All @@ -59,9 +59,12 @@ private function loadData(): string
$month = 12;
}

$url = 'https://www.fio.cz/ib_api/rest/periods/' . $this->privateKey
. '/' . $year . '-' . $month . '-01/' . date('Y-m-d')
. '/transactions.csv';
$url = sprintf(
'https://www.fio.cz/ib_api/rest/periods/%s/%s/%s/transactions.csv',
$this->privateKey,
sprintf('%d-%d-01', $year, $month),
date('Y-m-d'),
);

if (isset($staticCache[$url]) === true) {
return $staticCache[$url];
Expand All @@ -75,12 +78,10 @@ private function loadData(): string

$data = $this->safeDownload($url);
$staticCache[$url] = $data;
if ($this->cache !== null) {
$this->cache->save($url, $data, [
Cache::EXPIRE => '15 minutes',
Cache::TAGS => ['fio', 'bank', 'payment'],
]);
}
$this->cache?->save($url, $data, [
Cache::EXPIRE => '15 minutes',
Cache::TAGS => ['fio', 'bank', 'payment'],
]);

return $data;
}
Expand All @@ -97,7 +98,7 @@ private function normalize(string $s): string
if (class_exists('Normalizer', false)) {
$n = \Normalizer::normalize($s, \Normalizer::FORM_C);
if ($n !== false) {
$s = (string) $n;
$s = $n;
}
}

Expand All @@ -119,16 +120,11 @@ private function safeDownload(string $url): string
$data = (string) Callback::invokeSafe(
'file_get_contents',
[$url],
fn(string $message) => throw new FioPaymentException(
'Can not download data from URL "' . $url . '".'
. "\n" . 'Reported error: ' . $message,
),
static fn(string $message) => throw new FioPaymentException(sprintf('Can not download data from URL "%s". Reported error: %s', $url, $message)),
);
$data = $this->normalize($data);
if ($data === '') {
throw new FioPaymentException(
'Fio payment API response is empty, URL "' . $url . '" given. Is your API key valid?',
);
throw new FioPaymentException(sprintf('Fio payment API response is empty, URL "%s" given. Is your API key valid?', $url));
}
if (str_contains($data, '<status>error</status>')) {
throw new FioPaymentException(
Expand Down
61 changes: 31 additions & 30 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
namespace Baraja\FioPaymentAuthorizator;


use DateTime;
use DateTimeInterface;

final class Transaction implements \Baraja\BankTransferAuthorizator\Transaction
{
private int $id;

private DateTimeInterface $date;
private \DateTimeInterface $date;

private float $price;

Expand Down Expand Up @@ -51,32 +48,32 @@ final class Transaction implements \Baraja\BankTransferAuthorizator\Transaction

public function __construct(string $line, string $defaultCurrency = 'CZK')
{
$parser = explode(';', $line);
$part = explode(';', $line);

if (isset($parser[0]) && $parser[0]) {
$this->id = (int) $parser[0];
if (isset($part[0]) && $part[0] !== '') {
$this->id = (int) $part[0];
} else {
throw new \InvalidArgumentException('Transaction identifier is not defined.' . "\n\n" . 'Line: ' . $line);
throw new \InvalidArgumentException(sprintf("Transaction identifier is not defined.\n\nLine: %s", $line));
}

$this->date = new DateTime((string) ($parser[1] ?? 'now'));
$this->price = (float) str_replace(',', '.', $parser[2] ?? '0') ?: 0;
$this->currency = strtoupper(trim($parser[3] ?? '', '"')) ?: $defaultCurrency;
$this->toAccount = trim($parser[4] ?? '', '"') ?: null;
$this->toAccountName = trim($parser[5] ?? '', '"') ?: null;
$this->toBankCode = (int) ($parser[6] ?? null) ?: null;
$this->toBankName = trim($parser[7] ?? '', '"') ?: null;
$this->constantSymbol = (int) ($parser[8] ?? null) ?: null;
$this->variableSymbol = (int) ($parser[9] ?? null) ?: null;
$this->specificSymbol = (int) ($parser[10] ?? null) ?: null;
$this->userNotice = trim($parser[11] ?? '', '"') ?: null;
$this->toMessage = trim($parser[12] ?? '', '"') ?: null;
$this->type = trim($parser[13] ?? '', '"') ?: null;
$this->sender = trim($parser[14] ?? '', '"') ?: null;
$this->message = trim($parser[15] ?? '', '"') ?: null;
$this->comment = trim($parser[16] ?? '', '"') ?: null;
$this->bic = trim($parser[17] ?? '', '"') ?: null;
$this->idTransaction = (int) ($parser[18] ?? null) ?: null;
$this->date = new \DateTimeImmutable($part[1] ?? 'now');
$this->price = ((float) str_replace(',', '.', $part[2] ?? '0')) ?: 0;
$this->currency = strtoupper(trim($part[3] ?? '', '"')) ?: $defaultCurrency;
$this->toAccount = trim($part[4] ?? '', '"') ?: null;
$this->toAccountName = trim($part[5] ?? '', '"') ?: null;
$this->toBankCode = ((int) ($part[6] ?? null)) ?: null;
$this->toBankName = trim($part[7] ?? '', '"') ?: null;
$this->constantSymbol = ((int) ($part[8] ?? null)) ?: null;
$this->variableSymbol = ((int) ($part[9] ?? null)) ?: null;
$this->specificSymbol = ((int) ($part[10] ?? null)) ?: null;
$this->userNotice = trim($part[11] ?? '', '"') ?: null;
$this->toMessage = trim($part[12] ?? '', '"') ?: null;
$this->type = trim($part[13] ?? '', '"') ?: null;
$this->sender = trim($part[14] ?? '', '"') ?: null;
$this->message = trim($part[15] ?? '', '"') ?: null;
$this->comment = trim($part[16] ?? '', '"') ?: null;
$this->bic = trim($part[17] ?? '', '"') ?: null;
$this->idTransaction = ((int) ($part[18] ?? null)) ?: null;
}


Expand All @@ -88,9 +85,13 @@ public function isVariableSymbol(int $vs): bool

public function isContainVariableSymbolInMessage(int|string $vs): bool
{
$haystack = $this->userNotice . ' ' . $this->toMessage . ' ' . $this->message . ' ' . $this->comment;

return str_contains($haystack, (string) $vs);
return str_contains(sprintf(
'%s %s %s %s',
$this->userNotice,
$this->toMessage,
$this->message,
$this->comment,
), (string) $vs);
}


Expand All @@ -100,7 +101,7 @@ public function getId(): int
}


public function getDate(): DateTimeInterface
public function getDate(): \DateTimeInterface
{
return $this->date;
}
Expand Down
49 changes: 22 additions & 27 deletions src/TransactionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
namespace Baraja\FioPaymentAuthorizator;


use Nette\Utils\DateTime;

final class TransactionResult
{
/** @var Transaction[] */
/** @var array<int, Transaction> */
private array $transactions = [];

private int $accountId;
Expand All @@ -35,40 +33,37 @@ final class TransactionResult
private int $idTo;


public function __construct(string $data)
public function __construct(string $payload)
{
$parser = explode("\n", $data);
$lines = explode("\n", $payload);

if (isset($parser[10]) === false) {
throw new \InvalidArgumentException(
'Fio transaction data file is broken. File must define some variables.'
. "\n\n" . $data,
);
if (isset($lines[10]) === false) {
throw new \InvalidArgumentException(sprintf("Fio transaction data file is broken. File must define some variables.\n\n%s", $payload));
}

// Meta information parser
$line = static fn(string $line): string => explode(';', $line, 2)[1] ?? '';

$this->accountId = (int) $line($parser[0]);
$this->bankId = (int) $line($parser[1]);
$this->currency = strtoupper($line($parser[2]));
$this->iban = $line($parser[3]);
$this->bic = $line($parser[4]);
$this->openingBalance = (float) str_replace(',', '.', $line($parser[5]));
$this->closingBalance = (float) str_replace(',', '.', $line($parser[6]));
$this->dateStart = DateTime::from($line($parser[7]));
$this->dateEnd = DateTime::from($line($parser[8]));
$this->idFrom = (int) $line($parser[9]);
$this->idTo = (int) $line($parser[10]);

for ($i = 13; isset($parser[$i]); $i++) { // Transactions
$this->transactions[] = new Transaction($parser[$i]);
$lineParser = static fn(string $haystack): string => explode(';', $haystack, 2)[1] ?? '';

$this->accountId = (int) $lineParser($lines[0]);
$this->bankId = (int) $lineParser($lines[1]);
$this->currency = strtoupper($lineParser($lines[2]));
$this->iban = $lineParser($lines[3]);
$this->bic = $lineParser($lines[4]);
$this->openingBalance = (float) str_replace(',', '.', $lineParser($lines[5]));
$this->closingBalance = (float) str_replace(',', '.', $lineParser($lines[6]));
$this->dateStart = new \DateTimeImmutable($lineParser($lines[7]));
$this->dateEnd = new \DateTimeImmutable($lineParser($lines[8]));
$this->idFrom = (int) $lineParser($lines[9]);
$this->idTo = (int) $lineParser($lines[10]);

for ($i = 13; isset($lines[$i]); $i++) { // Transactions
$this->transactions[] = new Transaction($lines[$i]);
}
}


/**
* @return Transaction[]
* @return array<int, Transaction>
*/
public function getTransactions(): array
{
Expand Down

0 comments on commit 2b4f8ea

Please sign in to comment.