ClientService::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 19
rs 9.7998
c 0
b 0
f 0
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;
0 ignored issues
show
Bug Best Practice introduced by
The property repo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
		$this->repoAddress = $clientAddress;
0 ignored issues
show
Bug Best Practice introduced by
The property repoAddress does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $clientId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $clientId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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;
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...
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;
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...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$new was never initialized. Although not strictly required by PHP, it is generally a good practice to add $new = array(); before regardless.
Loading history...
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;
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...
95
        $attributes['modified_by_user_id'] = auth('hideyobackend')->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...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.
Loading history...
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));
0 ignored issues
show
Comprehensibility Best Practice introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.
Loading history...
166
            $model->fill($attributes);
167
            $model->save();
168
169
            return $model;
170
        }
171
        
172
        return false;
173
    }
174
175
    public function validateRegister($attributes, $noAccount = false) 
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
        if ($noAccount) {
192
            unset($rules['email']);
193
            unset($rules['password']);
194
        } 
195
196
197
        return $validator = Validator::make($attributes, $rules);
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
198
    }
199
200
    public function register($attributes, $shopId, $accountConfirmed = false)
201
    {
202
        $client = $this->repo->checkEmailByShopId($attributes['email'], $shopId);
203
204
        if($client AND $client->account_created) {
205
        	return false;
206
        }
207
208
        if($client) {
209
        	$model = $client;
210
        } else {
211
        	$model = $this->repo->getModel();
212
        }
213
214
        $attributes['shop_id'] = $shopId;
215
        $attributes['modified_by_user_id'] = null;
216
        $attributes['active'] = 0;
217
        $attributes['confirmed'] = 0;
218
        $attributes['confirmation_code'] = md5(uniqid(mt_rand(), true));      
219
        if($accountConfirmed) {
220
            $attributes['active'] = 1;
221
            $attributes['confirmed'] = 1;
222
            $attributes['confirmation_code'] = null;
223
        }
224
        
225
        if (isset($attributes['password'])) {            
226
            $attributes['password'] = Hash::make($attributes['password']);
227
            $attributes['account_created'] = Carbon::now()->toDateTimeString();
228
        }
229
230
        $model->fill($attributes);
231
        $model->save();
232
233
        $clientAddress = $this->createAddress($attributes, $model->id);
234
        $new['delivery_client_address_id'] = $clientAddress->id;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$new was never initialized. Although not strictly required by PHP, it is generally a good practice to add $new = array(); before regardless.
Loading history...
235
        $new['bill_client_address_id'] = $clientAddress->id;
236
        $model->fill($new);
237
        $model->save();
238
        return $model;
239
    }
240
241
    public function createAddress($attributes, $clientId) 
242
    {
243
        $attributes['client_id'] = $clientId;
244
  		$model = $this->repoAddress->getModel();
245
        $model->fill($attributes);
246
        $model->save();
247
        
248
        return $model;
249
    }
250
251
    public function editAddress($clientId, $addressId, $attributes)
0 ignored issues
show
Unused Code introduced by
The parameter $clientId is not used and could be removed. ( Ignorable by Annotation )

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

