Issues (62)

src/CommandHandler/ResetPasswordCommandHandler.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\CommandHandler;
4
5
use App\Command\ResetPasswordCommand;
6
use App\Repository\UserRepository;
7
use Symfony\Component\Messenger\MessageBusInterface;
8
use App\CommandHandler\Exception\PasswordResetException;
0 ignored issues
show
The type App\CommandHandler\Excep...\PasswordResetException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use App\Repository\Exception\UserNotFoundException;
10
use Psr\Log\LoggerInterface;
11
use App\Entity\User;
12
13
class ResetPasswordCommandHandler implements CommandHandlerInterface
14
{
15
    /**
16
     * @var UserRepository
17
     */
18
    private $repository;
19
    /**
20
     * @var MessageBusInterface
21
     */
22
    private $eventBus;
23
    /**
24
     * @var LoggerInterface
25
     */
26
    private $logger;
27
28
    /**
29
     * @param MessageBusInterface $eventBus
30
     * @param UserRepository $repository
31
     * @param LoggerInterface $logger
32
     */
33
    public function __construct(
34
        MessageBusInterface $eventBus, 
35
        UserRepository $repository, 
36
        LoggerInterface $logger
37
    )
38
    {
39
        $this->eventBus = $eventBus;
40
        $this->repository = $repository;
41
        $this->logger = $logger;
42
    }
43
44
    /**
45
     * @param ResetPasswordCommand $command
46
     */
47
    public function __invoke(ResetPasswordCommand $command)
48
    {
49
        try {
50
            $email = filter_var($command->getEmail(), FILTER_SANITIZE_EMAIL);
51
            $token = bin2hex(random_bytes(32));
52
            $user = $this->repository->findOneBy(['email' => $email]);
53
            
54
            if (!$user instanceof User) {
55
                throw new UserNotFoundException('User not found');
56
            }
57
58
            $user->setPasswordRequestToken($token);
59
            $this->repository->save($user);
60
        } catch (\Exception $e) {
61
            $this->logger->error($e->getMessage());
62
            throw new PasswordResetException('Pasword reset error: '.$e->getMessage());
63
        }
64
65
    }
66
67
}
68