CompleteResetProcessCommandHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
1
<?php namespace Modules\User\Commands\Handlers;
2
3
use Modules\Core\Contracts\Authentication;
4
use Modules\User\Exceptions\InvalidOrExpiredResetCode;
5
use Modules\User\Exceptions\UserNotFoundException;
6
use Modules\User\Repositories\UserRepository;
7
8
class CompleteResetProcessCommandHandler
9
{
10
    protected $input;
11
    /**
12
     * @var UserRepository
13
     */
14
    private $user;
15
    /**
16
     * @var Authentication
17
     */
18
    private $auth;
19
20
    public function __construct(UserRepository $user, Authentication $auth)
21
    {
22
        $this->user = $user;
23
        $this->auth = $auth;
24
    }
25
26
    /**
27
     * Handle the command
28
     *
29
     * @param $command
30
     * @throws InvalidOrExpiredResetCode
31
     * @throws UserNotFoundException
32
     * @return mixed
33
     */
34
    public function handle($command)
35
    {
36
        $this->input = $command;
37
38
        $user = $this->findUser();
39
40
        if (!$this->auth->completeResetPassword($user, $this->input->code, $this->input->password)) {
41
            throw new InvalidOrExpiredResetCode();
42
        }
43
44
        return $user;
45
    }
46
47
    public function findUser()
48
    {
49
        $user = $this->user->find($this->input->userId);
50
        if ($user) {
51
            return $user;
52
        }
53
54
        throw new UserNotFoundException();
55
    }
56
}
57