Passed
Push — master ( 458748...6b11f3 )
by Matthijs
05:25
created

AccountController::getEditAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace App\Http\Controllers\Frontend;
2
3
use App\Http\Controllers\Controller;
4
use Illuminate\Http\Request;
5
use Hideyo\Ecommerce\Framework\Services\Client\ClientFacade as ClientService;
6
use Validator;
7
use Mail;
8
use Notification;
9
10
class AccountController extends Controller
11
{
12
    public function getIndex()
13
    {
14
        return view('frontend.account.index')->with(array('user' => auth('web')->user()));
15
    }
16
17
    public function getEditAccount()
18
    {
19
        return view('frontend.account.edit-account')->with(array('user' => auth('web')->user()));
20
    }
21
22
    public function getResetAccount($confirmationCode, $email)
23
    {
24
        $result = ClientService::changeAccountDetails($confirmationCode, $email, config()->get('app.shop_id'));
25
26
        if ($result) {
27
            Notification::success('Je account gegevens zijn gewijzigd en je dient opnieuw in te loggen met de nieuwe gegevens.');
28
        }
29
        
30
        Notification::error('Wijziging is niet mogelijk.');
31
        auth('web')->logout();
32
        return redirect()->to('account/login');
33
    }
34
35
    public function getEditAddress($type)
36
    {
37
        $shop = app('shop');
38
        return view('frontend.account.edit-account-address-'.$type)->with(array('sendingMethods' => $shop->sendingMethods, 'user' => auth('web')->user()));
39
    }
40
41
    public function postEditAddress(Request $request, $type)
42
    {
43
        $userdata = $request->all();
44
45
        // create the validation rules ------------------------
46
        $rules = array(
47
            'firstname'     => 'required',
48
            'lastname'      => 'required',
49
            'zipcode'       => 'required|max:8',
50
            'housenumber'   => 'required|numeric',
51
            'street'        => 'required',
52
            'city'          => 'required',
53
            'country'       => 'required'
54
        );
55
56
        $validator = Validator::make($userdata, $rules);
57
58
        if ($validator->fails()) {
59
            // get the error messages from the validator
60
            foreach ($validator->errors()->all() as $error) {
61
                Notification::error($error);
62
            }
63
64
            // redirect our user back to the form with the errors from the validator
65
            return redirect()->to('account/edit-address/'.$type)
66
                ->with(array('type' => $type))->withInput();
67
        }
68
     
69
        $user = auth('web')->user();
70
71
        if ($type == 'bill') {
72
            $id = $user->clientBillAddress->id;
0 ignored issues
show
Bug introduced by
Accessing clientBillAddress on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
73
74
            if ($user->clientDeliveryAddress->id == $user->clientBillAddress->id) {
0 ignored issues
show
Bug introduced by
Accessing clientDeliveryAddress on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
75
                $clientAddress = ClientService::createAddress($userdata, $user->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
76
                ClientService::setBillOrDeliveryAddress(config()->get('app.shop_id'), $user->id, $clientAddress->id, $type);
77
            } else {
78
                $clientAddress = ClientService::editAddress($user->id, $id, $userdata);
0 ignored issues
show
Unused Code introduced by
The assignment to $clientAddress is dead and can be removed.
Loading history...
79
            }
80
        } elseif ($type == 'delivery') {
81
            $id = $user->clientDeliveryAddress->id;
82
83
            if ($user->clientDeliveryAddress->id == $user->clientBillAddress->id) {
84
                $clientAddress = ClientService::createAddress($userdata, $user->id);
85
                ClientService::setBillOrDeliveryAddress(config()->get('app.shop_id'), $user->id, $clientAddress->id, $type);
86
            } else {
87
                $clientAddress = ClientService::editAddress($user->id, $id, $userdata);
88
            }
89
        }
90
91
        return redirect()->to('account');
92
        
93
    }
94
95
    public function postEditAccount(Request $request)
96
    {
97
        if (auth('web')->check()) {
98
            $requestChange = ClientService::requestChangeAccountDetails($request->all(), config()->get('app.shop_id'));
99
100
            if ($requestChange) {
101
                $firstname = false;
102
103
                if ($requestChange->clientBillAddress->count()) {
104
                    $firstname = $requestChange->clientBillAddress->firstname;
105
                }
106
107
                $data = array(
108
                    'email' => $requestChange->new_email,
109
                    'firstname' => $firstname,
110
                    'confirmation_code' => $requestChange->confirmation_code
111
                );
112
113
                Mail::send('frontend.email.reset-account-settings-mail', $data, function ($message) use ($data) {
114
                
115
                    $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject('confirm changing account details');
116
                });
117
118
                Notification::success('E-mail sent');
119
            } else {
120
                Notification::error('error');
121
            }
122
        }
123
124
        return redirect()->to('account');
125
    }
126
127
    public function getLogin()
128
    {
129
        return view('frontend.account.login');
130
    }
131
132
    public function postLogin(Request $request)
133
    {
134
        $validateLogin = ClientService::validateLogin($request->all());
135
136
        if ($validateLogin->fails()) {
137
            foreach ($validateLogin->errors()->all() as $error) {
138
                Notification::error($error);
139
            }
140
141
            return redirect()->back()->withInput();
142
        }
143
144
        $login = ClientService::login($request);
145
146
        if($login) {
147
            return redirect()->to('/account');
148
        }
149
        
150
        Notification::error('Not correct.');
151
        return redirect()->back()->withInput(); 
152
    }
153
154
    public function getRegister()
155
    {     
156
        return view('frontend.account.register')->with(array('sendingMethods' => app('shop')->sendingMethods));
157
    }
158
159
    public function postRegister(Request $request)
160
    {
161
        $userdata = $request->all();
162
        $validateRegister = ClientService::validateRegister($userdata);
163
164
        if($validateRegister->fails()) {
165
            foreach ($validator->errors()->all() as $error) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $validator seems to be never defined.
Loading history...
166
                Notification::error($error);
167
            }
168
169
            return redirect()->back()->withInput();
170
        }
171
172
        $register = ClientService::register($userdata, config()->get('app.shop_id'));
173
174
        if ($register) {
175
            $data = $register->toArray();
176
            Mail::send('frontend.email.register-mail', array('user' => $register->toArray(), 'password' => $request->get('password'), 'billAddress' => $register->clientBillAddress->toArray()), function ($message) use ($data) {
177
                $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject(trans('register-completed-subject'));
178
            });
179
            Notification::success(trans('you-are-registered-consumer'));
0 ignored issues
show
Bug introduced by
It seems like trans('you-are-registered-consumer') can also be of type array; however, parameter $message of Krucas\Notification\Faca...Notification::success() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

179
            Notification::success(/** @scrutinizer ignore-type */ trans('you-are-registered-consumer'));
Loading history...
180
            return redirect()->to('account/login');
181
        }
182
183
        Notification::error('Email already exists.');
184
        return redirect()->back()->withInput();
185
    }
186
187
    public function getForgotPassword()
188
    {
189
        return view('frontend.account.forgot-password');
190
    }
191
192
    public function postForgotPassword(Request $request)
193
    {
194
        $rules = array(
195
            'email'            => 'required|email'
196
        );
197
198
        $validator = Validator::make($request->all(), $rules);
199
200
        if ($validator->fails()) {
201
            return redirect()->back()
202
                ->withErrors($validator, 'forgot')->withInput();
203
        }
204
205
        $userdata = $request->all();
206
207
        $forgotPassword = ClientService::getConfirmationCodeByEmail($userdata['email'], config()->get('app.shop_id'));
208
209
        if ($forgotPassword) {
210
            $firstname = false;
211
212
            if ($forgotPassword->clientBillAddress->count()) {
213
                $firstname = $forgotPassword->clientBillAddress->firstname;
214
            }
215
216
            $data = array(
217
                'email' => $userdata['email'],
218
                'firstname' => $firstname,
219
                'code' => $forgotPassword->confirmation_code
220
            );
221
222
            Mail::send('frontend.email.reset-password-mail', $data, function ($message) use ($data) {
223
                $message->to($data['email'])->from('[email protected]', 'Phil & Phae')->subject('Wachtwoord vergeten');
224
            });
225
226
            Notification::success('Er is een e-mail gestuurd. Hiermee kan je je wachtwoord resetten.');
227
228
            return redirect()->back();
229
        }
230
        
231
        Notification::error('Account not exist.');
232
        return redirect()->back()->withErrors($forgotPassword['errors'], 'forgot')->withInput();
233
    }
234
235
    public function getResetPassword($confirmationCode, $email)
236
    {
237
        $result = ClientService::validateConfirmationCode($confirmationCode, $email, config()->get('app.shop_id'));
238
239
        if ($result) {
240
            return view('frontend.account.reset-password')->with(array('confirmationCode' => $confirmationCode, 'email' => $email));
241
        }
242
243
        Notification::error('wachtwoord vergeten is mislukt');
244
        return redirect()->to('account/forgot-password')->withErrors(true, 'forgot')->withInput();
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type Illuminate\Contracts\Sup...geProvider|string|array expected by parameter $provider of Illuminate\Http\RedirectResponse::withErrors(). ( Ignorable by Annotation )

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

244
        return redirect()->to('account/forgot-password')->withErrors(/** @scrutinizer ignore-type */ true, 'forgot')->withInput();
Loading history...
245
    }
246
247
    public function postResetPassword(Request $request, $confirmationCode, $email)
248
    {
249
        $rules = array(
250
            'password'            => 'required'
251
        );
252
253
        $validator = Validator::make($request->all(), $rules);
254
255
        if ($validator->fails()) {
256
            // get the error messages from the validator
257
            $messages = $validator->messages();
0 ignored issues
show
Unused Code introduced by
The assignment to $messages is dead and can be removed.
Loading history...
258
259
            // redirect our user back to the form with the errors from the validator
260
            return redirect()->to('account/reset-password/'.$confirmationCode.'/'.$email)
261
                ->withErrors($validator, 'reset')->withInput();
262
        }
263
264
        $result = ClientService::validateConfirmationCode($confirmationCode, $email, config()->get('app.shop_id'));
265
266
        if ($result) {
267
            $result = ClientService::changePassword(array('confirmation_code' => $confirmationCode, 'email' => $email, 'password' => $request->get('password')), config()->get('app.shop_id'));
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
268
            Notification::success('Je wachtwoord is veranderd en je kan nu inloggen.');
269
            return redirect()->to('account/login');
270
        }
271
    }
272
273
    public function postSubscriberNewsletter(Request $request)
274
    {
275
        $userData = $request->all();
276
        $result = ClientService::subscribeNewsletter($userData['email'], config()->get('app.shop_id'));
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
277
        $result = array(
278
            "result" => true,
279
            "html" => view('frontend.newsletter.completed')->render()
280
        );
281
282
        ClientService::registerMailChimp($userData['email']);
283
        return response()->json($result);
284
    }
285
286
    public function getConfirm($code, $email)
287
    {
288
        $result = ClientService::validateConfirmationCode($code, $email, config()->get('app.shop_id'));
289
290
        if ($result->count()) {
291
            ClientService::confirmClient($code, $email, config()->get('app.shop_id'));
292
            Notification::success('Account is activated.');
293
            return redirect()->to('account/login');
294
        }
295
296
        Notification::error('Information is incorrrect.');
297
        return redirect()->to('account/login');
298
    }
299
300
    public function getLogout(Request $request)
301
    {
302
        auth('web')->logout();
303
        $referrer = $request->headers->get('referer');
304
        if ($referrer) {
305
            if (strpos($referrer, 'checkout') !== false) {
306
                return redirect()->to('cart/checkout');
307
            }
308
        }
309
310
        return redirect()->to('account/login');
311
    } 
312
}