1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
7
|
|
|
* an email on [email protected]. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Resolver; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
15
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface; |
16
|
|
|
use Sylius\Component\Core\Model\ShippingMethodInterface; |
17
|
|
|
use Sylius\Component\Taxation\Calculator\CalculatorInterface; |
18
|
|
|
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
final class TaxShipmentResolver implements TaxShipmentResolverInterface |
22
|
|
|
{ |
23
|
|
|
/** @var TaxRateResolverInterface */ |
24
|
|
|
private $taxRateResolver; |
25
|
|
|
|
26
|
|
|
/** @var CalculatorInterface */ |
27
|
|
|
private $calculator; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
TaxRateResolverInterface $taxRateResolver, |
31
|
|
|
CalculatorInterface $calculator |
32
|
|
|
) { |
33
|
|
|
$this->taxRateResolver = $taxRateResolver; |
34
|
|
|
$this->calculator = $calculator; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function resolve(OrderInterface $order): float |
38
|
|
|
{ |
39
|
|
|
$shippingMethod = $this->getShippingMethod($order); |
40
|
|
|
$rate = $this->taxRateResolver->resolve($shippingMethod); |
41
|
|
|
|
42
|
|
|
if (null === $rate) { |
43
|
|
|
throw new \LogicException('Merchant not assign tax rates to shipping method with name %s.', $shippingMethod->getName()); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $rate->getAmount(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getShippingMethod(OrderInterface $order): ShippingMethodInterface |
50
|
|
|
{ |
51
|
|
|
/** @var ShipmentInterface|bool $shipment */ |
52
|
|
|
$shipment = $order->getShipments()->first(); |
53
|
|
|
|
54
|
|
|
if (false === $shipment) { |
55
|
|
|
throw new \LogicException('Order should have at least one shipment.'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$method = $shipment->getMethod(); |
59
|
|
|
|
60
|
|
|
/** @var ShippingMethodInterface $method */ |
61
|
|
|
Assert::isInstanceOf($method, ShippingMethodInterface::class); |
62
|
|
|
|
63
|
|
|
return $method; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|