1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hideyo\Ecommerce\Framework\Services\Client; |
4
|
|
|
|
5
|
|
|
use Validator; |
|
|
|
|
6
|
|
|
use File; |
7
|
|
|
use Hideyo\Ecommerce\Framework\Services\Client\Entity\ClientRepository; |
8
|
|
|
use Hideyo\Ecommerce\Framework\Services\Client\Entity\ClientAddressRepository; |
9
|
|
|
use Hideyo\Ecommerce\Framework\Services\BaseService; |
10
|
|
|
use Carbon\Carbon; |
|
|
|
|
11
|
|
|
use Hash; |
12
|
|
|
|
13
|
|
|
class ClientService extends BaseService |
14
|
|
|
{ |
15
|
|
|
public function __construct(ClientRepository $client, ClientAddressRepository $clientAddress) |
16
|
|
|
{ |
17
|
|
|
$this->repo = $client; |
|
|
|
|
18
|
|
|
$this->repoAddress = $clientAddress; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The validation rules for the model. |
23
|
|
|
* |
24
|
|
|
* @param integer $clientId id attribute model |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
private function rules($clientId = false) |
28
|
|
|
{ |
29
|
|
|
if ($clientId) { |
|
|
|
|
30
|
|
|
$rules = array( |
31
|
|
|
'email' => 'required|email|unique_with:client, shop_id' |
32
|
|
|
); |
33
|
|
|
} else { |
34
|
|
|
$rules = array( |
35
|
|
|
'email' => 'required|email|unique_with:'.$this->repo->getModel()->getTable().', shop_id', |
36
|
|
|
'gender' => 'required', |
37
|
|
|
'firstname' => 'required', |
38
|
|
|
'lastname' => 'required', |
39
|
|
|
'street' => 'required', |
40
|
|
|
'housenumber' => 'required|integer', |
41
|
|
|
'zipcode' => 'required', |
42
|
|
|
'city' => 'required', |
43
|
|
|
'country' => 'required' |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($clientId) { |
|
|
|
|
48
|
|
|
$rules['email'] = 'required|email|unique_with:'.$this->repo->getModel()->getTable().', shop_id, '.$clientId.' = id'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $rules; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function validateAddress(array $attributes) |
55
|
|
|
{ |
56
|
|
|
$rules = array( |
57
|
|
|
'firstname' => 'required', |
58
|
|
|
'lastname' => 'required', |
59
|
|
|
'zipcode' => 'required|max:8', |
60
|
|
|
'housenumber' => 'required|numeric', |
61
|
|
|
'street' => 'required', |
62
|
|
|
'city' => 'required', |
63
|
|
|
'country' => 'required' |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return Validator::make($attributes, $rules); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public function create(array $attributes) |
71
|
|
|
{ |
72
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
73
|
|
|
$validator = Validator::make($attributes, $this->rules()); |
74
|
|
|
|
75
|
|
|
if ($validator->fails()) { |
76
|
|
|
return $validator; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$attributes['password'] = Hash::make($attributes['password']); |
80
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
81
|
|
|
$this->repo->getModel()->fill($attributes); |
82
|
|
|
$this->repo->getModel()->save(); |
83
|
|
|
$clientAddress = $this->createAddress($attributes, $this->repo->getModel()->id); |
84
|
|
|
$new['delivery_client_address_id'] = $clientAddress->id; |
|
|
|
|
85
|
|
|
$new['bill_client_address_id'] = $clientAddress->id; |
86
|
|
|
$this->repo->getModel()->fill($new); |
87
|
|
|
$this->repo->getModel()->save(); |
88
|
|
|
return $this->repo->getModel(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function updateById(array $attributes, $clientId) |
92
|
|
|
{ |
93
|
|
|
$model = $this->find($clientId); |
94
|
|
|
$attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id; |
|
|
|
|
95
|
|
|
$attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id; |
96
|
|
|
|
97
|
|
|
$validator = Validator::make($attributes, $this->rules($clientId)); |
98
|
|
|
|
99
|
|
|
if ($validator->fails()) { |
100
|
|
|
return $validator; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($attributes['password']) { |
104
|
|
|
$attributes['password'] = Hash::make($attributes['password']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$model->fill($attributes); |
108
|
|
|
$model->save(); |
109
|
|
|
|
110
|
|
|
return $model; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function validateConfirmationCode($confirmationCode, $email, $shopId) |
114
|
|
|
{ |
115
|
|
|
return $this->repo->validateConfirmationCodeByConfirmationCodeAndEmail($confirmationCode, $email, $shopId); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function validateLogin($attributes) |
119
|
|
|
{ |
120
|
|
|
$rules = array( |
121
|
|
|
'email' => 'required|email', |
122
|
|
|
'password' => 'required|min:2' |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return Validator::make($attributes, $rules); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function login($request) |
129
|
|
|
{ |
130
|
|
|
$loginData = array( |
131
|
|
|
'email' => $request->get('email'), |
132
|
|
|
'password' => $request->get('password'), |
133
|
|
|
'confirmed' => 1, |
134
|
|
|
'active' => 1, |
135
|
|
|
'shop_id' => config()->get('app.shop_id') |
|
|
|
|
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
if (auth('web')->attempt($loginData)) { |
|
|
|
|
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function confirmClient($confirmationCode, $email, $shopId) |
144
|
|
|
{ |
145
|
|
|
$model = $this->repo->getClientByConfirmationCode($shopId, $email, $confirmationCode); |
146
|
|
|
|
147
|
|
|
if ($model) { |
148
|
|
|
$attributes['confirmed'] = 1; |
|
|
|
|
149
|
|
|
$attributes['active'] = 1; |
150
|
|
|
$attributes['confirmation_code'] = null; |
151
|
|
|
|
152
|
|
|
$model->fill($attributes); |
153
|
|
|
$model->save(); |
154
|
|
|
return $model; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getConfirmationCodeByEmail($email, $shopId) |
161
|
|
|
{ |
162
|
|
|
$model = $this->repo->checkEmailByShopIdAndNoAccountCreated($email, $shopId); |
163
|
|
|
|
164
|
|
|
if ($model) { |
165
|
|
|
$attributes['confirmation_code'] = md5(uniqid(mt_rand(), true)); |
|
|
|
|
166
|
|
|
$model->fill($attributes); |
167
|
|
|
$model->save(); |
168
|
|
|
|
169
|
|
|
return $model; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function validateRegister($attributes) |
176
|
|
|
{ |
177
|
|
|
// create the validation rules ------------------------ |
178
|
|
|
$rules = array( |
179
|
|
|
'email' => 'required|email', // required and must be unique in the ducks table |
180
|
|
|
'password' => 'required', |
181
|
|
|
'firstname' => 'required', |
182
|
|
|
'lastname' => 'required', |
183
|
|
|
'zipcode' => 'required', |
184
|
|
|
'housenumber' => 'required|numeric', |
185
|
|
|
'houseletter' => 'alpha', |
186
|
|
|
'street' => 'required', |
187
|
|
|
'city' => 'required', |
188
|
|
|
'country' => 'required' |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
return $validator = Validator::make($attributes, $rules); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function register($attributes, $shopId, $accountConfirmed = false) |
195
|
|
|
{ |
196
|
|
|
$client = $this->repo->checkEmailByShopId($attributes['email'], $shopId); |
197
|
|
|
|
198
|
|
|
if($client AND $client->account_created) { |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if($client) { |
203
|
|
|
$model = $client; |
204
|
|
|
} else { |
205
|
|
|
$model = $this->repo->getModel(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$attributes['shop_id'] = $shopId; |
209
|
|
|
$attributes['modified_by_user_id'] = null; |
210
|
|
|
$attributes['active'] = 0; |
211
|
|
|
$attributes['confirmed'] = 0; |
212
|
|
|
$attributes['confirmation_code'] = md5(uniqid(mt_rand(), true)); |
213
|
|
|
if($accountConfirmed) { |
214
|
|
|
$attributes['active'] = 1; |
215
|
|
|
$attributes['confirmed'] = 1; |
216
|
|
|
$attributes['confirmation_code'] = null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (isset($attributes['password'])) { |
220
|
|
|
$attributes['password'] = Hash::make($attributes['password']); |
221
|
|
|
$attributes['account_created'] = Carbon::now()->toDateTimeString(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$model->fill($attributes); |
225
|
|
|
$model->save(); |
226
|
|
|
|
227
|
|
|
$clientAddress = $this->createAddress($attributes, $model->id); |
228
|
|
|
$new['delivery_client_address_id'] = $clientAddress->id; |
|
|
|
|
229
|
|
|
$new['bill_client_address_id'] = $clientAddress->id; |
230
|
|
|
$model->fill($new); |
231
|
|
|
$model->save(); |
232
|
|
|
return $model; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function createAddress($attributes, $clientId) |
236
|
|
|
{ |
237
|
|
|
$attributes['client_id'] = $clientId; |
238
|
|
|
$model = $this->repoAddress->getModel(); |
239
|
|
|
$model->fill($attributes); |
240
|
|
|
$model->save(); |
241
|
|
|
|
242
|
|
|
return $model; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function editAddress($clientId, $addressId, $attributes) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$clientAddress = $this->repoAddress->find($addressId); |
248
|
|
|
|
249
|
|
|
if($clientAddress) { |
250
|
|
|
$clientAddress->fill($attributes); |
251
|
|
|
$clientAddress->save(); |
252
|
|
|
|
253
|
|
|
return $clientAddress; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function updateByIdAndShopId($shopId, array $attributes, $clientId, $id) |
|
|
|
|
260
|
|
|
{ |
261
|
|
|
$this->model = $this->find($id); |
|
|
|
|
262
|
|
|
return $this->updateEntity($attributes); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function requestChangeAccountDetails($attributes, $shopId) { |
266
|
|
|
|
267
|
|
|
$client = $this->repo->checkEmailByShopId($attributes['email'], $shopId); |
268
|
|
|
|
269
|
|
|
if ($client) { |
270
|
|
|
$newAttributes = array( |
271
|
|
|
'new_email' => $attributes['email'], |
272
|
|
|
'new_password' => Hash::make($attributes['password']), |
273
|
|
|
'confirmation_code' => md5(uniqid(mt_rand(), true)) |
274
|
|
|
); |
275
|
|
|
|
276
|
|
|
$client->fill($newAttributes); |
277
|
|
|
$client->save(); |
278
|
|
|
|
279
|
|
|
return $client; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return false; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function changeAccountDetails($confirmationCode, $newEmail, $shopId) { |
286
|
|
|
|
287
|
|
|
$client = $this->repo->validateConfirmationCodeByConfirmationCodeAndNewEmail($confirmationCode, $newEmail, $shopId); |
288
|
|
|
|
289
|
|
|
if ($client) { |
290
|
|
|
$newAttributes['email'] = $client->new_email; |
|
|
|
|
291
|
|
|
$newAttributes['password'] = $client->new_password; |
292
|
|
|
$newAttributes['confirmed'] = 1; |
293
|
|
|
$newAttributes['active'] = 1; |
294
|
|
|
$newAttributes['confirmation_code'] = null; |
295
|
|
|
$newAttributes['new_email'] = null; |
296
|
|
|
$newAttributes['new_password'] = null; |
297
|
|
|
$client->fill($newAttributes); |
298
|
|
|
$client->save(); |
299
|
|
|
|
300
|
|
|
return $client; |
301
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return false; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function changePassword(array $attributes, $shopId) |
308
|
|
|
{ |
309
|
|
|
$result = array(); |
310
|
|
|
$result['result'] = false; |
311
|
|
|
|
312
|
|
|
$client = $this->repo->validateConfirmationCodeByConfirmationCodeAndEmail($attributes['confirmation_code'], $attributes['email'], $shopId); |
313
|
|
|
|
314
|
|
|
if ($client) { |
315
|
|
|
if ($attributes['password']) { |
316
|
|
|
$newAttributes['confirmed'] = 1; |
|
|
|
|
317
|
|
|
$newAttributes['active'] = 1; |
318
|
|
|
$newAttributes['confirmation_code'] = null; |
319
|
|
|
$newAttributes['password'] = Hash::make($attributes['password']); |
320
|
|
|
$client->fill($newAttributes); |
321
|
|
|
$client->save(); |
322
|
|
|
return $client; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function selectAllExport() { |
330
|
|
|
return $this->repo->selectAllExport(); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function activate($clientId) |
334
|
|
|
{ |
335
|
|
|
$model = $this->find($clientId); |
336
|
|
|
|
337
|
|
|
if ($model) { |
338
|
|
|
$attributes['confirmed'] = 1; |
|
|
|
|
339
|
|
|
$attributes['active'] = 1; |
340
|
|
|
$attributes['confirmation_code'] = null; |
341
|
|
|
|
342
|
|
|
$model->fill($attributes); |
343
|
|
|
$model->save(); |
344
|
|
|
|
345
|
|
|
return $model; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
return false; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function deactivate($clientId) |
352
|
|
|
{ |
353
|
|
|
$model = $this->find($clientId); |
354
|
|
|
|
355
|
|
|
if ($model) { |
356
|
|
|
$attributes['confirmed'] = 0; |
|
|
|
|
357
|
|
|
$attributes['active'] = 0; |
358
|
|
|
$attributes['confirmation_code'] = null; |
359
|
|
|
|
360
|
|
|
$model->fill($attributes); |
361
|
|
|
$model->save(); |
362
|
|
|
|
363
|
|
|
return $model; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
return false; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function setBillOrDeliveryAddress($shopId, $clientId, $addressId, $type) |
|
|
|
|
370
|
|
|
{ |
371
|
|
|
$client = $this->find($clientId); |
372
|
|
|
|
373
|
|
|
if ($client) { |
374
|
|
|
|
375
|
|
|
$newAttributes = array(); |
376
|
|
|
|
377
|
|
|
if ($type == 'bill') { |
378
|
|
|
$newAttributes['bill_client_address_id'] = $addressId; |
379
|
|
|
} elseif ($type == 'delivery') { |
380
|
|
|
$newAttributes['delivery_client_address_id'] = $addressId; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
$client->fill($newAttributes); |
384
|
|
|
$client->save(); |
385
|
|
|
return $client; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return false; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function selectOneByShopIdAndId($shopId, $clientId) |
392
|
|
|
{ |
393
|
|
|
return $this->repo->selectOneByShopIdAndId($shopId, $clientId); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
public function selectAddressesByClientId($clientId) { |
397
|
|
|
return $this->repoAddress->selectAllByClientId($clientId); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
public function getAddressModel() |
401
|
|
|
{ |
402
|
|
|
return $this->repoAddress->getModel(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
public function findAddress($clientAddressId) |
406
|
|
|
{ |
407
|
|
|
return $this->repoAddress->find($clientAddressId); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
public function validateRegisterNoAccount(array $attributes, $shopId) |
411
|
|
|
{ |
412
|
|
|
return $this->repo->validateRegisterNoAccount($attributes, $shopId); |
413
|
|
|
} |
414
|
|
|
} |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths