Completed
Push — master ( f75f45...8e0efc )
by Szymon
15s queued 11s
created

TotalsViewFactory::countDiscountAdjustmentValues()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 3
nc 4
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
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Factory\Cart\Totals;
14
15
use BitBag\SyliusVueStorefrontPlugin\Factory\Cart\CartItemViewFactoryInterface;
16
use BitBag\SyliusVueStorefrontPlugin\View\Cart\Totals\TotalsView;
17
use Sylius\Component\Core\Model\AdjustmentInterface;
18
use Sylius\Component\Core\Model\OrderInterface as SyliusOrderInterface;
19
20
final class TotalsViewFactory implements TotalsViewFactoryInterface
21
{
22
    /** @var string */
23
    private $totalsViewClass;
24
25
    /** @var CartItemViewFactoryInterface */
26
    private $cartItemViewFactory;
27
28
    /** @var TotalSegmentViewFactoryInterface */
29
    private $totalSegmentViewFactory;
30
31
    public function __construct(
32
        string $totalsViewClass,
33
        CartItemViewFactoryInterface $cartItemViewFactory,
34
        TotalSegmentViewFactoryInterface $totalSegmentViewFactory
35
    ) {
36
        $this->totalsViewClass = $totalsViewClass;
37
        $this->cartItemViewFactory = $cartItemViewFactory;
38
        $this->totalSegmentViewFactory = $totalSegmentViewFactory;
39
    }
40
41
    public function create(SyliusOrderInterface $syliusOrder): TotalsView
42
    {
43
        /** @var TotalsView $totalsView */
44
        $totalsView = new $this->totalsViewClass();
45
46
        $totalsView->grand_total = $syliusOrder->getTotal();
47
        $totalsView->base_grand_total = $syliusOrder->getTotal();
48
        $totalsView->subtotal = $syliusOrder->getItemsTotal();
49
        $totalsView->base_subtotal = $syliusOrder->getItemsTotal();
50
51
        if ($syliusOrder->getPromotionCoupon()) {
52
            $totalsView->coupon_code = $syliusOrder->getPromotionCoupon()->getCode();
53
        }
54
55
        $totalsView->discount_amount = abs($syliusOrder->getOrderPromotionTotal());
0 ignored issues
show
Documentation Bug introduced by
It seems like abs($syliusOrder->getOrderPromotionTotal()) can also be of type double. However, the property $discount_amount is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
56
        $totalsView->subtotal_with_discount = $syliusOrder->getItemsTotal();
57
        $totalsView->shipping_amount = $syliusOrder->getShippingTotal();
58
        $totalsView->shipping_discount_amount = $this->countShippingDiscount($syliusOrder);
59
        $totalsView->tax_amount = $syliusOrder->getTaxTotal();
60
61
        $totalsView->shipping_tax_amount = 0;
62
        $totalsView->base_shipping_tax_amount = 0;
63
        $totalsView->subtotal_incl_tax = $syliusOrder->getItemsTotal();
64
        $totalsView->shipping_incl_tax = $syliusOrder->getShippingTotal();
65
66
        $totalsView->base_currency_code = $syliusOrder->getCurrencyCode();
67
        $totalsView->quote_currency_code = $syliusOrder->getCurrencyCode();
68
        $totalsView->items_qty = $syliusOrder->getItems()->count();
69
        $totalsView->items = $this->cartItemViewFactory->createList($syliusOrder->getItems());
70
        $totalsView->total_segments = $this->totalSegmentViewFactory->createList($syliusOrder);
71
72
        return $totalsView;
73
    }
74
75
    private function countShippingDiscount(SyliusOrderInterface $syliusOrder): int
76
    {
77
        return $syliusOrder->getAdjustmentsTotal(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT);
78
    }
79
}
80