|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 | +namespace Symfony\Bridge\Twig\Mime; |
| 13 | + |
| 14 | +use Symfony\Component\ErrorRenderer\Exception\FlattenException; |
| 15 | +use Symfony\Component\Mime\Header\Headers; |
| 16 | +use Symfony\Component\Mime\Part\AbstractPart; |
| 17 | +use Twig\Extra\CssInliner\CssInlinerExtension; |
| 18 | +use Twig\Extra\Inky\InkyExtension; |
| 19 | +use Twig\Extra\Markdown\MarkdownExtension; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Fabien Potencier <fabien@symfony.com> |
| 23 | + */ |
| 24 | +class NotificationEmail extends TemplatedEmail |
| 25 | +{ |
| 26 | + public const URGENT = 'urgent'; |
| 27 | + public const HIGH = 'high'; |
| 28 | + public const MEDIUM = 'medium'; |
| 29 | + public const LOW = 'low'; |
| 30 | + |
| 31 | + private static $htmlTemplatePath = '@email/notification.html.twig'; |
| 32 | + private static $textTemplatePath = '@email/notification.txt.twig'; |
| 33 | + |
| 34 | + private $context = [ |
| 35 | + 'importance' => 'low', |
| 36 | + 'content' => '', |
| 37 | + 'exception' => false, |
| 38 | + 'action_url' => null, |
| 39 | + 'action_text' => null, |
| 40 | + 'markdown' => false, |
| 41 | + 'raw' => false, |
| 42 | + ]; |
| 43 | + |
| 44 | + public function __construct(Headers $headers = null, AbstractPart $body = null) |
| 45 | + { |
| 46 | + if (!class_exists(CssInlinerExtension::class)) { |
| 47 | + throw new \LogicException(sprintf('You cannot use "%s" if the CSS Inliner Twig extension is not available; try running "composer require twig/cssinliner-extra".', static::class)); |
| 48 | + } |
| 49 | + |
| 50 | + if (!class_exists(InkyExtension::class)) { |
| 51 | + throw new \LogicException(sprintf('You cannot use "%s" if the Inky Twig extension is not available; try running "composer require twig/inky-extra".', static::class)); |
| 52 | + } |
| 53 | + |
| 54 | + parent::__construct($headers, $body); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return $this |
| 59 | + */ |
| 60 | + public function markdown() |
| 61 | + { |
| 62 | + if (!class_exists(MarkdownExtension::class)) { |
| 63 | + throw new \LogicException(sprintf('You cannot use "%s" if the Markdown Twig extension is not available; try running "composer require twig/markdown-extra".', __METHOD__)); |
| 64 | + } |
| 65 | + |
| 66 | + $this->context['markdown'] = true; |
| 67 | + |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return $this |
| 73 | + */ |
| 74 | + public function content(string $content, bool $raw = false) |
| 75 | + { |
| 76 | + $this->context['content'] = $content; |
| 77 | + $this->context['raw'] = $raw; |
| 78 | + |
| 79 | + return $this; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return $this |
| 84 | + */ |
| 85 | + public function action(string $text, string $url) |
| 86 | + { |
| 87 | + $this->context['action_text'] = $text; |
| 88 | + $this->context['action_url'] = $url; |
| 89 | + |
| 90 | + return $this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return self |
| 95 | + */ |
| 96 | + public function importance(string $importance) |
| 97 | + { |
| 98 | + $this->context['importance'] = $importance; |
| 99 | + |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param \Throwable|FlattenException |
| 105 | + * |
| 106 | + * @return $this |
| 107 | + */ |
| 108 | + public function exception($exception) |
| 109 | + { |
| 110 | + $exceptionAsString = $this->getExceptionAsString($exception); |
| 111 | + |
| 112 | + $this->context['exception'] = true; |
| 113 | + $this->attach($exceptionAsString, 'exception.txt', 'text/plain'); |
| 114 | + $this->importance(self::URGENT); |
| 115 | + |
| 116 | + if (!$this->getSubject()) { |
| 117 | + $this->subject($exception->getMessage()); |
| 118 | + } |
| 119 | + |
| 120 | + return $this; |
| 121 | + } |
| 122 | + |
| 123 | + public static function setDefaultTemplates(string $htmlTemplatePath = null, string $textTemplatePath = null) |
| 124 | + { |
| 125 | + if ($htmlTemplatePath) { |
| 126 | + self::$htmlTemplatePath = $htmlTemplatePath; |
| 127 | + } |
| 128 | + |
| 129 | + if ($textTemplatePath) { |
| 130 | + self::$textTemplatePath = $textTemplatePath; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public function getTextTemplate(): ?string |
| 135 | + { |
| 136 | + if ($template = parent::getTextTemplate()) { |
| 137 | + return $template; |
| 138 | + } |
| 139 | + |
| 140 | + return self::$textTemplatePath; |
| 141 | + } |
| 142 | + |
| 143 | + public function getHtmlTemplate(): ?string |
| 144 | + { |
| 145 | + if ($template = parent::getHtmlTemplate()) { |
| 146 | + return $template; |
| 147 | + } |
| 148 | + |
| 149 | + return self::$htmlTemplatePath; |
| 150 | + } |
| 151 | + |
| 152 | + public function getContext(): array |
| 153 | + { |
| 154 | + return array_merge($this->context, parent::getContext()); |
| 155 | + } |
| 156 | + |
| 157 | + public function getPreparedHeaders(): Headers |
| 158 | + { |
| 159 | + $headers = parent::getPreparedHeaders(); |
| 160 | + |
| 161 | + $importance = $this->context['importance'] ?? self::LOW; |
| 162 | + $this->priority($this->determinePriority($importance)); |
| 163 | + $headers->setHeaderBody('Text', 'Subject', sprintf('[%s] %s', strtoupper($importance), $this->getSubject())); |
| 164 | + |
| 165 | + return $headers; |
| 166 | + } |
| 167 | + |
| 168 | + private function determinePriority(string $importance): int |
| 169 | + { |
| 170 | + switch ($importance) { |
| 171 | + case self::URGENT: |
| 172 | + return self::PRIORITY_HIGHEST; |
| 173 | + case self::HIGH: |
| 174 | + return self::PRIORITY_HIGH; |
| 175 | + case self::MEDIUM: |
| 176 | + return self::PRIORITY_NORMAL; |
| 177 | + case self::LOW: |
| 178 | + return self::PRIORITY_LOW; |
| 179 | + default: |
| 180 | + return self::PRIORITY_LOWEST; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private function getExceptionAsString($exception): string |
| 185 | + { |
| 186 | + if (class_exists(FlattenException::class)) { |
| 187 | + $exception = $exception instanceof FlattenException ? $exception : FlattenException::createFromThrowable($exception); |
| 188 | + |
| 189 | + return $exception->getAsString(); |
| 190 | + } |
| 191 | + |
| 192 | + $message = \get_class($exception); |
| 193 | + if ('' != $exception->getMessage()) { |
| 194 | + $message .= ': '.$exception->getMessage(); |
| 195 | + } |
| 196 | + |
| 197 | + $message .= ' in '.$exception->getFile().':'.$exception->getLine()."\n"; |
| 198 | + $message .= "Stack trace:\n".$exception->getTraceAsString()."\n\n"; |
| 199 | + |
| 200 | + return rtrim($message); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @internal |
| 205 | + */ |
| 206 | + public function __serialize(): array |
| 207 | + { |
| 208 | + return [$this->context, parent::__serialize()]; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * @internal |
| 213 | + */ |
| 214 | + public function __unserialize(array $data): void |
| 215 | + { |
| 216 | + [$this->context, $parentData] = $data; |
| 217 | + |
| 218 | + parent::__unserialize($parentData); |
| 219 | + } |
| 220 | +} |
0 commit comments