InvoiceRepository::generateInvoiceFromOrder()   C
last analyzed

Complexity

Conditions 14
Paths 13

Size

Total Lines 86
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 54
nc 13
nop 1
dl 0
loc 86
rs 6.2666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
namespace Hideyo\Ecommerce\Framework\Repositories;
3
 
4
use Hideyo\Ecommerce\Framework\Models\Invoice;
5
use Hideyo\Ecommerce\Framework\Models\InvoiceRule;
6
use Hideyo\Ecommerce\Framework\Models\InvoiceAddress;
7
use Hideyo\Ecommerce\Framework\Models\InvoiceSendingMethod;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...ls\InvoiceSendingMethod 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...
8
use Hideyo\Ecommerce\Framework\Models\InvoicePaymentMethod;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...ls\InvoicePaymentMethod 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...
9
use Hideyo\Ecommerce\Framework\Repositories\OrderRepository;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...itories\OrderRepository 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...
10
use Hideyo\Ecommerce\Framework\Repositories\ClientRepository;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...tories\ClientRepository 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...
11
use Hideyo\Ecommerce\Framework\Repositories\InvoiceAddressRepository;
12
use Hideyo\Ecommerce\Framework\Repositories\SendingMethodRepository;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...SendingMethodRepository 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
use Hideyo\Ecommerce\Framework\Repositories\PaymentMethodRepository;
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...PaymentMethodRepository 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...
14
use Validator;
15
 
