Completed
Push — master ( d86698...3d0fab )
by
unknown
03:08
created

UpdateUserTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\User\Tasks;
4
5
use App\Containers\User\Data\Repositories\UserRepository;
6
use App\Containers\User\Models\User;
7
use App\Ship\Exceptions\InternalErrorException;
8
use App\Ship\Exceptions\NotFoundException;
9
use App\Ship\Exceptions\UpdateResourceFailedException;
10
use App\Ship\Parents\Tasks\Task;
11
use Exception;
12
use Illuminate\Database\Eloquent\ModelNotFoundException;
13
14
/**
15
 * Class UpdateUserTask.
16
 *
17
 * @author Mahmoud Zalt <[email protected]>
18
 */
19
class UpdateUserTask extends Task
20
{
21
22
    protected $repository;
23
24
    public function __construct(UserRepository $repository)
25
    {
26
        $this->repository = $repository;
27
    }
28
29
    /**
30
     * @param $userData
31
     * @param $userId
32
     *
33
     * @return mixed
34
     * @throws InternalErrorException
35
     * @throws NotFoundException
36
     * @throws UpdateResourceFailedException
37
     *
38
     * @return  \App\Containers\User\Models\User
39
     */
40
    public function run($userData, $userId): User
41
    {
42
        if (empty($userData)) {
43
            throw new UpdateResourceFailedException('Inputs are empty.');
44
        }
45
46
        try {
47
            $user = $this->repository->update($userData, $userId);
48
        } catch (ModelNotFoundException $exception) {
49
            throw new NotFoundException('User Not Found.');
50
        } catch (Exception $exception) {
51
            throw new InternalErrorException();
52
        }
53
54
        return $user;
55
    }
56
57
}
58