ClientController   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 83
c 1
b 0
f 1
dl 0
loc 174
rs 10
wmc 28

12 Methods

Rating   Name   Duplication   Size   Complexity  
A postExport() 0 39 5
A update() 0 24 5
A destroy() 0 7 2
A store() 0 4 1
A index() 0 26 4
A postActivate() 0 15 2
A getDeActivate() 0 3 1
A postDeActivate() 0 5 1
A create() 0 3 1
A getActivate() 0 3 1
A getExport() 0 3 1
A edit() 0 18 4
1
<?php namespace App\Http\Controllers\Backend;
2
3
4
use App\Http\Controllers\Controller;
5
6
/**
7
 * ClientController
8
 *
9
 * This is the controller for the shop clients
10
 * @author Matthijs Neijenhuijs <[email protected]>
11
 * @version 0.1
12
 */
13
14
use Hideyo\Ecommerce\Framework\Services\Client\ClientFacade as ClientService;
15
16
use Illuminate\Http\Request;
17
use Notification;
18
use Mail;
19
use Excel;
0 ignored issues
show
Bug introduced by
The type Excel 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...
20
use Form;
21
use DataTables;
22
23
class ClientController extends Controller
24
{
25
    public function index(Request $request)
26
    {
27
        if ($request->wantsJson()) {
28
            $clients = ClientService::getModel()->where('shop_id', '=', auth('hideyobackend')->user()->selected_shop_id);
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...
29
            
30
            $datatables = DataTables::of($clients)
31
            ->addColumn('last_login', function ($clients) {
32
                return date('d F H:i', strtotime($clients->last_login));
33
            })
34
            ->addColumn('action', function ($clients) {
35
                $deleteLink = Form::deleteajax(url()->route('client.destroy', $clients->id), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger'));
36
                $links = '<a href="'.url()->route('client.edit', $clients->id).'" class="btn btn-default btn-sm btn-success"><i class="entypo-pencil"></i>Show</a>  '.$deleteLink;
37
            
38
                if (!$clients->active || !$clients->confirmed) {
39
                    $links .= ' <a href="'.url()->route('client.activate', $clients->id).'" class="btn btn-default btn-sm btn-info">activate</a>';
40
                } else {
41
                    $links .= ' <a href="'.url()->route('client.de-activate', $clients->id).'" class="btn btn-default btn-sm btn-info">block</a>';
42
                }
43
                
44
                return $links;
45
            });
46
47
            return $datatables->make(true);
48
        }
49
        
50
        return view('backend.client.index')->with('client', ClientService::selectAll());    
51
    }
52
53
    public function create()
54
    {
55
        return view('backend.client.create')->with(array());
56
    }
57
58
    public function getActivate($clientId)
59
    {
60
        return view('backend.client.activate')->with(array('client' => ClientService::find($clientId)));
61
    }
62
63
    public function getDeActivate($clientId)
64
    {
65
        return view('backend.client.de-activate')->with(array('client' => ClientService::find($clientId)));
66
    }
67
68
    public function postActivate(Request $request, $clientId)
69
    {
70
        $input = $request->all();
71
        $result  = ClientService::activate($clientId);
72
73
        if ($input['send_mail']) {
74
                Mail::send('frontend.email.activate-mail', array('user' => $result->toArray(), 'billAddress' => $result->clientBillAddress->toArray()), function ($message) use ($result) {
75
                    $message->to($result['email'])->from('[email protected]', 'Hideyo')->subject('Toegang tot account.');
76
                });
77
78
                Notification::container('foundation')->success('Uw account is geactiveerd.');
79
        }
80
        
81
        flash('The client was activate.');
82
        return redirect()->route('client.index');
83
    }
84
85
    public function postDeActivate($clientId)
86
    {
87
        ClientService::deactivate($clientId);
88
        flash('The client was deactivate.');
89
        return redirect()->route('client.index');
90
    }
91
92
    public function store(Request $request)
93
    {
94
        $result  = ClientService::create($request->all());
95
        return ClientService::notificationRedirect('client.index', $result, 'The client was inserted.');
96
    }
97
98
    public function edit($clientId)
99
    {
100
        $addresses = ClientService::selectAddressesByClientId($clientId);
101
102
        $addressesList = array();
103
104
        if ($addresses) {
105
            foreach ($addresses as $row) {
106
                $addressesList[$row->id] = $row->street.' '.$row->housenumber;
107
                if ($row->housenumber_suffix) {
108
                    $addressesList[$row->id] .= $row->housenumber_suffix;
109
                }
110
111
                $addressesList[$row->id] .= ', '.$row->city;
112
            }
113
        }
114
115
        return view('backend.client.edit')->with(array('client' => ClientService::find($clientId), 'addresses' => $addressesList));
116
    }
117
118
    public function getExport()
119
    {
120
        return view('backend.client.export')->with(array());
121
    }
122
123
    public function postExport()
124
    {
125
        $result  =  ClientService::selectAllExport();
126
        Excel::create('export', function ($excel) use ($result) {
127
128
            $excel->sheet('Clients', function ($sheet) use ($result) {
129
                $newArray = array();
130
                foreach ($result as $row) {
131
                    $firstname = null;
132
                    if($row->clientBillAddress) {
133
                        $firstname = $row->clientBillAddress->firstname;
134
                    }
135
136
                    $lastname = null;
137
                    if($row->clientBillAddress) {
138
                        $lastname = $row->clientBillAddress->lastname;
139
                    }
140
141
                    $gender = null;
142
                    if($row->clientBillAddress) {
143
                        $gender = $row->clientBillAddress->gender;
144
                    }
145
146
                    $newArray[$row->id] = array(
147
                        'email' => $row->email,
148
                        'company' => $row->company,
149
                        'firstname' => $firstname,
150
                        'lastname' => $lastname,
151
                        'gender' => $gender
152
                    );
153
                }
154
155
                $sheet->fromArray($newArray);
156
            });
157
        })->download('xls');
158
159
160
        flash('The product export is completed.');
161
        return redirect()->route('product.index');
162
    }
163
164
    public function update(Request $request, $clientId)
165
    {
166
        $result  = ClientService::updateById($request->all(), $clientId);
167
        $input = $request->all();
168
        if (isset($result->id)) {
169
            if ($result->active) {
170
                if ($input['send_mail']) {
171
                    Mail::send('frontend.email.activate-mail', array('user' => $result->toArray(), 'billAddress' => $result->clientBillAddress->toArray()), function ($message) use ($result) {
172
                        $message->to($result['email'])->from('[email protected]', 'Hideyo')->subject('Toegang tot account.');
173
                    });
174
175
                    Notification::container('foundation')->success('Uw account is geactiveerd.');
176
                }
177
                
178
            }
179
180
            flash('The client was updated.');
181
            return redirect()->route('client.index');
182
        }
183
        
184
        foreach ($result->errors()->all() as $error) {
185
            flash($error);
186
        }
187
        return redirect()->back()->withInput();
188
    }
189
190
    public function destroy($clientId)
191
    {
192
        $result  = ClientService::destroy($clientId);
193
194
        if ($result) {
195
            flash('The client was deleted.');
196
            return redirect()->route('client.index');
197
        }
198
    }
199
}
200