Passed
Push — master ( 21ee73...1640ca )
by Matthijs
06:07
created

AccountController::postEditAddress()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 6
nop 2
dl 0
loc 25
rs 9.4888
c 0
b 0
f 0
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
        $login = ClientService::login($request);
118
119
        if($login) {
120
            return redirect()->to('/account');
121
        }
122
        
123
        Notification::error('Not correct.');
124
        return redirect()->back()->withInput(); 
125
    }
126
127
    public function getRegister()
128
    {     
129
        return view('frontend.account.register')->with(array('sendingMethods' => app('shop')->sendingMethods));
130
    }
131
132
    public function postRegister(Request $request)
133
    {
134
        $validateRegister = ClientService::validateRegister($request->all());
135
136
        if($validateRegister->fails()) {
137
            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...
138
                Notification::error($error);
139
            }
140
141
            return redirect()->back()->withInput();
142
        }
143
144
        $register = ClientService::register($request->all(), config()->get('app.shop_id'));
145
146
        if ($register) {
147
            $data = $register->toArray();
148
            Mail::send('frontend.email.register-mail', array('user' => $register->toArray(), 'password' => $request->get('password'), 'billAddress' => $register->clientBillAddress->toArray()), function ($message) use ($data) {
149
                $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject(trans('register-completed-subject'));
150
            });
151
            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

151
            Notification::success(/** @scrutinizer ignore-type */ trans('you-are-registered-consumer'));
Loading history...
152
            return redirect()->to('account/login');
153
        }
154
155
        Notification::error('Email already exists.');
156
        return redirect()->back()->withInput();
157
    }
158
159
    public function getForgotPassword()
160
    {
161
        return view('frontend.account.forgot-password');
162
    }
163
164
    public function postForgotPassword(Request $request)
165
    {
166
        $rules = array(
167
            'email'            => 'required|email'
168
        );
169
170
        $validator = Validator::make($request->all(), $rules);
171
172
        if ($validator->fails()) {
173
            return redirect()->back()
174
                ->withErrors($validator, 'forgot')->withInput();
175
        }
176
177
        $forgotPassword = ClientService::getConfirmationCodeByEmail($request->get('email'), config()->get('app.shop_id'));
178
179
        if ($forgotPassword) {
180
            $firstname = false;
181
182
            if ($forgotPassword->clientBillAddress->count()) {
183
                $firstname = $forgotPassword->clientBillAddress->firstname;
184
            }
185
186
            $data = array(
187
                'email' => $request->get('email'),
188
                'firstname' => $firstname,
189
                'code' => $forgotPassword->confirmation_code
190
            );
191
192
            Mail::send('frontend.email.reset-password-mail', $data, function ($message) use ($data) {
193
                $message->to($data['email'])->from('[email protected]', 'Hideyo')->subject('Forgot password');
194
            });
195
196
            Notification::success('Email is sent with password reset link.');
197
            return redirect()->back();
198
        }
199
        
200
        Notification::error('Account not exist.');
201
        return redirect()->back()->withErrors($forgotPassword['errors'], 'forgot')->withInput();
202
    }
203
204
    public function getResetPassword($confirmationCode, $email)
205
    {
206
        $result = ClientService::validateConfirmationCode($confirmationCode, $email, config()->get('app.shop_id'));
207
208
        if ($result) {
209
            return view('frontend.account.reset-password')->with(array('confirmationCode' => $confirmationCode, 'email' => $email));
210
        }
211
212
        Notification::error('wachtwoord vergeten is mislukt');
213
        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

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