UpdateUserSocialProfileTask::run()   F
last analyzed

Complexity

Conditions 13
Paths 4096

Size

Total Lines 70
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

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