16
class InvoiceRepository extends BaseRepository 
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...sitories\BaseRepository 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...
17
{
18
19
    protected $model;
20
21
    public function __construct(
22
        Invoice $model,
23
        OrderRepository $order,
24
        ClientRepository $client,
25
        InvoiceAddressRepository $invoiceAddress,
26
        SendingMethodRepository $sendingMethod,
27
        PaymentMethodRepository $paymentMethod
28
    ) {
29
        $this->model = $model;
30
        $this->client = $client;
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->order = $order;
0 ignored issues
show
Bug Best Practice introduced by
The property order does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        $this->invoiceAddress = $invoiceAddress;
0 ignored issues
show
Bug Best Practice introduced by
The property invoiceAddress does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
        $this->paymentMethod = $paymentMethod;
0 ignored issues
show
Bug Best Practice introduced by
The property paymentMethod does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
35
        $this->sendingMethod = $sendingMethod;
0 ignored issues
show
Bug Best Practice introduced by
The property sendingMethod does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
    }
37
38
    /**
39
     * The validation rules for the model.
40
     *
41
     * @param  integer  $id id attribute model    
42
     * @return array
43
     */
44
    private function rules($id = false)
45
    {
46
        $rules = array(
47
            'order_id' => 'required|unique:invoice',
48
        );
49
50
        return $rules;
51
    }
52
53
  
54
    public function create(array $attributes)
55
    {
56
        $attributes['shop_id'] = auth()->user()->selected_shop_id;
57
        $attributes['modified_by_user_id'] = auth()->user()->id;
58
        $this->model->fill($attributes);
59
        $this->model->save();
60
61
        if (isset($attributes['categories'])) {
62
            $this->model->categories()->sync($attributes['categories']);
63
        }
64
        
65
        return $this->model;
66
    }
67
68
    public function generateInvoiceFromOrder($orderId)
69
    {        
70
        $order = $this->order->find($orderId);
71
72
        if ($order->count()) {
73
            $attributes = $order->toArray();
74
            $attributes['order_id'] = $order->id;
75
76
            $validator = Validator::make($attributes, $this->rules());
77
78
            if ($validator->fails()) {
79
                return $validator;
80
            }
81
82
            $this->model->fill($attributes);
83
            $this->model->save();
84
        
85
            if ($this->model->id) {
86
                if ($order->products) {
87
                    foreach ($order->products as $product) {
88
                        $product = $product->toArray();
89
                        $product['product_id'] = $product['product_id'];
90
91
                        if (isset($product['product_combination_id'])) {
92
                            $product['product_attribute_id'] = $product['product_combination_id'];
93
                            $productCombinationTitleArray = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $productCombinationTitleArray is dead and can be removed.
Loading history...
94
                            if (isset($product['product_combination_title']) and is_array($product['product_combination_title'])) {
95
                                foreach ($product['product_combination_title'] as $key => $val) {
96
                                    $productCombinationTitle[] = $key.': '.$val;
97
                                }
98
99
                                $product['product_attribute_title'] = implode(', ', $productCombinationTitle);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $productCombinationTitle does not seem to be defined for all execution paths leading up to this point.
Loading history...
100
                            }
101
                        }
102
103
                        $products[] = new InvoiceRule($product);
104
                    }
105
106
                    if ($order->orderSendingMethod) {
107
                        $invoiceRule = array(
108
                            'type' => 'sending_cost',
109
                            'title' => $order->orderSendingMethod->title,
110
                            'tax_rate_id' =>  $order->orderSendingMethod->tax_rate_id,
111
                            'tax_rate' =>  $order->orderSendingMethod->tax_rate,
112
                            'amount' =>  1,
113
                            'price_with_tax' =>  $order->orderSendingMethod->price_with_tax,
114
                            'price_without_tax' =>  $order->orderSendingMethod->price_without_tax,
115
                            'total_price_with_tax' =>  $order->orderSendingMethod->price_with_tax,
116
                            'total_price_without_tax' =>  $order->orderSendingMethod->price_without_tax,
117
                        );
118
119
                        $products[] = new InvoiceRule($invoiceRule);
120
                    }
121
122
                    if ($order->orderPaymentMethod) {
123
                        $invoiceRule = array(
124
                            'type' => 'payment_cost',
125
                            'title' => $order->orderPaymentMethod->title,
126
                            'tax_rate_id' =>  $order->orderPaymentMethod->tax_rate_id,
127
                            'tax_rate' =>  $order->orderPaymentMethod->tax_rate,
128
                            'amount' =>  1,
129
                            'price_with_tax' =>  $order->orderPaymentMethod->price_with_tax,
130
                            'price_without_tax' =>  $order->orderPaymentMethod->price_without_tax,
131
                            'total_price_with_tax' =>  $order->orderPaymentMethod->price_with_tax,
132
                            'total_price_without_tax' =>  $order->orderPaymentMethod->price_without_tax,
133
                        );
134
135
                        $products[] = new InvoiceRule($invoiceRule);
136
                    }
137
138
                    $this->model->products()->saveMany($products);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $products seems to be defined by a foreach iteration on line 87. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
139
                }
140
141
                if ($order->orderBillAddress and $order->orderDeliveryAddress) {
142
                    $deliveryInvoiceAddress = new InvoiceAddress($order->orderBillAddress->toArray());
143
       
144
                    $billInvoiceAddress = new InvoiceAddress($order->orderDeliveryAddress->toArray());
145
146
                    $this->model->invoiceAddress()->saveMany(array($deliveryInvoiceAddress, $billInvoiceAddress));
147
     
148
                    $this->model->fill(array('delivery_invoice_address_id' => $deliveryInvoiceAddress->id, 'bill_invoice_address_id' => $billInvoiceAddress->id));
149
                    $this->model->save();
150
                }
151
            }
152
153
            return $this->model;
154
        }
155
    }
156
157
    public function updateById(array $attributes, $id)
158
    {
159
        $this->model = $this->find($id);
160
        $attributes['shop_id'] = auth()->user()->selected_shop_id;
161
        $attributes['modified_by_user_id'] = auth()->user()->id;
162
        return $this->updateEntity($attributes);
163
    }
164
165
    public function selectAllByAllProductsAndProductCategoryId($productCategoryId)
166
    {
167
        return $this->model->select('extra_field.*')->leftJoin('product_category_related_extra_field', 'extra_field.id', '=', 'product_category_related_extra_field.extra_field_id')->where('all_products', '=', 1)->orWhere('product_category_related_extra_field.product_category_id', '=', $productCategoryId)->get();
168
    }
169
170
171
}
172