AccountController::postEditAccount()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 16
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 30
rs 9.7333
1
<?php namespace App\Http\Controllers\Frontend;
2
3
use App\Http\Controllers\Controller;
4
use Hideyo\Ecommerce\Framework\Services\Client\ClientFacade as ClientService;
5
use Illuminate\Http\Request;
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('Your account details has been changed. Please login with your new credentials');
28
            auth('web')->logout();
29
            return redirect()->to('account/login');
30
        }
31
        
32
        Notification::error('Not confirmed.');
33
        return redirect()->to('account');
34
    }
35
36
    public function getEditAddress($type)
37
    {
38
        return view('frontend.account.edit-account-address-'.$type)->with(array('sendingMethods' => app('shop')->sendingMethods, 'user' => auth('web')->user()));
39
    }
40
41
    public function postEditAddress(Request $request, $type)
42
    {
43
        $validate = ClientService::validateAddress($request->all());
44
45
        if ($validate->fails()) {
46
            foreach ($validate->errors()->all() as $error) {
47
                Notification::error($error);
48
            }
49
50
            return redirect()->to('account/edit-address/'.$type)->with(array('type' => $type))->withInput();
51
        }
52
53
        $id = auth('web')->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...
54
        if ($type == 'delivery') {
55
            $id = auth('web')->user()->clientDeliveryAddress->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...
56
        }
57
58
        if (auth('web')->user()->clientDeliveryAddress->id == auth('web')->user()->clientBillAddress->id) {
59
            $clientAddress = ClientService::createAddress($request->all(), auth('web')->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...
60
            ClientService::setBillOrDeliveryAddress(config()->get('app.shop_id'), auth('web')->user()->id, $clientAddress->id, $type);  
61
        } else {
62
            $clientAddress = ClientService::editAddress(auth('web')->user()->id, $id, $request->all());
0 ignored issues
show
Unused Code introduced by
The assignment to $clientAddress is dead and can be removed.
Loading history...
63
        }
64
 
65
        return redirect()->to('account');     
66
    }
67
68
    public function postEditAccount(Request $request)
69
    {
70
        if (auth('web')->check()) {
71
            $requestChange = ClientService::requestChangeAccountDetails($request->all(), config()->get('app.shop_id'));
72
73
            if ($requestChange) {
74
                $firstname = false;
75
76
                if ($requestChange->clientBillAddress->count()) {
77
                    $firstname = $requestChange->clientBillAddress->firstname;
78
                }
79
80
                $data = array(
81
                    'email' => $requestChange->new_email,
82
                    'firstname' => $firstname,
83
                    'confirmation_code' => $requestChange->confirmation_code
84
                );
85
86
                Mail::send('frontend.email.reset-account-settings-mail', $data, function ($message) use ($data) {
87
                
88
                    $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject('confirm changing account details');
89
                });
90
91
                Notification::success('E-mail sent');
92
            } else {
93
                Notification::error('error');
94
            }
95
        }
96
97
        return redirect()->to('account');
98
    }
99
100
    public function getLogin()
101
    {
102
        return view('frontend.account.login');
103
    }
104
105
    public function postLogin(Request $request)
106
    {
107
        $validateLogin = ClientService::validateLogin($request->all());
108
109
        if ($validateLogin->fails()) {
110
            foreach ($validateLogin->errors()->all() as $error) {
111
                Notification::error($error);
112
            }
113
114
            return redirect()->back()->withInput();
115
        }
116
117
        if(ClientService::login($request)) {
118
            return redirect()->to('/account');
119
        }
120
        
121
        Notification::error('Not correct.');
122
        return redirect()->back()->withInput(); 
123
    }
124
125
    public function getRegister()
126
    {     
127
        return view('frontend.account.register')->with(array('sendingMethods' => app('shop')->sendingMethods));
128
    }
129
130
    public function postRegister(Request $request)
131
    {
132
        $validateRegister = ClientService::validateRegister($request->all());
133
134
        if($validateRegister->fails()) {
135
            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...
136
                Notification::error($error);
137
            }
138
139
            return redirect()->back()->withInput();
140
        }
141
142
        $register = ClientService::register($request->all(), config()->get('app.shop_id'));
143
144
        if ($register) {
145
            $data = $register->toArray();
146
            Mail::send('frontend.email.register-mail', array('user' => $register->toArray(), 'password' => $request->get('password'), 'billAddress' => $register->clientBillAddress->toArray()), function ($message) use ($data) {
147
                $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject(trans('register-completed-subject'));
148
            });
149
            Notification::success(trans('you-are-registered-consumer'));
150
            return redirect()->to('account/login');
151
        }
152
153
        Notification::error('Email already exists.');
154
        return redirect()->back()->withInput();
155
    }
