OrderViewFactory::createFromOrder()   B
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 7.263
c 0
b 0
f 0
cc 7
nc 64
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\User\OrderHistory;
14
15
use BitBag\SyliusVueStorefrontPlugin\Factory\Cart\CartItemViewFactoryInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\Common\AddressViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Helper\DateHelper;
18
use BitBag\SyliusVueStorefrontPlugin\View\Common\AddressView;
19
use BitBag\SyliusVueStorefrontPlugin\View\User\OrderHistory\OrderView;
20
use BitBag\SyliusVueStorefrontPlugin\View\User\OrderHistory\PaymentView;
21
use Sylius\Component\Core\Model\OrderInterface;
22
23
final class OrderViewFactory implements OrderViewFactoryInterface
24
{
25
    /** @var string */
26
    private $orderViewClass;
27
28
    /** @var CartItemViewFactoryInterface */
29
    private $cartItemViewFactory;
30
31
    /** @var AddressViewFactoryInterface */
32
    private $addressViewFactory;
33
34
    /** @var OrderExtensionAttributesViewFactoryInterface */
35
    private $orderExtensionAttributesViewFactory;
36
37
    /** @var PaymentViewFactoryInterface */
38
    private $paymentViewFactory;
39
40
    public function __construct(
41
        string $orderViewClass,
42
        CartItemViewFactoryInterface $cartItemViewFactory,
43
        AddressViewFactoryInterface $addressViewFactory,
44
        OrderExtensionAttributesViewFactoryInterface $orderExtensionAttributesViewFactory,
45
        PaymentViewFactoryInterface $paymentViewFactory
46
    ) {
47
        $this->orderViewClass = $orderViewClass;
48
        $this->cartItemViewFactory = $cartItemViewFactory;
49
        $this->addressViewFactory = $addressViewFactory;
50
        $this->orderExtensionAttributesViewFactory = $orderExtensionAttributesViewFactory;
51
        $this->paymentViewFactory = $paymentViewFactory;
52
    }
53
54
    public function create(OrderInterface $syliusOrder): OrderView
55
    {
56
        return $this->createFromOrder($syliusOrder);
57
    }
58
59
    public function createList(array $syliusOrders): array
60
    {
61
        $ordersList = [];
62
63
        /** @var OrderInterface $syliusOrder */
64
        foreach ($syliusOrders as $syliusOrder) {
65
            if (!$syliusOrder->getTokenValue()) {
66
                continue;
67
            }
68
69
            $ordersList[] = $this->createFromOrder($syliusOrder);
70
        }
71
72
        return $ordersList;
73
    }
74
75
    private function createFromOrder(OrderInterface $syliusOrder): OrderView
76
    {
77
        /** @var OrderView $orderView */
78
        $orderView = new $this->orderViewClass();
79
        $orderView->applied_rule_ids = '';
80
        $orderView->base_currency_code = $syliusOrder->getCurrencyCode();
81
        $orderView->base_discount_amount = 0;
82
        $orderView->base_grand_total = $syliusOrder->getTotal();
83
        $orderView->base_discount_tax_compensation_amount = 0;
84
        $orderView->base_shipping_amount = $syliusOrder->getShippingTotal();
85
        $orderView->base_shipping_discount_amount = 0;
86
        $orderView->base_shipping_incl_tax = $syliusOrder->getShippingTotal();
87
        $orderView->base_shipping_tax_amount = 0;
88
        $orderView->base_subtotal = $syliusOrder->getItemsTotal();
89
        $orderView->base_subtotal_incl_tax = $syliusOrder->getItemsTotal();
90
        $orderView->base_tax_amount = $syliusOrder->getTaxTotal();
91
        $orderView->base_total_due = $syliusOrder->getTotal();
92
        $orderView->base_to_global_rate = 0;
93
        $orderView->base_to_order_rate = 0;
94
        $orderView->billing_address_id = $syliusOrder->getBillingAddress() ? $syliusOrder->getBillingAddress()->getId() : 1;
95
        $orderView->created_at = $syliusOrder->getCreatedAt()->format(DateHelper::DATE_TIME_FORMAT);
96
        $orderView->customer_email = $syliusOrder->getCustomer()->getEmail();
97
        $orderView->customer_firstname = $syliusOrder->getCustomer()->getFirstName();
98
        $orderView->customer_lastname = $syliusOrder->getCustomer()->getLastName();
99
        $orderView->customer_group_id = 0;
100
        $orderView->customer_is_guest = 0;
101
        $orderView->customer_note_notify = 0;
102
103
        $orderView->discount_amount = $syliusOrder->getOrderPromotionTotal();
104
105
        $orderView->email_sent = 1;
106
        $orderView->entity_id = $syliusOrder->getId();
107
        $orderView->global_currency_code = $syliusOrder->getCurrencyCode();
108
        $orderView->grand_total = $syliusOrder->getItemsTotal() + $syliusOrder->getShippingTotal();
109
        $orderView->discount_tax_compensation_amount = 0;
110
111
        $orderView->increment_id = '000000000';
112
        if ($syliusOrder->getNumber()) {
113
            $orderView->increment_id = $syliusOrder->getNumber();
114
        }
115
116
        $orderView->is_virtual = 0;
117
        $orderView->order_currency_code = $syliusOrder->getCurrencyCode();
118
        $orderView->protect_code = '';
119
        $orderView->quote_id = $syliusOrder->getId();
120
        $orderView->shipping_amount = $syliusOrder->getShippingTotal();
121
122
        $orderView->shipping_description = '';
123
        if ($syliusOrder->getShipments()->first()) {
124
            $orderView->shipping_description = $syliusOrder->getShipments()->first()->getMethod()->getName();
125
        }
126
127
        $orderView->shipping_discount_amount = 0;
128
        $orderView->shipping_discount_tax_compensation_amount = 0;
129
        $orderView->shipping_incl_tax = $syliusOrder->getShippingTotal();
130
        $orderView->shipping_tax_amount = 0;
131
        $orderView->state = $syliusOrder->getState();
132
        $orderView->status = $syliusOrder->getCheckoutState();
133
        $orderView->store_currency_code = $syliusOrder->getCurrencyCode();
134
        $orderView->store_id = 1;
135
        $orderView->store_name = 'Example';
136
        $orderView->store_to_base_rate = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $store_to_base_rate was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
137
        $orderView->store_to_order_rate = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $store_to_order_rate was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
138
        $syliusOrder->recalculateItemsTotal();
139
        $orderView->subtotal = $syliusOrder->getItemsTotal();
140
        $orderView->subtotal_incl_tax = $syliusOrder->getItemsTotal();
141
        $orderView->tax_amount = $syliusOrder->getTaxTotal();
142
        $orderView->total_due = $syliusOrder->getTotal();
143
        $orderView->total_item_count = $syliusOrder->getTotalQuantity();
144
        $orderView->total_qty_ordered = $syliusOrder->getTotalQuantity();
145
        $orderView->updated_at = $syliusOrder->getUpdatedAt() ? $syliusOrder->getUpdatedAt()->format(DateHelper::DATE_TIME_FORMAT) : null;
146
        $orderView->weight = 5;
0 ignored issues
show
Documentation Bug introduced by
The property $weight was declared of type double, but 5 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
147
        $orderView->items = $this->cartItemViewFactory->createList($syliusOrder->getItems());
148
149
        if ($syliusOrder->getBillingAddress()) {
150
            $orderView->billing_address = $this->addressViewFactory->create($syliusOrder->getBillingAddress());
151
        } else {
152
            $orderView->billing_address = new AddressView();
153
        }
154
155
        if ($syliusOrder->getPayments()->first()) {
156
            $orderView->payment = $this->paymentViewFactory->create($syliusOrder->getPayments()->first());
157
        } else {
158
            $orderView->payment = new PaymentView();
159
        }
160
161
        $orderView->status_histories = [];
162
        $orderView->extension_attributes = $this->orderExtensionAttributesViewFactory->create($syliusOrder);
163
164
        return $orderView;
165
    }
166
}
167