Skip to content

Commit

Permalink
Feature New TaxInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Oct 10, 2024
1 parent e5e314e commit d3e6e2e
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/External/Enums/TransactionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\External\Enums;

use Bavix\Wallet\Models\Transaction;

enum TransactionType: string
{
case Deposit = Transaction::TYPE_DEPOSIT;
case Withdraw = Transaction::TYPE_WITHDRAW;
}
4 changes: 4 additions & 0 deletions src/Interfaces/MaximalTaxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Bavix\Wallet\Interfaces;

/**
* @deprecated Use TaxConstraintsInterface.
* @see TaxConstraintsInterface
*/
interface MaximalTaxable extends Taxable
{
/**
Expand Down
4 changes: 4 additions & 0 deletions src/Interfaces/MinimalTaxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Bavix\Wallet\Interfaces;

/**
* @deprecated Use TaxConstraintsInterface.
* @see TaxConstraintsInterface
*/
interface MinimalTaxable extends Taxable
{
/**
Expand Down
14 changes: 14 additions & 0 deletions src/Interfaces/TaxConstraintsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Interfaces;

use Bavix\Wallet\External\Enums\TransactionType;

interface TaxConstraintsInterface extends TaxInterface
{
public function getMinimumTax(TransactionType $enum): float|int|null;

public function getMaximumTax(TransactionType $enum): float|int|null;
}
12 changes: 12 additions & 0 deletions src/Interfaces/TaxInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Interfaces;

use Bavix\Wallet\External\Enums\TransactionType;

interface TaxInterface
{
public function getTaxPercent(TransactionType $enum): float|int;
}
4 changes: 4 additions & 0 deletions src/Interfaces/Taxable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Bavix\Wallet\Interfaces;

/**
* @deprecated Use TaxInterface.
* @see TaxInterface
*/
interface Taxable
{
/**
Expand Down
60 changes: 60 additions & 0 deletions src/Services/TaxCollectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Services;

use Bavix\Wallet\External\Enums\TransactionType;
use Bavix\Wallet\Interfaces\TaxConstraintsInterface;
use Bavix\Wallet\Interfaces\TaxInterface;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Internal\Service\MathServiceInterface;

class TaxCollectionService implements TaxCollectionServiceInterface

Check failure on line 13 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Bavix\Wallet\Services\TaxCollectionService is neither abstract nor final.

Check notice on line 13 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

PHPStan validation

phpstan:: Class Bavix\\Wallet\\Services\\TaxCollectionService is neither abstract nor final.
{
public function __construct(
private MathServiceInterface $mathService,

Check notice on line 16 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Property can be 'readonly'

Property can be 'readonly'
private CastServiceInterface $castService,

Check notice on line 17 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Property can be 'readonly'

Property can be 'readonly'
) {
}

public function calculate(
TransactionType $type,
Wallet $wallet,
float|int|string $amount,
): string {
$feePercent = null;
if ($wallet instanceof TaxInterface) {
$feePercent = $wallet->getTaxPercent($type);
}

$feeMinimum = null;
$feeMaximum = null;
if ($wallet instanceof TaxConstraintsInterface) {
$feeMinimum = $wallet->getMinimumTax($type);
$feeMaximum = $wallet->getMaximumTax($type);
}

$fee = '0';
if ($feePercent !== null) {
$fee = $this->mathService->floor(
$this->mathService->div(
$this->mathService->mul($amount, $feePercent, 0),

Check failure on line 42 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $first of method Bavix\Wallet\Internal\Service\MathServiceInterface::mul() expects float|int|non-empty-string, float|int|string given.

Check notice on line 42 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

PHPStan validation

phpstan:: Parameter #1 $first of method Bavix\\Wallet\\Internal\\Service\\MathServiceInterface::mul() expects float\|int\|non-empty-string, float\|int\|string given.
100,
$this->castService->getWallet($wallet)
->decimal_places
)
);
}

if ($feeMinimum !== null && $this->mathService->compare($fee, $feeMinimum) === -1) {
$fee = $feeMinimum;
}

if ($feeMaximum !== null && $this->mathService->compare($feeMaximum, $fee) === -1) {
$fee = $feeMaximum;
}

return $fee;

Check failure on line 58 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Bavix\Wallet\Services\TaxCollectionService::calculate() should return string but returns float|int|string.

Check notice on line 58 in src/Services/TaxCollectionService.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

PHPStan validation

phpstan:: Method Bavix\\Wallet\\Services\\TaxCollectionService::calculate() should return string but returns float\|int\|string.
}
}
17 changes: 17 additions & 0 deletions src/Services/TaxCollectionServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Services;

use Bavix\Wallet\External\Enums\TransactionType;
use Bavix\Wallet\Interfaces\Wallet;

interface TaxCollectionServiceInterface
{
public function calculate(
TransactionType $type,
Wallet $wallet,
float|int|string $amount,
): string;
}

0 comments on commit d3e6e2e

Please sign in to comment.