Passed
Push — master ( 81a345...8ff05f )
by Matthijs
01:44
created

OrderService::updateById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Order;
4
5
use Validator;
0 ignored issues
show
Bug introduced by
The type Validator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use File;
7
use Hideyo\Ecommerce\Framework\Services\Order\Entity\OrderRepository;
8
use Hideyo\Ecommerce\Framework\Services\BaseService;
9
use Hideyo\Ecommerce\Framework\Services\Client\ClientFacade as ClientService;
10
use Hideyo\Ecommerce\Framework\Services\SendingMethod\SendingMethodFacade as SendingMethodService;
11
use Hideyo\Ecommerce\Framework\Services\PaymentMethod\PaymentMethodFacade as PaymentMethodService;
12
use Cart; 
0 ignored issues
show
Bug introduced by
The type Cart was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
class OrderService extends BaseService
15
{
16
	public function __construct(OrderRepository $order)
17
	{
18
		$this->repo = $order;
0 ignored issues
show
Bug Best Practice introduced by
The property repo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
	} 
20
21
    public function createAddress(array $attributes, $orderId)
22
    {
23
        if (auth('hideyobackend')->check()) {
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

23
        if (/** @scrutinizer ignore-call */ auth('hideyobackend')->check()) {
Loading history...
24
            $userId = auth('hideyobackend')->user()->id;
25
            $attributes['modified_by_user_id'] = $userId;
26
        }
27
28
        $attributes['order_id'] = $orderId;  
29
        $this->getAddressModel()->fill($attributes);
30
        $this->getAddressModel()->save();
31
        
32
        return $this->getAddressModel();
33
    }
34
35
    public function create(array $attributes)
36
    {
37
        $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

37
        $attributes['shop_id'] = /** @scrutinizer ignore-call */ auth('hideyobackend')->user()->selected_shop_id;
Loading history...
38
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
39
        $this->repo->getModel()->fill($attributes);
40
        $this->repo->getModel()->save();
41
42
        if (isset($attributes['categories'])) {
43
            $this->repo->getModel()->categories()->sync($attributes['categories']);
44
        }
45
        
46
        return $this->model;
47
    }
48
49
    public function updateById(array $attributes, $id)
50
    {
51
        $this->model = $this->find($id);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
        if (auth('hideyobackend')->check()) {
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
        if (/** @scrutinizer ignore-call */ auth('hideyobackend')->check()) {
Loading history...
53
            $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
54
            $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
55
        }
56
57
        return $this->updateEntity($attributes);
0 ignored issues
show
Bug introduced by
The method updateEntity() does not exist on Hideyo\Ecommerce\Framewo...ices\Order\OrderService. ( Ignorable by Annotation )

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

57
        return $this->/** @scrutinizer ignore-call */ updateEntity($attributes);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
    }
59
60
    public function addProducts($orderModel, $products) 
61
    {
62
        foreach ($products  as $product) {
63
64
            $quantity = $product->quantity;
65
            $newProduct = array(
66
                'product_id' => $product['attributes']['id'],
67
                'title' => $product['attributes']['title'],
68
                'original_price_without_tax' => $product['attributes']['price_details']['original_price_ex_tax'],
69
                'original_price_with_tax' => $product['attributes']['price_details']['original_price_inc_tax'],
70
                'original_total_price_without_tax' => $quantity * $product['attributes']['price_details']['original_price_ex_tax'],
71
                'original_total_price_with_tax' => $quantity * $product['attributes']['price_details']['original_price_inc_tax'],
72
                'price_without_tax' => $product->getOriginalPriceWithoutTaxAndConditions(false),
73
                'price_with_tax' => $product->getOriginalPriceWithTaxAndConditions(false),
74
                'total_price_without_tax' => $product->getOriginalPriceWithoutTaxSum(false),
75
                'total_price_with_tax' => $product->getOriginalPriceWithTaxSum(false),
76
                'amount' => $quantity,
77
                'tax_rate' => $product['attributes']['tax_rate'],
78
                'tax_rate_id' => $product['attributes']['tax_rate_id'],
79
                'weight' => $product['attributes']['weight'],
80
                'reference_code' => $product['attributes']['reference_code'],
81
            );
82
83
            if (isset($product['attributes']['product_combination_id'])) {
84
                $newProduct['product_attribute_id'] = $product['attributes']['product_combination_id'];
85
                $productCombinationTitleArray = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $productCombinationTitleArray is dead and can be removed.
Loading history...
86
87
                if (isset($product['attributes']['product_combination_title'])) {
88
                    $productCombinationTitle = array();
89
90
                    foreach ($product['attributes']['product_combination_title'] as $key => $val) {
91
92
                        $productCombinationTitle[] = $key.': '.$val;
93
                    }
94
95
                    $newProduct['product_attribute_title'] = implode(', ', $productCombinationTitle);
96
                
97
                }
98
            }
99
            $modelProduct = $this->getProductModel();
100
            $newProducts[] = new $modelProduct($newProduct);
101
        }
102
103
        $orderModel->products()->saveMany($newProducts);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newProducts seems to be defined by a foreach iteration on line 62. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
104
105
        return $orderModel;
106
    }
107
108
    public function createOrderFrontend(array $attributes, $shopId, $noAccountUser)
109
    {
110
        $attributes['shop_id'] = $shopId;
111
        $attributes['client_id'] = $attributes['user_id'];
112
        $client  = ClientService::selectOneByShopIdAndId($shopId, $attributes['user_id']);
113
114
        $this->repo->getModel()->fill($attributes);
115
        $this->repo->getModel()->save();
116
117
        if (!Cart::getContent()->count()) {
118
            return false;
119
        }
120
121
        $this->addProducts($this->repo->getModel(), Cart::getContent());
122
        
123
        if ($client) {
124
            if ($client->clientDeliveryAddress) {
125
                $deliveryOrderAddress = $this->createAddress($client->clientDeliveryAddress->toArray(), $this->repo->getModel()->id);
126
            }
127
128
            if ($client->clientBillAddress) {
129
                $billOrderAddress = $this->createAddress($client->clientBillAddress->toArray(), $this->repo->getModel()->id);
130
            }
131
132
            $this->repo->getModel()->fill(array('delivery_order_address_id' => $deliveryOrderAddress->id, 'bill_order_address_id' => $billOrderAddress->id));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $billOrderAddress does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $deliveryOrderAddress does not seem to be defined for all execution paths leading up to this point.
Loading history...
133
            $this->repo->getModel()->save();
134
135
        } elseif ($noAccountUser) {
136
            if (isset($noAccountUser['delivery'])) {
137
                $deliveryOrderAddress = $billOrderAddress = $this->createAddress($noAccountUser['delivery'], $this->repo->getModel()->id);
0 ignored issues
show
Unused Code introduced by
The assignment to $billOrderAddress is dead and can be removed.
Loading history...
138
            } else {
139
                $deliveryOrderAddress = $billOrderAddress = $this->createAddress($noAccountUser, $this->repo->getModel()->id);
140
            }
141
142
            $billOrderAddress = $billOrderAddress = $this->createAddress($noAccountUser, $this->repo->getModel()->id);
143
            $this->repo->getModel()->fill(array('delivery_order_address_id' => $deliveryOrderAddress->id, 'bill_order_address_id' => $billOrderAddress->id));
144
            $this->repo->getModel()->save();
145
        }
146
147
        if (Cart::getConditionsByType('sending_method')->count()) {
148
            $attributes = Cart::getConditionsByType('sending_method')->first()->getAttributes();
149
            $sendingMethod = SendingMethodService::find($attributes['data']['id']);
150
            $price = $sendingMethod->getPriceDetails();
151
            $sendingMethodArray = $sendingMethod->toArray();
152
            $sendingMethodArray['price_with_tax'] = Cart::getConditionsByType('sending_method')->first()->getAttributes()['data']['price_details']['original_price_inc_tax'];
153
            $sendingMethodArray['price_without_tax'] = Cart::getConditionsByType('sending_method')->first()->getAttributes()['data']['price_details']['original_price_ex_tax'];
154
            $sendingMethodArray['tax_rate'] = $price['tax_rate'];
155
            $sendingMethodArray['sending_method_id'] = $sendingMethod->id;
156
            $sendingMethodArray['order_id'] = $this->repo->getModel()->id;
157
            $orderSendingMethod = $this->getSendingMethodModel()->fill($sendingMethodArray);
158
            $orderSendingMethod = $orderSendingMethod->save();
0 ignored issues
show
Unused Code introduced by
The assignment to $orderSendingMethod is dead and can be removed.
Loading history...
159
        }
160
161
        if (Cart::getConditionsByType('payment_method')->count()) {
162
            $attributes = Cart::getConditionsByType('payment_method')->first()->getAttributes();
163
            $paymentMethod = PaymentMethodService::find($attributes['data']['id']);
164
            $price = $paymentMethod->getPriceDetails();
165
            $paymentMethodArray = $paymentMethod->toArray();
166
            $paymentMethodArray['price_with_tax'] = Cart::getConditionsByType('payment_method')->first()->getAttributes()['data']['value_inc_tax'];
167
            $paymentMethodArray['price_without_tax'] = Cart::getConditionsByType('payment_method')->first()->getAttributes()['data']['value_ex_tax'];
168
            $paymentMethodArray['tax_rate'] = $price['tax_rate'];
169
            $paymentMethodArray['payment_method_id'] = $paymentMethod->id;
170
            $paymentMethodArray['order_id'] = $this->repo->getModel()->id;
171
            $orderPaymentMethod = $this->getPaymentMethodModel()->fill($paymentMethodArray);
172
            $orderPaymentMethod = $orderPaymentMethod->save();
0 ignored issues
show
Unused Code introduced by
The assignment to $orderPaymentMethod is dead and can be removed.
Loading history...
173
        }
174
175
        if ($this->repo->getModel()->orderPaymentMethod->paymentMethod->order_confirmed_order_status_id) {
176
            $this->repo->getModel()->fill(array('order_status_id' => $this->repo->getModel()->orderPaymentMethod->paymentMethod->order_confirmed_order_status_id));
177
            $this->repo->getModel()->save();
178
        }
179
180
        return $this->repo->getModel();
181
    }
182
183
    public function getProductModel()
184
    {
185
        return $this->repo->getProductModel();
186
    }
187
188
    public function getAddressModel()
189
    {
190
        return $this->repo->getAddressModel();
191
    }
192
193
    public function getSendingMethodModel()
194
    {
195
        return $this->repo->getOrderSendingMethodModel();
196
    }
197
198
    public function getPaymentMethodModel()
199
    {
200
        return $this->repo->getOrderPaymentMethodModel();
201
    }
202
203
    public function updateStatus($id, $orderStatusId)
204
    {
205
        $model = $this->find($id);
206
207
        $attributes['order_status_id'] = $orderStatusId;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.
Loading history...
208
        if (count($attributes) > 0) {
209
            $model->fill($attributes);
210
            $model->save();
211
        }
212
213
        return $model;
214
    }
215
}