Passed
Pull Request — feature/unit-tests (#37)
by Daniel
05:46
created

PasswordResetEmailFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 10
c 1
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Factory\Mailer\User;
15
16
use Silverback\ApiComponentBundle\Entity\User\AbstractUser;
17
use Silverback\ApiComponentBundle\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 function create(AbstractUser $user, array $context = []): ?RawMessage
26
    {
27
        if (!$this->enabled) {
28
            return null;
29
        }
30
31
        $this->initUser($user);
32
33
        $token = $user->getNewEmailVerificationToken();
34
        if (!$token) {
35
            throw new InvalidArgumentException('A new email verification token must be set to send the `email verification` email');
36
        }
37
38
        $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\ApiComponentB...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

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