Passed
Push — master ( 1e16c8...f08fae )
by Matthijs
01:55
created

ClientService::confirmClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Client;
4
5
use Validator;
0 ignored issues
show
Bug introduced by
The type Validator was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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; 
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 integer|false 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 integer|false 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
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

72
        $attributes['shop_id'] = /** @scrutinizer ignore-call */ auth('hideyobackend')->user()->selected_shop_id;
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;
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
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

94
        $attributes['shop_id'] = /** @scrutinizer ignore-call */ auth('hideyobackend')->user()->selected_shop_id;
Loading history...
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')
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

135
            'shop_id' => /** @scrutinizer ignore-call */ config()->get('app.shop_id')
Loading history...
136
        );
137
138
        if (auth('web')->attempt($loginData)) {
0 ignored issues
show
Bug introduced by
The function auth was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

138
        if (/** @scrutinizer ignore-call */ auth('web')->attempt($loginData)) {
Loading history...
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) 
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $validator is dead and can be removed.
Loading history...
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;
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...
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)
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

245
    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...
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)
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

259
    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

259
    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...
260
    {
261
        $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...
262
        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

262
        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...
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;
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...
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;
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...
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;
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...
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;
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...
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)
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

369
    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...
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
}