Passed
Push — master ( 81a345...8ff05f )
by Matthijs
01:44
created

ClientService::selectOneByShopIdAndId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
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
48
        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...
49
            $rules['email'] =   'required|email|unique_with:'.$this->repo->getModel()->getTable().', shop_id, '.$clientId.' = id';
50
        }
51
52
        return $rules;
53
    }
54
55
    public function create(array $attributes)
56
    {
57
        $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

57
        $attributes['shop_id'] = /** @scrutinizer ignore-call */ auth('hideyobackend')->user()->selected_shop_id;
Loading history...
58
        $validator = Validator::make($attributes, $this->rules());
59
60
        if ($validator->fails()) {
61
            return $validator;
62
        }
63
64
        $attributes['password'] = Hash::make($attributes['password']);
65
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
66
        $this->repo->getModel()->fill($attributes);
67
        $this->repo->getModel()->save();
68
        $clientAddress = $this->createAddress($attributes, $this->repo->getModel()->id);
69
        $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...
70
        $new['bill_client_address_id'] = $clientAddress->id;
71
        $this->repo->getModel()->fill($new);
72
        $this->repo->getModel()->save();
73
        return $this->repo->getModel();
74
    }
75
76
    public function updateById(array $attributes, $clientId)
77
    {
78
        $model = $this->find($clientId);
79
        $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

79
        $attributes['shop_id'] = /** @scrutinizer ignore-call */ auth('hideyobackend')->user()->selected_shop_id;
Loading history...
80
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
81
82
        $validator = Validator::make($attributes, $this->rules($clientId));
83
84
        if ($validator->fails()) {
85
            return $validator;
86
        }
87
88
        if ($attributes['password']) {
89
            $attributes['password'] = Hash::make($attributes['password']);
90
        }
91
92
        $model->fill($attributes);
93
        $model->save();
94
95
        return $model;
96
    }
97
98
    public function validateConfirmationCode($confirmationCode, $email, $shopId)
99
    {
100
    	return $this->repo->validateConfirmationCodeByConfirmationCodeAndEmail($confirmationCode, $email, $shopId);
101
	}
102
103
    public function validateLogin($attributes) 
104
    {
105
        $rules = array(
106
            'email'            => 'required|email',
107
            'password'         => 'required|min:2'
108
        );
109
110
        return Validator::make($attributes, $rules);
111
    }
112
113
    public function login($request) {
114
115
        $loginData = array(
116
            'email' => $request->get('email'),
117
            'password' => $request->get('password'),
118
            'confirmed' => 1,
119
            'active' => 1,
120
            '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

120
            'shop_id' => /** @scrutinizer ignore-call */ config()->get('app.shop_id')
Loading history...
121
        );
122
123
        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

123
        if (/** @scrutinizer ignore-call */ auth('web')->attempt($loginData)) {
Loading history...
124
            return true;
125
        }
126
127
128
    }
129
130
    public function confirmClient($confirmationCode, $email, $shopId)
131
    {
132
        $model = $this->repo->getClientByConfirmationCode($shopId, $email, $confirmationCode);
133
134
        if ($model) {
135
            $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...
136
            $attributes['active'] = 1;
137
            $attributes['confirmation_code'] = null;
138
            
139
            $model->fill($attributes);
140
            $model->save();
141
            return $model;
142
        }
143
        
144
        return false;
145
    }
146
147
    public function getConfirmationCodeByEmail($email, $shopId)
148
    {
149
        $model = $this->repo->checkEmailByShopIdAndNoAccountCreated($email, $shopId);
150
151
        if ($model) {
152
            $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...
153
            $model->fill($attributes);
154
            $model->save();
155
156
            return $model;
157
        }
158
        
159
        return false;
160
    }
161
162
    public function validateRegister($attributes) {
163
164
        // create the validation rules ------------------------
165
        $rules = array(
166
            'email'         => 'required|email',     // required and must be unique in the ducks table
167
            'password'      => 'required',
168
            'firstname'     => 'required',
169
            'lastname'      => 'required',
170
            'zipcode'       => 'required',
171
            'housenumber'   => 'required|numeric',
172
            'houseletter'   => 'alpha',
173
            'street'        => 'required',
174
            'city'          => 'required',
175
            'country'       => 'required'
176
        );
177
178
        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...
179
    }
180
181
    public function register($attributes, $shopId, $accountConfirmed = false)
182
    {
183
        $client = $this->repo->checkEmailByShopId($attributes['email'], $shopId);
184
185
        if($client AND $client->account_created) {
186
        	return false;
187
        }
188
189
        if($client) {
190
        	$model = $client;
191
        } else {
192
        	$model = $this->repo->getModel();
193
        }
194
195
        $attributes['shop_id'] = $shopId;
196
        $attributes['modified_by_user_id'] = null;
197
        $attributes['active'] = 0;
198
        $attributes['confirmed'] = 0;
199
        $attributes['confirmation_code'] = md5(uniqid(mt_rand(), true));      
200
        if($accountConfirmed) {
201
            $attributes['active'] = 1;
202
            $attributes['confirmed'] = 1;
203
            $attributes['confirmation_code'] = null;
204
        }
205
        
206
        if (isset($attributes['password'])) {            
207
            $attributes['password'] = Hash::make($attributes['password']);
208
            $attributes['account_created'] = Carbon::now()->toDateTimeString();
209
        }
210
211
        $model->fill($attributes);
212
        $model->save();
213
214
        $clientAddress = $this->createAddress($attributes, $model->id);
215
        $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...
216
        $new['bill_client_address_id'] = $clientAddress->id;
217
        $model->fill($new);
218
        $model->save();
219
        return $model;
220
    }
