Completed
Push — master ( d8e2a0...679457 )
by Mahmoud
04:23
created

UpdateUserTask::run()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 12
nc 16
nop 4
1
<?php
2
3
namespace App\Containers\User\Tasks;
4
5
use App\Containers\ApiAuthentication\Exceptions\UpdateResourceFailedException;
6
use App\Containers\User\Contracts\UserRepositoryInterface;
7
use App\Port\Task\Abstracts\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
     * UpdateUserAction 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
    /**
35
     * @param      $userId
36
     * @param null $password
37
     * @param null $name
38
     * @param null $email
39
     *
40
     * @return  mixed
41
     */
42
    public function run($userId, $password = null, $name = null, $email = null)
43
    {
44
        $attributes = [];
45
46
        if ($password) {
47
            $attributes['password'] = Hash::make($password);
48
        }
49
50
        if ($name) {
51
            $attributes['name'] = $name;
52
        }
53
54
        if ($email) {
55
            $attributes['email'] = $email;
56
        }
57
58
        // check if data is empty
59
        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...
60
            throw new UpdateResourceFailedException('Inputs are empty.');
61
        }
62
63
        // updating the attributes
64
        $user = $this->userRepository->update($attributes, $userId);
65
66
        return $user;
67
    }
68
69
70
}
71