156
157
    public function getForgotPassword()
158
    {
159
        return view('frontend.account.forgot-password');
160
    }
161
162
    public function postForgotPassword(Request $request)
163
    {
164
        $rules = array(
165
            'email'            => 'required|email'
166
        );
167
168
        $validator = Validator::make($request->all(), $rules);
169
170
        if ($validator->fails()) {
171
            return redirect()->back()
172
                ->withErrors($validator, 'forgot')->withInput();
173
        }
174
175
        $forgotPassword = ClientService::getConfirmationCodeByEmail($request->get('email'), config()->get('app.shop_id'));
176
177
        if ($forgotPassword) {
178
            $firstname = false;
179
180
            if ($forgotPassword->clientBillAddress->count()) {
181
                $firstname = $forgotPassword->clientBillAddress->firstname;
182
            }
183
184
            $data = array(
185
                'email' => $request->get('email'),
186
                'firstname' => $firstname,
187
                'code' => $forgotPassword->confirmation_code
188
            );
189
190
            Mail::send('frontend.email.reset-password-mail', $data, function ($message) use ($data) {
191
                $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject('Forgot password');
192
            });
193
194
            Notification::success('Email is sent with password reset link.');
195
            return redirect()->back();
196
        }
197
        
198
        Notification::error('Account not exist.');
199
        return redirect()->back()->withErrors($forgotPassword['errors'], 'forgot')->withInput();
200
    }
201
202
    public function getResetPassword($confirmationCode, $email)
203
    {
204
        $result = ClientService::validateConfirmationCode($confirmationCode, $email, config()->get('app.shop_id'));
205
206
        if ($result) {
207
            return view('frontend.account.reset-password')->with(array('confirmationCode' => $confirmationCode, 'email' => $email));
208
        }
209
210
        Notification::error('wachtwoord vergeten is mislukt');
211
        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|array|string 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

211
        return redirect()->to('account/forgot-password')->withErrors(/** @scrutinizer ignore-type */ true, 'forgot')->withInput();
Loading history...
212
    }
213
214
    public function postResetPassword(Request $request, $confirmationCode, $email)
215
    {
216
        $rules = array(
217
            'password'            => 'required'
218
        );
219
220
        $validator = Validator::make($request->all(), $rules);
221
222
        if ($validator->fails()) {
223
            return redirect()->to('account/reset-password/'.$confirmationCode.'/'.$email)
224
                ->withErrors($validator, 'reset')->withInput();
225
        }
226
227
        $result = ClientService::validateConfirmationCode($confirmationCode, $email, config()->get('app.shop_id'));
228
229
        if ($result) {
230
            $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...
231
            Notification::success('Password resetting completed. You can now login');
232
            return redirect()->to('account/login');
233
        }
234
    }
235
236
    public function getConfirm($code, $email)
237
    {
238
        $result = ClientService::validateConfirmationCode($code, $email, config()->get('app.shop_id'));
239
240
        if ($result) {
241
            ClientService::confirmClient($code, $email, config()->get('app.shop_id'));
242
            Notification::success('Account is activated.');
243
            return redirect()->to('account/login');
244
        }
245
246
        Notification::error('Information is incorrrect.');
247
        return redirect()->to('account/login');
248
    }
249
250
    public function getLogout(Request $request)
251
    {
252
        auth('web')->logout();
253
        $referer = $request->headers->get('referer');
254
        if ($referer) {
255
            if (strpos($referer, 'checkout') !== false) {
256
                return redirect()->to('cart/checkout');
257
            }
258
        }
259
260
        return redirect()->to('account/login');
261
    } 
262
}