Test Failed
Pull Request — master (#72)
by Daniel
05:27
created

ChangeEmailConfirmationEmailFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 17 3
A getTemplate() 0 3 1
A getContextKeys() 0 4 1
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 ChangeEmailConfirmationEmailFactory extends AbstractUserEmailFactory
24
{
25
    public const MESSAGE_ID_PREFIX = 'cec';
26
27
    public function create(AbstractUser $user, array $context = []): ?RawMessage
28
    {
29
        if (!$this->enabled) {
30
            return null;
31
        }
32
33
        $this->initUser($user);
34
35
        $token = $user->plainNewEmailConfirmationToken;
36
        $user->plainNewEmailConfirmationToken = null;
37
        if (!$token) {
38
            throw new InvalidArgumentException('A `new email confirmation token` must be set to send the confirmation email');
39
        }
40
41
        $context['redirect_url'] = $this->getTokenUrl($token, $user->getUsername(), $user->getNewEmailAddress());
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(), $user->getNewEmailAddress());
Loading history...
42
43
        return $this->createEmailMessage($context);
44
    }
45
46
    protected static function getContextKeys(): ?array
47
    {
48
        return array_merge(parent::getContextKeys(), [
49
            'redirect_url',
50
        ]);
51
    }
52
53
    protected function getTemplate(): string
54
    {
55
        return 'user_change_email_confirmation.html.twig';
56
    }
57
}
58