Test Failed
Pull Request — master (#72)
by Daniel
06:07
created

AbstractUserEmailFactory::getContextKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
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 Psr\Container\ContainerInterface;
17
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
18
use Silverback\ApiComponentsBundle\Event\UserEmailMessageEvent;
19
use Silverback\ApiComponentsBundle\Exception\BadMethodCallException;
20
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
21
use Silverback\ApiComponentsBundle\Exception\RfcComplianceException;
22
use Silverback\ApiComponentsBundle\Exception\UnexpectedValueException;
23
use Silverback\ApiComponentsBundle\Helper\RefererUrlResolver;
24
use Silverback\ApiComponentsBundle\Security\TokenGenerator;
25
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
use Symfony\Component\HttpFoundation\RequestStack;
28
use Symfony\Component\Mime\Address;
29
use Symfony\Component\Mime\Exception\RfcComplianceException as SymfonyRfcComplianceException;
30
use Symfony\Component\Mime\RawMessage;
31
use Symfony\Contracts\Service\ServiceSubscriberInterface;
32
use Twig\Environment;
33
34
/**
35
 * @author Daniel West <[email protected]>
36
 */
37
abstract class AbstractUserEmailFactory implements ServiceSubscriberInterface
38
{
39
    public const MESSAGE_ID_PREFIX = 'xx';
40
41
    protected ContainerInterface $container;
42
    private EventDispatcherInterface $eventDispatcher;
43
    protected string $subject;
44
    protected bool $enabled;
45
    protected ?string $defaultRedirectPath;
46
    protected ?string $redirectPathQueryKey;
47
    protected array $emailContext;
48
    protected ?RawMessage $message;
49
    private AbstractUser $user;
50
51
    public function __construct(ContainerInterface $container, EventDispatcherInterface $eventDispatcher, string $subject, bool $enabled = true, ?string $defaultRedirectPath = null, ?string $redirectPathQueryKey = null, array $emailContext = [])
52
    {
53
        $this->container = $container;
54
        $this->eventDispatcher = $eventDispatcher;
55
        $this->subject = $subject;
56
        $this->enabled = $enabled;
57
        $this->emailContext = $emailContext;
58
        $this->defaultRedirectPath = $defaultRedirectPath;
59
        $this->redirectPathQueryKey = $redirectPathQueryKey;
60
    }
61
62
    public static function getSubscribedServices(): array
63
    {
64
        return [
65
            RequestStack::class,
66
            RefererUrlResolver::class,
67
            Environment::class,
68
        ];
69
    }
70
71
    protected static function getContextKeys(): ?array
72
    {
73
        return [
74
            'website_name',
75
            'user',
76
        ];
77
    }
78
79
    protected function initUser(AbstractUser $user): void
80
    {
81
        if (!$user->getUsername()) {
82
            throw new InvalidArgumentException('The user must have a username set to send them any email');
83
        }
84
85
        if (!$user->getEmailAddress()) {
86
            throw new InvalidArgumentException('The user must have an email address set to send them any email');
87
        }
88
89
        $this->user = $user;
90
    }
91
92
    protected function createEmailMessage(array $context = []): ?TemplatedEmail
93
    {
94
        if (!$this->enabled) {
95
            return null;
96
        }
97
98
        if (!isset($this->user)) {
99
            throw new BadMethodCallException('You must call the method `initUser` before `createEmailMessage`');
100
        }
101
102
        try {
103
            $toEmailAddress = Address::fromString((string) $this->user->getEmailAddress());
104
        } catch (SymfonyRfcComplianceException $exception) {
105
            $exception = new RfcComplianceException($exception->getMessage());
106
            throw $exception;
107
        }
108
109
        $context = array_replace_recursive([
110
            'user' => $this->user,
111
        ], $this->emailContext, $context);
112
        $this->validateContext($context);
113
114
        $twig = $this->container->get(Environment::class);
115
        $template = $twig->createTemplate($this->subject);
116
        $subject = $template->render($context);
117
118
        $email = (new TemplatedEmail())
119
            ->to($toEmailAddress)
120
            ->subject($subject)
121
            ->htmlTemplate('@SilverbackApiComponents/emails/' . $this->getTemplate())
122
            ->context($context);
123
124
        $event = new UserEmailMessageEvent(static::class, $email);
125
        $this->eventDispatcher->dispatch($event);
126
127
        $email->getHeaders()->addTextHeader('X-Message-ID', sprintf('%s-%s', static::MESSAGE_ID_PREFIX, TokenGenerator::generateToken()));
128
129
        return $event->getEmail();
130
    }
131
132
    protected function getTokenUrl(string $token, string $username, ?string $newEmail = null): string
133
    {
134
        $path = $this->populatePathVariables($this->getTokenPath(), [
135
            'token' => $token,
136
            'username' => $username,
137
            'new_email' => $newEmail,
138
        ]);
139
140
        $refererUrlResolver = $this->container->get(RefererUrlResolver::class);
141
142
        return $refererUrlResolver->getAbsoluteUrl($path);
143
    }
144
145
    private function getTokenPath(): string
146
    {
147
        if (null === $this->defaultRedirectPath && null === $this->redirectPathQueryKey) {
148
            throw new InvalidArgumentException('The `defaultRedirectPath` or `redirectPathQueryKey` must be set');
149
        }
150
151
        $requestStack = $this->container->get(RequestStack::class);
152
        $request = $requestStack->getMasterRequest();
153
154
        $path = ($request && $this->redirectPathQueryKey) ?
155
            $request->query->get($this->redirectPathQueryKey, $this->defaultRedirectPath) :
156
            $this->defaultRedirectPath;
157
158
        if (null === $path) {
159
            throw new UnexpectedValueException(sprintf('The querystring key `%s` could not be found in the request to generate a token URL', $this->redirectPathQueryKey));
160
        }
161
162
        return $path;
163
    }
164
165
    private function populatePathVariables(string $path, array $variables): string
166
    {
167
        preg_match_all('/{{[\s]*(\w+)[\s]*}}/', $path, $matches);
168
        foreach ($matches[0] as $matchIndex => $fullMatch) {
169
            if (\array_key_exists($varKey = $matches[1][$matchIndex], $variables) && null !== $variables[$varKey]) {
170
                $path = str_replace($fullMatch, rawurlencode($variables[$varKey]), $path);
171
            }
172
        }
173
174
        return $path;
175
    }
176
177
    private function validateContext(array $context): void
178
    {
179
        $contextKeys = static::getContextKeys();
180
        $keys = array_keys($context);
181
        if (\count($differences = array_diff($contextKeys, $keys))) {
182
            throw new InvalidArgumentException(sprintf('You have not specified required context key(s) for the user email factory factory `%s` (expected: `%s`)', static::class, implode('`, `', $differences)));
183
        }
184
    }
185
186
    abstract public function create(AbstractUser $user, array $context = []): ?RawMessage;
187
188
    abstract protected function getTemplate(): string;
189
}
190