Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| TaxService | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTaxValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addTax | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTaxFromTotal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBaseFromTotal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Service; |
| 6 | |
| 7 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 8 | |
| 9 | class TaxService |
| 10 | { |
| 11 | private readonly float $taxMultiplier; |
| 12 | |
| 13 | public function __construct( |
| 14 | #[Autowire('%env(float:VALUE_IVA)%')] |
| 15 | private readonly float $taxRate, |
| 16 | ) |
| 17 | { |
| 18 | $this->taxMultiplier = 1 + $this->taxRate / 100; |
| 19 | } |
| 20 | |
| 21 | public function getTaxValue(): float |
| 22 | { |
| 23 | return $this->taxRate; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Calculate total with tax from base value. |
| 28 | * Example: 100 with 12% tax = 112 |
| 29 | */ |
| 30 | public function addTax(float $baseValue): float |
| 31 | { |
| 32 | return round($baseValue * $this->taxMultiplier, 2); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Extract tax amount from total value. |
| 37 | * Example: 112 total with 12% tax = 12 tax |
| 38 | */ |
| 39 | public function getTaxFromTotal(float $total): float |
| 40 | { |
| 41 | return round($total - $this->getBaseFromTotal($total), 2); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Extract base value from total with tax. |
| 46 | * Example: 112 total with 12% tax = 100 base |
| 47 | */ |
| 48 | public function getBaseFromTotal(float $total): float |
| 49 | { |
| 50 | return round($total / $this->taxMultiplier, 2); |
| 51 | } |
| 52 | } |