File size: 2,290 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

declare(strict_types=1);

namespace User\Application\CommandHandlers;

use Psr\EventDispatcher\EventDispatcherInterface;
use User\Application\Commands\CreatePasswordRecoveryCommand;
use User\Domain\Events\PasswordRecoveryCreatedEvent;
use User\Domain\Exceptions\UserNotFoundException;
use User\Domain\Repositories\UserRepositoryInterface;
use User\Domain\ValueObjects\Status;

/** 
 * Class CreatePasswordRecoveryCommandHandler
 *
 * This class handles the CreatePasswordRecoveryCommand. 
 * It uses UserRepositoryInterface to interact with user data and 
 * EventDispatcherInterface to dispatch events.
 */
class CreatePasswordRecoveryCommandHandler
{
    /**
     * Constructs a new instance of CreatePasswordRecoveryCommandHandler.
     *
     * @param UserRepositoryInterface $repo An instance of UserRepositoryInterface.
     * @param EventDispatcherInterface $dispatcher An instance of EventDispatcherInterface.
     */
    public function __construct(
        private UserRepositoryInterface $repo,
        private EventDispatcherInterface $dispatcher
    ) {
    }

    /**
     * Handles the provided CreatePasswordRecoveryCommand.
     *
     * It retrieves the user with the email provided in the command. If the user 
     * status is not active, throws UserNotFoundException. Generates a new 
     * recovery token for the user and then flushes changes to the repository. 
     * Dispatches a PasswordRecoveryCreatedEvent afterwards.
     *
     * @param CreatePasswordRecoveryCommand $cmd The command to handle.
     * @return void
     * @throws UserNotFoundException
     */
    public function handle(CreatePasswordRecoveryCommand $cmd): void
    {
        // Retrieve the user with given email from the repository
        $user = $this->repo->ofEmail($cmd->email);

        // Check if user is active. If not, throw a UserNotFoundException.
        if ($user->getStatus() !== Status::ACTIVE) {
            throw new UserNotFoundException($cmd->email);
        }

        // Generate a recovery token for the user
        $user->generateRecoveryToken();

        // Create and dispatch a new PasswordRecoveryCreatedEvent with the 
        // updated user
        $event = new PasswordRecoveryCreatedEvent($user);
        $this->dispatcher->dispatch($event);
    }
}