221
222
    public function createAddress($attributes, $clientId) 
223
    {
224
        $attributes['client_id'] = $clientId;
225
  		$model = $this->repoAddress->getModel();
226
        $model->fill($attributes);
227
        $model->save();
228
        
229
        return $model;
230
    }
231
232
    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

232
    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...
233
    {
234
235
        $clientAddress = $this->repoAddress->find($addressId);
236
237
        if($clientAddress) {
238
            $clientAddress->fill($attributes);
239
            $clientAddress->save();
240
241
            return $clientAddress;
242
        }
243
244
        return false;
245
    }    
246
247
    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

247
    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

247
    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...
248
    {
249
        $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...
250
        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

250
        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...
251
    }
252
253
    public function requestChangeAccountDetails($attributes, $shopId) {
254
255
        $client = $this->repo->checkEmailByShopId($attributes['email'], $shopId);
256
257
        if ($client) {
258
            $newAttributes = array(
259
                'new_email' => $attributes['email'],
260
                'new_password' => Hash::make($attributes['password']),
261
                'confirmation_code' => md5(uniqid(mt_rand(), true))
262
            );
263
264
            $client->fill($newAttributes);
265
            $client->save();
266
            
267
            return $client;
268
        }
269
270
        return false;
271
    }
272
273
    public function changeAccountDetails($confirmationCode, $newEmail, $shopId) {
274
275
        $client = $this->repo->validateConfirmationCodeByConfirmationCodeAndNewEmail($confirmationCode, $newEmail, $shopId);
276
277
        if ($client) {
278
            $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...
279
            $newAttributes['password'] = $client->new_password;
280
            $newAttributes['confirmed'] = 1;
281
            $newAttributes['active'] = 1;
282
            $newAttributes['confirmation_code'] = null;
283
            $newAttributes['new_email'] = null;
284
            $newAttributes['new_password'] = null;
285
            $client->fill($newAttributes);
286
            $client->save();
287
            
288
            return $client;
289
290
        }
291
292
        return false;
293
    }
294
295
    public function changePassword(array $attributes, $shopId)
296
    {
297
        $result = array();
298
        $result['result'] = false;
299
300
        $client = $this->repo->validateConfirmationCodeByConfirmationCodeAndEmail($attributes['confirmation_code'], $attributes['email'], $shopId);
301
302
        if ($client) {
303
            if ($attributes['password']) {
304
                $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...
305
                $newAttributes['active'] = 1;
306
                $newAttributes['confirmation_code'] = null;
307
                $newAttributes['password'] = Hash::make($attributes['password']);
308
                $client->fill($newAttributes);
309
                $client->save();
310
                return $client;
311
            }
312
        }
313
314
        return false;
315
    }
316
317
318
    public function selectAllExport() {
319
        return $this->repo->selectAllExport();
320
    }
321
322
323
    public function activate($clientId)
324
    {
325
        $model = $this->find($clientId);
326
327
        if ($model) {
328
            $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...
329
            $attributes['active'] = 1;
330
            $attributes['confirmation_code'] = null;
331
            
332
            $model->fill($attributes);
333
            $model->save();
334
            
335
            return $model;
336
        }
337
        
338
        return false;
339
    }
340
341
    public function deactivate($clientId)
342
    {
343
        $model = $this->find($clientId);
344
345
        if ($model) {
346
            $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...
347
            $attributes['active'] = 0;
348
            $attributes['confirmation_code'] = null;
349
            
350
            $model->fill($attributes);
351
            $model->save();
352
            
353
            return $model;
354
        }
355
        
356
        return false;
357
    }
358
359
    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

359
    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...
360
    {
361
        $client = $this->find($clientId);
362
        
363
        if ($client) {
364
365
            $newAttributes = array();
366
367
            if ($type == 'bill') {
368
                $newAttributes['bill_client_address_id'] = $addressId;
369
            } elseif ($type == 'delivery') {
370
                $newAttributes['delivery_client_address_id'] = $addressId;
371
            }
372
            
373
            $client->fill($newAttributes);
374
            $client->save();
375
            return $client;
376
        }
377
        
378
        return false;
379
    }
380
381
382
    public function selectOneByShopIdAndId($shopId, $clientId)
383
    {
384
        return $this->repo->selectOneByShopIdAndId($shopId, $clientId);
385
    }
386
387
388
    public function selectAddressesByClientId($clientId) {
389
        return $this->repoAddress->selectAllByClientId($clientId);
390
    }
391
392
    public function getAddressModel()
393
    {
394
        return $this->repoAddress->getModel();
395
    }
396
397
    public function findAddress($clientAddressId)
398
    {
399
        return $this->repoAddress->find($clientAddressId);
400
    }
401
402
403
    public function validateRegisterNoAccount(array $attributes, $shopId)
404
    {
405
        return $this->repo->validateRegisterNoAccount($attributes, $shopId);
406
407
    }
408
409
410
}