UpdateUserSocialProfileTask::run()   F
last analyzed

Complexity

Conditions 13
Paths 4096

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
c 0
b 0
f 0
rs 2.6145
cc 13
nc 4096
nop 12

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\SocialAuth\Tasks;
4
5
use App\Containers\User\Data\Repositories\UserRepository;
6
use App\Ship\Exceptions\UpdateResourceFailedException;
7
use App\Ship\Parents\Tasks\Task;
8
9
/**
10
 * Class UpdateUserSocialProfileTask.
11
 *
12
 * @author Mahmoud Zalt <[email protected]>
13
 */
14
class UpdateUserSocialProfileTask extends Task
15
{
16
17
    protected $repository;
18
19
    public function __construct(UserRepository $repository)
20
    {
21
        $this->repository = $repository;
22
    }
23
24
    /**
25
     * @param      $userId
26
     * @param null $token
27
     * @param null $expiresIn
28
     * @param null $refreshToken
29
     * @param null $tokenSecret
30
     * @param null $provider
31
     * @param null $avatar
32
     * @param null $avatar_original
33
     * @param null $socialId
34
     * @param null $nickname
35
     * @param null $name
36
     * @param null $email
37
     *
38
     * @return  mixed
39
     * @throws  UpdateResourceFailedException
40
     */
41
    public function run(
42
        $userId,
43
        $token = null,
44
        $expiresIn = null,
45
        $refreshToken = null,
46
        $tokenSecret = null,
47
        $avatar = null,
48
        $avatar_original = null,
49
        $provider = null,
50
        $socialId = null,
51
        $nickname = null,
52
        $name = null,
53
        $email = null
54
    ) {
55
        $attributes = [];
56
57
        if ($token) {
58
            $attributes['social_token'] = $token;
59
        }
60
61
        if ($expiresIn) {
62
            $attributes['social_expires_in'] = $expiresIn;
63
        }
64
65
        if ($refreshToken) {
66
            $attributes['social_refresh_token'] = $refreshToken;
67
        }
68
69
        if ($tokenSecret) {
70
            $attributes['social_token_secret'] = $tokenSecret;
71
        }
72
73
        if ($provider) {
74
            $attributes['social_provider'] = $provider;
75
        }
76
77
        if ($avatar) {
78
            $attributes['social_avatar'] = $avatar;
79
        }
80
81
        if ($avatar_original) {
82
            $attributes['social_avatar_original'] = $avatar_original;
83
        }
84
85
        if ($socialId) {
86
            $attributes['social_id'] = $socialId;
87
        }
88
89
        if ($nickname) {
90
            $attributes['social_nickname'] = $nickname;
91
        }
92
93
        if ($name) {
94
            $attributes['name'] = $name;
95
        }
96
97
        if ($email) {
98
            $attributes['email'] = $email;
99
        }
100
101
        // check if data is empty
102
        if (empty($attributes)) {
103
            throw new UpdateResourceFailedException('Inputs are empty.');
104
        }
105
106
        // updating the attributes
107
        $user = $this->repository->update($attributes, $userId);
108
109
        return $user;
110
    }
111
112
113
}
114