Completed
Push — master ( 7ff671...be21d3 )
by Mahmoud
03:25
created

UpdateUserTask::run()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 25
nc 2
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Containers\User\Tasks;
4
5
use App\Containers\Authentication\Exceptions\UpdateResourceFailedException;
6
use App\Containers\User\Contracts\UserRepositoryInterface;
7
use App\Ship\Parents\Tasks\Task;
8
use Illuminate\Support\Facades\Hash;
9
10
/**
11
 * Class UpdateUserTask.
12
 *
13
 * @author Mahmoud Zalt <[email protected]>
14
 */
15
class UpdateUserTask extends Task
16
{
17
18
    /**
19
     * @var  \App\Containers\User\Contracts\UserRepositoryInterface
20
     */
21
    private $userRepository;
22
23
    /**
24
     * UpdateUserTask constructor.
25
     *
26
     * @param \App\Containers\User\Contracts\UserRepositoryInterface $userRepository
27
     */
28
    public function __construct(UserRepositoryInterface $userRepository)
29
    {
30
        $this->userRepository = $userRepository;
31
    }
32
33
    /**
34
     * @param      $userId
35
     * @param null $password
36
     * @param null $name
37
     * @param null $email
38
     * @param null $gender
39
     * @param null $birth
40
     * @param null $token
41
     * @param null $expiresIn
42
     * @param null $refreshToken
43
     * @param null $tokenSecret
44
     *
45
     * @return  mixed
46
     * @throws \App\Containers\Authentication\Exceptions\UpdateResourceFailedException
47
     */
48
    public function run(
49
        $userId,
50
        $password = null,
51
        $name = null,
52
        $email = null,
53
        $gender = null,
54
        $birth = null,
55
        $token = null,
56
        $expiresIn = null,
57
        $refreshToken = null,
58
        $tokenSecret = null
59
    ) {
60
61
        // set all data in the array, then remove all null values and their keys
62
        $attributes = array_filter([
63
            'password'             => $password ? Hash::make($password) : null,
64
            'name'                 => $name,
65
            'email'                => $email,
66
            'gender'               => $gender,
67
            'birth'                => $birth,
68
            'social_token'         => $token,
69
            'social_expires_in'    => $expiresIn,
70
            'social_refresh_token' => $refreshToken,
71
            'social_token_secret'  => $tokenSecret,
72
        ]);
73
74
        // optionally, check if data is empty and return error
75
        if (!$attributes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attributes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
76
            throw new UpdateResourceFailedException('Inputs are empty.');
77
        }
78
79
        // updating the attributes
80
        $user = $this->userRepository->update($attributes, $userId);
81
82
        return $user;
83
    }
84
85
}
86