Test Failed
Push — develop ( 28e0cd...307ddb )
by Daniel
05:05
created

Mailer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 53
dl 0
loc 85
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A newUsernameConfirmation() 0 26 1
A pathToAppUrl() 0 10 2
A __construct() 0 10 1
A passwordResetEmail() 0 26 1
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Mailer;
4
5
use Silverback\ApiComponentBundle\Entity\User\User;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
use Twig\Environment;
8
9
class Mailer
10
{
11
    private $mailer;
12
    private $twig;
13
    private $requestStack;
14
    private $fromEmailAddress;
15
16
    public function __construct(
17
        \Swift_Mailer $mailer,
18
        Environment $twig,
19
        RequestStack $requestStack,
20
        string $fromEmailAddress
21
    ) {
22
        $this->mailer = $mailer;
23
        $this->twig = $twig;
24
        $this->requestStack = $requestStack;
25
        $this->fromEmailAddress = $fromEmailAddress;
26
    }
27
28
    public function passwordResetEmail(User $user, string $resetUrl): int
29
    {
30
        $resetUrl = $this->pathToAppUrl(
31
            $resetUrl,
32
            $user->getPasswordResetConfirmationToken(),
0 ignored issues
show
Bug introduced by
It seems like $user->getPasswordResetConfirmationToken() can also be of type null; however, parameter $token of Silverback\ApiComponentB...\Mailer::pathToAppUrl() 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

32
            /** @scrutinizer ignore-type */ $user->getPasswordResetConfirmationToken(),
Loading history...
33
            $user->getUsername()
0 ignored issues
show
Bug introduced by
It seems like $user->getUsername() can also be of type null; however, parameter $email of Silverback\ApiComponentB...\Mailer::pathToAppUrl() 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

33
            /** @scrutinizer ignore-type */ $user->getUsername()
Loading history...
34
        );
35
        $message = (new \Swift_Message('Password Reset Request'))
36
            ->setFrom($this->fromEmailAddress)
37
            ->setTo($user->getUsername())
38
            ->setBody(
39
                $this->twig->render(
40
                    '@SilverbackApiComponent/emails/password_reset.html.twig',
41
                    ['user' => $user, 'reset_url' => $resetUrl]
42
                ),
43
                'text/html'
44
            )
45
            ->addPart(
46
                $this->twig->render(
47
                    '@SilverbackApiComponent/emails/password_reset.txt.twig',
48
                    ['user' => $user, 'reset_url' => $resetUrl]
49
                ),
50
                'text/plain'
51
            )
52
        ;
53
        return $this->mailer->send($message);
54
    }
55
56
    public function newUsernameConfirmation(User $user, string $confirmUrl): int
57
    {
58
        $confirmUrl = $this->pathToAppUrl(
59
            $confirmUrl,
60
            $user->getUsernameConfirmationToken(),
0 ignored issues
show
Bug introduced by
It seems like $user->getUsernameConfirmationToken() can also be of type null; however, parameter $token of Silverback\ApiComponentB...\Mailer::pathToAppUrl() 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

60
            /** @scrutinizer ignore-type */ $user->getUsernameConfirmationToken(),
Loading history...
61
            $user->getNewUsername()
0 ignored issues
show
Bug introduced by
It seems like $user->getNewUsername() can also be of type null; however, parameter $email of Silverback\ApiComponentB...\Mailer::pathToAppUrl() 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

61
            /** @scrutinizer ignore-type */ $user->getNewUsername()
Loading history...
62
        );
63
        $message = (new \Swift_Message('Confirm change of username'))
64
            ->setFrom($this->fromEmailAddress)
65
            ->setTo($user->getNewUsername())
66
            ->setBody(
67
                $this->twig->render(
68
                    '@SilverbackApiComponent/emails/new_username_confirmation.html.twig',
69
                    ['user' => $user, 'confirm_url' => $confirmUrl]
70
                ),
71
                'text/html'
72
            )
73
            ->addPart(
74
                $this->twig->render(
75
                    '@SilverbackApiComponent/emails/new_username_confirmation.txt.twig',
76
                    ['user' => $user, 'confirm_url' => $confirmUrl]
77
                ),
78
                'text/plain'
79
            )
80
        ;
81
        return $this->mailer->send($message);
82
    }
83
84
    private function pathToAppUrl(string $path, string $token, string $email): string
85
    {
86
        $request = $this->requestStack->getCurrentRequest();
87
        $urlParts = $request ? parse_url($request->headers->get('referer')) : ['scheme' => 'https', 'host' => 'no-referrer'];
88
        $path = str_replace(['{{ token }}', '{{ email }}'], [$token, $email], $path);
89
        return sprintf(
90
            '%s://%s/%s',
91
            $urlParts['scheme'],
92
            $urlParts['host'],
93
            ltrim($path, '/')
94
        );
95
    }
96
}
97