Completed
Push — checkout-consistent-prices ( af49ca )
by Kamil
13:24
created

OrderUnitTaxesExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 33
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 7 1
A getIncludedTax() 0 4 1
A getExcludedTax() 0 4 1
A getAmount() 0 12 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\AdminBundle\Twig;
15
16
use Sylius\Component\Core\Model\AdjustmentInterface;
17
use Sylius\Component\Core\Model\OrderItemInterface;
18
use Sylius\Component\Order\Model\AdjustmentInterface as BaseAdjustmentInterface;
19
use Twig\Extension\AbstractExtension;
20
use Twig\TwigFunction;
21
22
final class OrderUnitTaxesExtension extends AbstractExtension
23
{
24
    public function getFunctions(): array
25
    {
26
        return [
27
            new TwigFunction('sylius_admin_order_unit_tax_included', [$this, 'getIncludedTax']),
28
            new TwigFunction('sylius_admin_order_unit_tax_excluded', [$this, 'getExcludedTax']),
29
        ];
30
    }
31
32
    public function getIncludedTax(OrderItemInterface $orderItemUnit): int
33
    {
34
        return $this->getAmount($orderItemUnit, true);
35
    }
36
37
    public function getExcludedTax(OrderItemInterface $orderItemUnit): int
38
    {
39
        return $this->getAmount($orderItemUnit, false);
40
    }
41
42
    private function getAmount(OrderItemInterface $orderItem, bool $neutral): int
43
    {
44
        $total = array_reduce(
45
            $orderItem->getAdjustmentsRecursively(AdjustmentInterface::TAX_ADJUSTMENT)->toArray(),
46
            static function (int $total, BaseAdjustmentInterface $adjustment) use ($neutral) {
47
                return $neutral === $adjustment->isNeutral() ? $total + $adjustment->getAmount() : $total;
48
            },
49
            0
50
        );
51
52
        return $total;
53
    }
54
}
55