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