Completed
Push — master ( 5b6d3c...e19d53 )
by Marcel
03:17
created

ForgotPasswordManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 6
dl 0
loc 7
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Security\ForgotPassword;
4
5
use App\Entity\ActiveDirectoryUser;
6
use App\Entity\PasswordResetToken;
7
use App\Entity\User;
8
use Psr\Log\NullLogger;
9
use Psr\Log\Test\LoggerInterfaceTest;
10
use Symfony\Contracts\Translation\TranslatorInterface;
11
use Twig\Environment;
12
13
class ForgotPasswordManager {
14
    private $from;
15
    private $passwordManager;
16
    private $mailer;
17
    private $twig;
18
    private $translator;
19
    private $logger;
20
21 1
    public function __construct(string $from, PasswordManager $passwordManager, \Swift_Mailer $mailer, Environment $twig, TranslatorInterface $translator, LoggerInterfaceTest $logger = null) {
22 1
        $this->from = $from;
23 1
        $this->passwordManager = $passwordManager;
24 1
        $this->mailer = $mailer;
25 1
        $this->twig = $twig;
26 1
        $this->translator = $translator;
27 1
        $this->logger = $logger ?? new NullLogger();
28 1
    }
29
30 1
    public function canResetPassword(User $user) {
31 1
        return $user instanceof User && !$user instanceof ActiveDirectoryUser && $user->getEmail() !== null;
32
    }
33
34 1
    public function resetPassword(?User $user) {
35 1
        if($user === null) {
36
            return;
37
        }
38
39 1
        if($this->canResetPassword($user) !== true) {
40
            return;
41
        }
42
43 1
        $token = $this->passwordManager->createPasswordToken($user);
44
45 1
        $content = $this->twig
46 1
            ->render('mail/reset_password.twig', [
47 1
                'token' => $token,
48 1
                'user' => $user
49
            ]);
50
51 1
        $message = (new \Swift_Message())
52 1
            ->setSubject($this->translator->trans('reset_password.title', [], 'mail'))
53 1
            ->setTo($user->getEmail())
54 1
            ->setFrom($this->from)
55 1
            ->setBody($content);
56
57 1
        $this->mailer->send($message);
58 1
    }
59
60 1
    public function updatePassword(PasswordResetToken $token, string $password) {
61 1
        $this->passwordManager->setPassword($token, $password);
62
63 1
        $this->logger
64 1
            ->info(sprintf('User "%s" successfully updated his/her password.', $token->getUser()->getUsername()));
0 ignored issues
show
Bug introduced by
The method info() does not exist on Psr\Log\Test\LoggerInterfaceTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            ->/** @scrutinizer ignore-call */ 
65
              info(sprintf('User "%s" successfully updated his/her password.', $token->getUser()->getUsername()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
    }
66
}