-
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / phpstan
|
||
{ | ||
public function __construct( | ||
private MathServiceInterface $mathService, | ||
private CastServiceInterface $castService, | ||
) { | ||
} | ||
|
||
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 GitHub Actions / phpstan
Check notice on line 42 in src/Services/TaxCollectionService.php GitHub Actions / Qodana for PHPPHPStan validation
|
||
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 GitHub Actions / phpstan
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |