Passed
Pull Request — master (#72)
by Daniel
06:31
created

PasswordResetEmailFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Factory\User\Mailer;
15
16
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
17
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
18
use Symfony\Component\Mime\RawMessage;
19
20
/**
21
 * @author Daniel West <[email protected]>
22
 */
23
final class PasswordResetEmailFactory extends AbstractUserEmailFactory
24
{
25
    public const MESSAGE_ID_PREFIX = 'pre';
26
27 3
    public function create(AbstractUser $user, array $context = []): ?RawMessage
28
    {
29 3
        if (!$this->enabled) {
30 1
            return null;
31
        }
32
33 2
        $this->initUser($user);
34
35 2
        $token = $user->plainNewPasswordConfirmationToken;
36 2
        $user->plainNewPasswordConfirmationToken = null;
37 2
        if (!$token) {
38 1
            throw new InvalidArgumentException('A new password confirmation token must be set to send the `password reset` email');
39
        }
40
41 1
        $context['redirect_url'] = $this->getTokenUrl($token, $user->getUsername());
0 ignored issues
show
Bug introduced by
It seems like $user->getUsername() can also be of type null; however, parameter $username of Silverback\ApiComponents...lFactory::getTokenUrl() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

41
        $context['redirect_url'] = $this->getTokenUrl($token, /** @scrutinizer ignore-type */ $user->getUsername());
Loading history...
42
43 1
        return $this->createEmailMessage($context);
44
    }
45
46 1
    protected static function getContextKeys(): ?array
47
    {
48 1
        return array_merge(parent::getContextKeys(), [
49 1
            'redirect_url',
50
        ]);
51
    }
52
53 1
    protected function getTemplate(): string
54
    {
55 1
        return 'user_password_reset.html.twig';
56
    }
57
}
58