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