Passed
Pull Request — master (#93)
by
unknown
13:46 queued 06:13
created

TaxShipmentResolver::getTaxZone()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 1
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());
0 ignored issues
show
Bug introduced by
It seems like $shippingMethod->getName() can also be of type string; however, parameter $code of LogicException::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            throw new \LogicException('Merchant not assign tax rates to shipping method with name %s.', /** @scrutinizer ignore-type */ $shippingMethod->getName());
Loading history...
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