251
    public function editAddress(/** @scrutinizer ignore-unused */ $clientId, $addressId, $attributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
252
    {
253
        $clientAddress = $this->repoAddress->find($addressId);
254
255
        if($clientAddress) {
256
            $clientAddress->fill($attributes);
257
            $clientAddress->save();
258
259
            return $clientAddress;
260
        }
261
262
        return false;
263
    }    
264
265
    public function updateByIdAndShopId($shopId, array $attributes, $clientId, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $shopId is not used and could be removed. ( Ignorable by Annotation )

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

265
    public function updateByIdAndShopId(/** @scrutinizer ignore-unused */ $shopId, array $attributes, $clientId, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $clientId is not used and could be removed. ( Ignorable by Annotation )

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

265
    public function updateByIdAndShopId($shopId, array $attributes, /** @scrutinizer ignore-unused */ $clientId, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
266
    {
267
        $this->model = $this->find($id);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
268
        return $this->updateEntity($attributes);
0 ignored issues
show
Bug introduced by
The method updateEntity() does not exist on Hideyo\Ecommerce\Framewo...es\Client\ClientService. ( Ignorable by Annotation )

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

268
        return $this->/** @scrutinizer ignore-call */ updateEntity($attributes);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
269
    }
270
271
    public function requestChangeAccountDetails($attributes, $shopId) {
272
273
        $client = $this->repo->checkEmailByShopId($attributes['email'], $shopId);
274
275
        if ($client) {
276
            $newAttributes = array(
277
                'new_email' => $attributes['email'],
278
                'new_password' => Hash::make($attributes['password']),
279
                'confirmation_code' => md5(uniqid(mt_rand(), true))
280
            );
281
282
            $client->fill($newAttributes);
283
            $client->save();
284
            
285
            return $client;
286
        }
287
288
        return false;
289
    }
290
291
    public function changeAccountDetails($confirmationCode, $newEmail, $shopId) {
292
293
        $client = $this->repo->validateConfirmationCodeByConfirmationCodeAndNewEmail($confirmationCode, $newEmail, $shopId);
294
295
        if ($client) {
296
            $newAttributes['email'] = $client->new_email;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$newAttributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $newAttributes = array(); before regardless.
Loading history...
297
            $newAttributes['password'] = $client->new_password;
298
            $newAttributes['confirmed'] = 1;
299
            $newAttributes['active'] = 1;
300
            $newAttributes['confirmation_code'] = null;
301
            $newAttributes['new_email'] = null;
302
            $newAttributes['new_password'] = null;
303
            $client->fill($newAttributes);
304
            $client->save();
305
            
306
            return $client;
307
308
        }
309
310
        return false;
311
    }
312
313
    public function changePassword(array $attributes, $shopId)
314
    {
315
        $result = array();
316
        $result['result'] = false;
317
318
        $client = $this->repo->validateConfirmationCodeByConfirmationCodeAndEmail($attributes['confirmation_code'], $attributes['email'], $shopId);
319
320
        if ($client) {
321
            if ($attributes['password']) {
322
                $newAttributes['confirmed'] = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$newAttributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $newAttributes = array(); before regardless.
Loading history...
323
                $newAttributes['active'] = 1;
324
                $newAttributes['confirmation_code'] = null;
325
                $newAttributes['password'] = Hash::make($attributes['password']);
326
                $client->fill($newAttributes);
327
                $client->save();
328
                return $client;
329
            }
330
        }
331
332
        return false;
333
    }
334
335
    public function selectAllExport() {
336
        return $this->repo->selectAllExport();
337
    }
338
339
    public function activate($clientId)
340
    {
341
        $model = $this->find($clientId);
342
343
        if ($model) {
344
            $attributes['confirmed'] = 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.
Loading history...
345
            $attributes['active'] = 1;
346
            $attributes['confirmation_code'] = null;
347
            
348
            $model->fill($attributes);
349
            $model->save();
350
            
351
            return $model;
352
        }
353
        
354
        return false;
355
    }
356
357
    public function deactivate($clientId)
358
    {
359
        $model = $this->find($clientId);
360
361
        if ($model) {
362
            $attributes['confirmed'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.
Loading history...
363
            $attributes['active'] = 0;
364
            $attributes['confirmation_code'] = null;
365
            
366
            $model->fill($attributes);
367
            $model->save();
368
            
369
            return $model;
370
        }
371
        
372
        return false;
373
    }
374
375
    public function setBillOrDeliveryAddress($shopId, $clientId, $addressId, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $shopId is not used and could be removed. ( Ignorable by Annotation )

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

375
    public function setBillOrDeliveryAddress(/** @scrutinizer ignore-unused */ $shopId, $clientId, $addressId, $type)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
376
    {
377
        $client = $this->find($clientId);
378
        
379
        if ($client) {
380
381
            $newAttributes = array();
382
383
            if ($type == 'bill') {
384
                $newAttributes['bill_client_address_id'] = $addressId;
385
            } elseif ($type == 'delivery') {
386
                $newAttributes['delivery_client_address_id'] = $addressId;
387
            }
388
            
389
            $client->fill($newAttributes);
390
            $client->save();
391
            return $client;
392
        }
393
        
394
        return false;
395
    }
396
397
    public function selectOneByShopIdAndId($shopId, $clientId)
398
    {
399
        return $this->repo->selectOneByShopIdAndId($shopId, $clientId);
400
    }
401
402
    public function selectAddressesByClientId($clientId) {
403
        return $this->repoAddress->selectAllByClientId($clientId);
404
    }
405
406
    public function getAddressModel()
407
    {
408
        return $this->repoAddress->getModel();
409
    }
410
411
    public function findAddress($clientAddressId)
412
    {
413
        return $this->repoAddress->find($clientAddressId);
414
    }
415
416
    public function validateRegisterNoAccount(array $attributes, $shopId)
417
    {
418
        return $this->repo->validateRegisterNoAccount($attributes, $shopId);
419
    }
420
}