ClientOrderController::index()   B
last analyzed

Complexity

Conditions 10
Paths 2

Size

Total Lines 67
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 39
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 67
rs 7.6666

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 namespace App\Http\Controllers\Backend;
2
3
use App\Http\Controllers\Controller;
4
5
/**
6
 * ClientOrderController
7
 *
8
 * This is the controller for the shop clients orders
9
 * @author Matthijs Neijenhuijs <[email protected]>
10
 * @version 0.1
11
 */
12
13
use Hideyo\Ecommerce\Framework\Services\Client\Entity\ClientRepository;
14
use Hideyo\Ecommerce\Framework\Services\Client\Entity\ClientAddressRepository;
15
use Hideyo\Ecommerce\Framework\Services\Order\Entity\OrderRepository;
16
use Illuminate\Http\Request;
17
use Form;
18
use DataTables;
19
20
class ClientOrderController extends Controller
21
{
22
    public function __construct(
23
        Request $request, 
24
        ClientAddressRepository $clientAddress, 
25
        ClientRepository $client, 
26
        OrderRepository $order)
27
    {
28
        $this->clientAddress = $clientAddress;
0 ignored issues
show
Bug Best Practice introduced by
The property clientAddress does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
        $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...
30
        $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...
31
        $this->request = $request;
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
    }
33
34
    public function index($clientId)
35
    {
36
        $client = $this->client->find($clientId);
37
        if ($this->request->wantsJson()) {
38
39
            $order = $this->order->getModel()->select(
40
                ['id', 'created_at', 'generated_custom_order_id', 'order_status_id', 'client_id', 'delivery_order_address_id', 'bill_order_address_id',
41
                'price_with_tax']
42
            )->with(array('orderStatus', 'orderPaymentMethod', 'orderSendingMethod', 'products', 'client', 'orderBillAddress', 'orderDeliveryAddress'))->where('shop_id', '=', auth('hideyobackend')->user()->selected_shop_id)->where('client_id', '=', $clientId);
0 ignored issues
show
Bug introduced by
Accessing selected_shop_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
43
            
44
            
45
            $datatables = DataTables::of($order)
46
47
            ->addColumn('status', function ($order) {
48
                if ($order->orderStatus) {
49
                     return $order->orderStatus->title;
50
                }
51
            })
52
53
            ->addColumn('created_at', function ($order) {
54
                return date('d F H:i', strtotime($order->created_at));
55
            })
56
            ->addColumn('status', function ($order) {
57
                if ($order->orderStatus) {
58
                    if ($order->orderStatus->color) {
59
                        return '<a href="/admin/order/'.$order->id.'" style="text-decoration:none;"><span style="background-color:'.$order->orderStatus->color.'; padding: 10px; line-height:30px; text-align:center; color:white;">'.$order->orderStatus->title.'</span></a>';
60
                    }
61
                    return $order->orderStatus->title;
62
                }
63
            })
64
            ->addColumn('client', function ($order) {
65
                if ($order->client) {
66
                    if ($order->orderBillAddress) {
67
                        return $order->orderBillAddress->firstname.' '.$order->orderBillAddress->lastname;
68
                    }
69
                }
70
            })
71
            ->addColumn('products', function ($order) {
72
                if ($order->products) {
73
                    return $order->products->count();
74
                }
75
            })
76
            ->addColumn('price_with_tax', function ($order) {
77
                $money = '&euro; '.$order->getPriceWithTaxNumberFormat();
78
                return $money;
79
            })
80
            ->addColumn('paymentMethod', function ($order) {
81
                if ($order->orderPaymentMethod) {
82
                    return $order->orderPaymentMethod->title;
83
                }
84
            })
85
            ->addColumn('sendingMethod', function ($order) {
86
                if ($order->orderSendingMethod) {
87
                    return $order->orderSendingMethod->title;
88
                }
89
            })
90
            ->addColumn('action', function ($order) {
91
                $download = '<a href="/admin/order/'.$order->id.'/download" class="btn btn-default btn-sm btn-info"><i class="entypo-pencil"></i>Download</a>  ';
92
                $links = '<a href="/admin/order/'.$order->id.'" class="btn btn-default btn-sm btn-success"><i class="entypo-pencil"></i>Show</a>  '.$download;
93
            
94
                return $links;
95
            });
96
97
            return $datatables->rawColumns(['status', 'action'])->make(true);
98
        }
99
        
100
        return view('backend.client_order.index')->with(array('client' => $client));
101
    }   
102
}