@@ -25,47 +25,47 @@ |
||
| 25 | 25 | interface Mail extends Renderable |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Set the template that the email will use. |
|
| 30 | - * |
|
| 31 | - * @since 1.0.0 |
|
| 32 | - * |
|
| 33 | - * @param string|Template $template Template to use. |
|
| 34 | - * |
|
| 35 | - * @return Mail |
|
| 36 | - */ |
|
| 37 | - public function setTemplate($template); |
|
| 28 | + /** |
|
| 29 | + * Set the template that the email will use. |
|
| 30 | + * |
|
| 31 | + * @since 1.0.0 |
|
| 32 | + * |
|
| 33 | + * @param string|Template $template Template to use. |
|
| 34 | + * |
|
| 35 | + * @return Mail |
|
| 36 | + */ |
|
| 37 | + public function setTemplate($template); |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Get the instance of the template. |
|
| 41 | - * |
|
| 42 | - * @since 1.0.0 |
|
| 43 | - * |
|
| 44 | - * @return Template |
|
| 45 | - */ |
|
| 46 | - public function getTemplate(); |
|
| 39 | + /** |
|
| 40 | + * Get the instance of the template. |
|
| 41 | + * |
|
| 42 | + * @since 1.0.0 |
|
| 43 | + * |
|
| 44 | + * @return Template |
|
| 45 | + */ |
|
| 46 | + public function getTemplate(); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Add a section to the Mail. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.0 |
|
| 52 | - * |
|
| 53 | - * @param string $type Type of section to add. |
|
| 54 | - * @param string $content Content of the section. |
|
| 55 | - * |
|
| 56 | - * @return Mail |
|
| 57 | - * @throws RuntimeException |
|
| 58 | - */ |
|
| 59 | - public function addSection($type, $content); |
|
| 48 | + /** |
|
| 49 | + * Add a section to the Mail. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.0 |
|
| 52 | + * |
|
| 53 | + * @param string $type Type of section to add. |
|
| 54 | + * @param string $content Content of the section. |
|
| 55 | + * |
|
| 56 | + * @return Mail |
|
| 57 | + * @throws RuntimeException |
|
| 58 | + */ |
|
| 59 | + public function addSection($type, $content); |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Send the email to one or more recipients. |
|
| 63 | - * |
|
| 64 | - * @since 1.0.0 |
|
| 65 | - * |
|
| 66 | - * @param Recipients|EmailAddress|array|string $recipients |
|
| 67 | - * |
|
| 68 | - * @return Mail |
|
| 69 | - */ |
|
| 70 | - public function sendTo($recipients); |
|
| 61 | + /** |
|
| 62 | + * Send the email to one or more recipients. |
|
| 63 | + * |
|
| 64 | + * @since 1.0.0 |
|
| 65 | + * |
|
| 66 | + * @param Recipients|EmailAddress|array|string $recipients |
|
| 67 | + * |
|
| 68 | + * @return Mail |
|
| 69 | + */ |
|
| 70 | + public function sendTo($recipients); |
|
| 71 | 71 | } |
@@ -24,57 +24,57 @@ |
||
| 24 | 24 | class Domain |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Domain name. |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - protected $domain; |
|
| 27 | + /** |
|
| 28 | + * Domain name. |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + protected $domain; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Instantiate a Domain object. |
|
| 36 | - * |
|
| 37 | - * @param Domain|string $domain |
|
| 38 | - * |
|
| 39 | - * @throws InvalidDomain If the domain is not valid. |
|
| 40 | - */ |
|
| 41 | - public function __construct($domain) |
|
| 42 | - { |
|
| 43 | - if ( ! $this->isValid($domain)) { |
|
| 44 | - throw InvalidDomain::from($domain); |
|
| 45 | - } |
|
| 34 | + /** |
|
| 35 | + * Instantiate a Domain object. |
|
| 36 | + * |
|
| 37 | + * @param Domain|string $domain |
|
| 38 | + * |
|
| 39 | + * @throws InvalidDomain If the domain is not valid. |
|
| 40 | + */ |
|
| 41 | + public function __construct($domain) |
|
| 42 | + { |
|
| 43 | + if ( ! $this->isValid($domain)) { |
|
| 44 | + throw InvalidDomain::from($domain); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - $this->domain = (string)$domain; |
|
| 48 | - } |
|
| 47 | + $this->domain = (string)$domain; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Return the domain name. |
|
| 52 | - * |
|
| 53 | - * @return string |
|
| 54 | - */ |
|
| 55 | - public function getDomain() |
|
| 56 | - { |
|
| 57 | - return $this->domain; |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * Return the domain name. |
|
| 52 | + * |
|
| 53 | + * @return string |
|
| 54 | + */ |
|
| 55 | + public function getDomain() |
|
| 56 | + { |
|
| 57 | + return $this->domain; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Reutrn a string representation of the Domain object. |
|
| 62 | - * |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public function __toString() |
|
| 66 | - { |
|
| 67 | - return $this->getDomain(); |
|
| 68 | - } |
|
| 60 | + /** |
|
| 61 | + * Reutrn a string representation of the Domain object. |
|
| 62 | + * |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public function __toString() |
|
| 66 | + { |
|
| 67 | + return $this->getDomain(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * @param $domain |
|
| 72 | - * |
|
| 73 | - * @return bool |
|
| 74 | - */ |
|
| 75 | - protected function isValid($domain) |
|
| 76 | - { |
|
| 77 | - // TODO: Implement real validation. |
|
| 78 | - return $domain === trim($domain); |
|
| 79 | - } |
|
| 70 | + /** |
|
| 71 | + * @param $domain |
|
| 72 | + * |
|
| 73 | + * @return bool |
|
| 74 | + */ |
|
| 75 | + protected function isValid($domain) |
|
| 76 | + { |
|
| 77 | + // TODO: Implement real validation. |
|
| 78 | + return $domain === trim($domain); |
|
| 79 | + } |
|
| 80 | 80 | } |
| 81 | 81 | \ No newline at end of file |
@@ -36,304 +36,304 @@ |
||
| 36 | 36 | abstract class AbstractMail implements Mail |
| 37 | 37 | { |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Configuration Settings. |
|
| 41 | - * |
|
| 42 | - * @since 1.0.0 |
|
| 43 | - * |
|
| 44 | - * @var ConfigInterface |
|
| 45 | - */ |
|
| 46 | - protected $config; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Template that is used for the email. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.0 |
|
| 52 | - * |
|
| 53 | - * @var Template |
|
| 54 | - */ |
|
| 55 | - protected $template; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Content for the different sections. |
|
| 59 | - * |
|
| 60 | - * @since 1.0.0 |
|
| 61 | - * |
|
| 62 | - * @var array |
|
| 63 | - */ |
|
| 64 | - protected $sectionContent = []; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Format of the mail. |
|
| 68 | - * |
|
| 69 | - * @since 1.0.0 |
|
| 70 | - * |
|
| 71 | - * @var string |
|
| 72 | - */ |
|
| 73 | - protected $format; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * ViewBuilder to create template and section views. |
|
| 77 | - * |
|
| 78 | - * @since 1.0.0 |
|
| 79 | - * |
|
| 80 | - * @var ViewBuilder |
|
| 81 | - */ |
|
| 82 | - protected $viewBuilder; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Instantiate an AbstractMail object. |
|
| 86 | - * |
|
| 87 | - * @since 1.0.0 |
|
| 88 | - * |
|
| 89 | - * @param ConfigInterface $config The Config to use. |
|
| 90 | - * |
|
| 91 | - * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 92 | - */ |
|
| 93 | - public function __construct(ConfigInterface $config) |
|
| 94 | - { |
|
| 95 | - $this->config = $config; |
|
| 96 | - $this->setFormat(); |
|
| 97 | - |
|
| 98 | - $this->viewBuilder = new ViewBuilder($config |
|
| 99 | - ? $config->getSubConfig('ViewBuilder') |
|
| 100 | - : null |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - foreach ($this->config->getKey('view_root_locations') as $folder) { |
|
| 104 | - $this->viewBuilder->addLocation( |
|
| 105 | - new FilesystemLocation($folder) |
|
| 106 | - ); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Get the template to use for the renderer. |
|
| 112 | - * |
|
| 113 | - * @since 1.0.0 |
|
| 114 | - * |
|
| 115 | - * @return Template Reference to the template that is used. |
|
| 116 | - * @throws RuntimeException |
|
| 117 | - */ |
|
| 118 | - public function getTemplate() |
|
| 119 | - { |
|
| 120 | - |
|
| 121 | - if ( ! $this->template) { |
|
| 122 | - $this->setDefaultTemplate(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if (is_string($this->template)) { |
|
| 126 | - $this->template = $this->createTemplate($this->template); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - return $this->template; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Set the template to use for the renderer. |
|
| 134 | - * |
|
| 135 | - * @since 1.0.0 |
|
| 136 | - * |
|
| 137 | - * @param string|Template $template Template to use for the |
|
| 138 | - * renderer. |
|
| 139 | - * |
|
| 140 | - * @return Mail |
|
| 141 | - * @throws InvalidTemplate If the template class could not be instantiated. |
|
| 142 | - * @throws InvalidTemplate If the template type is not recognized. |
|
| 143 | - */ |
|
| 144 | - public function setTemplate($template) |
|
| 145 | - { |
|
| 146 | - try { |
|
| 147 | - if (is_string($template)) { |
|
| 148 | - $template = $this->createTemplate($template); |
|
| 149 | - } |
|
| 150 | - } catch (Exception $exception) { |
|
| 151 | - throw new InvalidTemplate( |
|
| 152 | - 'Could not instantiate the template class "%1$s". Reason: "%2$s".', |
|
| 153 | - $template, |
|
| 154 | - $exception->getMessage() |
|
| 155 | - ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - if ( ! $template instanceof Template) { |
|
| 159 | - throw new InvalidTemplate( |
|
| 160 | - 'Could not set the template, invalid type.', |
|
| 161 | - (array)$template |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - $this->template = $template; |
|
| 165 | - |
|
| 166 | - return $this; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Add a section to the Mail. |
|
| 171 | - * |
|
| 172 | - * @since 1.0.0 |
|
| 173 | - * |
|
| 174 | - * @param string $type Type of section to add. |
|
| 175 | - * @param string $content Content of the section. |
|
| 176 | - * |
|
| 177 | - * @throws RuntimeException |
|
| 178 | - */ |
|
| 179 | - public function addSection($type, $content) |
|
| 180 | - { |
|
| 181 | - $this->sectionContent[$type] = $content; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Render the Mail for a given context. |
|
| 186 | - * |
|
| 187 | - * @since 1.0.0 |
|
| 188 | - * |
|
| 189 | - * @param array $context The context in which to render the email. |
|
| 190 | - * |
|
| 191 | - * @return string Rendered output of the email |
|
| 192 | - */ |
|
| 193 | - public function render(array $context) |
|
| 194 | - { |
|
| 195 | - $template = $this->getTemplate(); |
|
| 196 | - |
|
| 197 | - $context['template'] = $template; |
|
| 198 | - |
|
| 199 | - $this->instantiateSections($template->getUsedSections(), $context); |
|
| 200 | - |
|
| 201 | - $context['format'] = $this->getFormat(); |
|
| 202 | - |
|
| 203 | - $context = $this->setContext($context); |
|
| 204 | - |
|
| 205 | - return $template->render($context); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Send the email to one or more recipients. |
|
| 210 | - * |
|
| 211 | - * @since 1.0.0 |
|
| 212 | - * |
|
| 213 | - * @param Recipients|EmailAddress|array|string $recipients |
|
| 214 | - * |
|
| 215 | - * @return Mail |
|
| 216 | - */ |
|
| 217 | - public function sendTo($recipients) |
|
| 218 | - { |
|
| 219 | - if ($recipients instanceof EmailAddress) { |
|
| 220 | - return $this->executeSend($recipients); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - if (is_string($recipients)) { |
|
| 224 | - return $this->sendTo(new EmailAddress($recipients)); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - if ($recipients instanceof Recipients) { |
|
| 228 | - array_walk($recipients, [$this, 'sendTo']); |
|
| 229 | - |
|
| 230 | - return $this; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Execute the sending of one individual email. |
|
| 236 | - * |
|
| 237 | - * @since 1.0.0 |
|
| 238 | - * |
|
| 239 | - * @param EmailAddress $recipient Recipient to send the email to. |
|
| 240 | - * |
|
| 241 | - * @return Mail |
|
| 242 | - */ |
|
| 243 | - protected function executeSend(EmailAddress $recipient) |
|
| 244 | - { |
|
| 245 | - // Do the actual sending. |
|
| 246 | - echo "Sending email to ${recipient}..."; |
|
| 247 | - |
|
| 248 | - return $this; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Instantiate the requested sections for a template. |
|
| 253 | - * |
|
| 254 | - * @since 1.0.0 |
|
| 255 | - * |
|
| 256 | - * @param array $sections Sections to instantiate. |
|
| 257 | - * @param array $context The context in which to instantiate the sections. |
|
| 258 | - */ |
|
| 259 | - protected function instantiateSections(array $sections, array &$context) |
|
| 260 | - { |
|
| 261 | - $sectionFactory = new Factory($this->config, 'sections'); |
|
| 262 | - |
|
| 263 | - foreach ($sections as $section) { |
|
| 264 | - |
|
| 265 | - $content = null; |
|
| 266 | - |
|
| 267 | - if (array_key_exists($section, $this->sectionContent)) { |
|
| 268 | - $content = $this->sectionContent[$section]; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - $context['sections'][$section] = $sectionFactory->create( |
|
| 272 | - $section, |
|
| 273 | - [$section, $content, $this->viewBuilder] |
|
| 274 | - ); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Set the format of the mail. |
|
| 280 | - * |
|
| 281 | - * @since 1.0.0 |
|
| 282 | - * |
|
| 283 | - * @return string Format of the Mail. |
|
| 284 | - */ |
|
| 285 | - protected function getFormat() |
|
| 286 | - { |
|
| 287 | - return $this->format; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Set the format of the mail. |
|
| 292 | - * |
|
| 293 | - * @since 1.0.0 |
|
| 294 | - * |
|
| 295 | - * @return void |
|
| 296 | - */ |
|
| 297 | - abstract protected function setFormat(); |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Set the template to the default template defined in the configuration. |
|
| 301 | - * |
|
| 302 | - * @since 1.0.0 |
|
| 303 | - * |
|
| 304 | - * @throws RuntimeException |
|
| 305 | - */ |
|
| 306 | - protected function setDefaultTemplate() |
|
| 307 | - { |
|
| 308 | - $defaultTemplate = $this->config->getKey('default_template'); |
|
| 309 | - $this->setTemplate($defaultTemplate); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Create an instance of a template. |
|
| 314 | - * |
|
| 315 | - * @since 1.0.0 |
|
| 316 | - * |
|
| 317 | - * @param string $template Template to instantiate. |
|
| 318 | - * |
|
| 319 | - * @return Template $template Newly created instance. |
|
| 320 | - * @throws RuntimeException |
|
| 321 | - */ |
|
| 322 | - protected function createTemplate($template) |
|
| 323 | - { |
|
| 324 | - $templateFactory = new Factory($this->config, 'templates'); |
|
| 325 | - |
|
| 326 | - return $templateFactory->create($template, [$template, $this->viewBuilder]); |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Set the context of the mail. |
|
| 331 | - * |
|
| 332 | - * @since 1.0.0 |
|
| 333 | - * |
|
| 334 | - * @param array $context Context to set/modify. |
|
| 335 | - * |
|
| 336 | - * @return array Updated context. |
|
| 337 | - */ |
|
| 338 | - abstract protected function setContext(array $context); |
|
| 39 | + /** |
|
| 40 | + * Configuration Settings. |
|
| 41 | + * |
|
| 42 | + * @since 1.0.0 |
|
| 43 | + * |
|
| 44 | + * @var ConfigInterface |
|
| 45 | + */ |
|
| 46 | + protected $config; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Template that is used for the email. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.0 |
|
| 52 | + * |
|
| 53 | + * @var Template |
|
| 54 | + */ |
|
| 55 | + protected $template; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Content for the different sections. |
|
| 59 | + * |
|
| 60 | + * @since 1.0.0 |
|
| 61 | + * |
|
| 62 | + * @var array |
|
| 63 | + */ |
|
| 64 | + protected $sectionContent = []; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Format of the mail. |
|
| 68 | + * |
|
| 69 | + * @since 1.0.0 |
|
| 70 | + * |
|
| 71 | + * @var string |
|
| 72 | + */ |
|
| 73 | + protected $format; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * ViewBuilder to create template and section views. |
|
| 77 | + * |
|
| 78 | + * @since 1.0.0 |
|
| 79 | + * |
|
| 80 | + * @var ViewBuilder |
|
| 81 | + */ |
|
| 82 | + protected $viewBuilder; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Instantiate an AbstractMail object. |
|
| 86 | + * |
|
| 87 | + * @since 1.0.0 |
|
| 88 | + * |
|
| 89 | + * @param ConfigInterface $config The Config to use. |
|
| 90 | + * |
|
| 91 | + * @throws FailedToProcessConfigException If the Config could not be processed. |
|
| 92 | + */ |
|
| 93 | + public function __construct(ConfigInterface $config) |
|
| 94 | + { |
|
| 95 | + $this->config = $config; |
|
| 96 | + $this->setFormat(); |
|
| 97 | + |
|
| 98 | + $this->viewBuilder = new ViewBuilder($config |
|
| 99 | + ? $config->getSubConfig('ViewBuilder') |
|
| 100 | + : null |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + foreach ($this->config->getKey('view_root_locations') as $folder) { |
|
| 104 | + $this->viewBuilder->addLocation( |
|
| 105 | + new FilesystemLocation($folder) |
|
| 106 | + ); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Get the template to use for the renderer. |
|
| 112 | + * |
|
| 113 | + * @since 1.0.0 |
|
| 114 | + * |
|
| 115 | + * @return Template Reference to the template that is used. |
|
| 116 | + * @throws RuntimeException |
|
| 117 | + */ |
|
| 118 | + public function getTemplate() |
|
| 119 | + { |
|
| 120 | + |
|
| 121 | + if ( ! $this->template) { |
|
| 122 | + $this->setDefaultTemplate(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if (is_string($this->template)) { |
|
| 126 | + $this->template = $this->createTemplate($this->template); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + return $this->template; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Set the template to use for the renderer. |
|
| 134 | + * |
|
| 135 | + * @since 1.0.0 |
|
| 136 | + * |
|
| 137 | + * @param string|Template $template Template to use for the |
|
| 138 | + * renderer. |
|
| 139 | + * |
|
| 140 | + * @return Mail |
|
| 141 | + * @throws InvalidTemplate If the template class could not be instantiated. |
|
| 142 | + * @throws InvalidTemplate If the template type is not recognized. |
|
| 143 | + */ |
|
| 144 | + public function setTemplate($template) |
|
| 145 | + { |
|
| 146 | + try { |
|
| 147 | + if (is_string($template)) { |
|
| 148 | + $template = $this->createTemplate($template); |
|
| 149 | + } |
|
| 150 | + } catch (Exception $exception) { |
|
| 151 | + throw new InvalidTemplate( |
|
| 152 | + 'Could not instantiate the template class "%1$s". Reason: "%2$s".', |
|
| 153 | + $template, |
|
| 154 | + $exception->getMessage() |
|
| 155 | + ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + if ( ! $template instanceof Template) { |
|
| 159 | + throw new InvalidTemplate( |
|
| 160 | + 'Could not set the template, invalid type.', |
|
| 161 | + (array)$template |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + $this->template = $template; |
|
| 165 | + |
|
| 166 | + return $this; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Add a section to the Mail. |
|
| 171 | + * |
|
| 172 | + * @since 1.0.0 |
|
| 173 | + * |
|
| 174 | + * @param string $type Type of section to add. |
|
| 175 | + * @param string $content Content of the section. |
|
| 176 | + * |
|
| 177 | + * @throws RuntimeException |
|
| 178 | + */ |
|
| 179 | + public function addSection($type, $content) |
|
| 180 | + { |
|
| 181 | + $this->sectionContent[$type] = $content; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Render the Mail for a given context. |
|
| 186 | + * |
|
| 187 | + * @since 1.0.0 |
|
| 188 | + * |
|
| 189 | + * @param array $context The context in which to render the email. |
|
| 190 | + * |
|
| 191 | + * @return string Rendered output of the email |
|
| 192 | + */ |
|
| 193 | + public function render(array $context) |
|
| 194 | + { |
|
| 195 | + $template = $this->getTemplate(); |
|
| 196 | + |
|
| 197 | + $context['template'] = $template; |
|
| 198 | + |
|
| 199 | + $this->instantiateSections($template->getUsedSections(), $context); |
|
| 200 | + |
|
| 201 | + $context['format'] = $this->getFormat(); |
|
| 202 | + |
|
| 203 | + $context = $this->setContext($context); |
|
| 204 | + |
|
| 205 | + return $template->render($context); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Send the email to one or more recipients. |
|
| 210 | + * |
|
| 211 | + * @since 1.0.0 |
|
| 212 | + * |
|
| 213 | + * @param Recipients|EmailAddress|array|string $recipients |
|
| 214 | + * |
|
| 215 | + * @return Mail |
|
| 216 | + */ |
|
| 217 | + public function sendTo($recipients) |
|
| 218 | + { |
|
| 219 | + if ($recipients instanceof EmailAddress) { |
|
| 220 | + return $this->executeSend($recipients); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + if (is_string($recipients)) { |
|
| 224 | + return $this->sendTo(new EmailAddress($recipients)); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + if ($recipients instanceof Recipients) { |
|
| 228 | + array_walk($recipients, [$this, 'sendTo']); |
|
| 229 | + |
|
| 230 | + return $this; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Execute the sending of one individual email. |
|
| 236 | + * |
|
| 237 | + * @since 1.0.0 |
|
| 238 | + * |
|
| 239 | + * @param EmailAddress $recipient Recipient to send the email to. |
|
| 240 | + * |
|
| 241 | + * @return Mail |
|
| 242 | + */ |
|
| 243 | + protected function executeSend(EmailAddress $recipient) |
|
| 244 | + { |
|
| 245 | + // Do the actual sending. |
|
| 246 | + echo "Sending email to ${recipient}..."; |
|
| 247 | + |
|
| 248 | + return $this; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Instantiate the requested sections for a template. |
|
| 253 | + * |
|
| 254 | + * @since 1.0.0 |
|
| 255 | + * |
|
| 256 | + * @param array $sections Sections to instantiate. |
|
| 257 | + * @param array $context The context in which to instantiate the sections. |
|
| 258 | + */ |
|
| 259 | + protected function instantiateSections(array $sections, array &$context) |
|
| 260 | + { |
|
| 261 | + $sectionFactory = new Factory($this->config, 'sections'); |
|
| 262 | + |
|
| 263 | + foreach ($sections as $section) { |
|
| 264 | + |
|
| 265 | + $content = null; |
|
| 266 | + |
|
| 267 | + if (array_key_exists($section, $this->sectionContent)) { |
|
| 268 | + $content = $this->sectionContent[$section]; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + $context['sections'][$section] = $sectionFactory->create( |
|
| 272 | + $section, |
|
| 273 | + [$section, $content, $this->viewBuilder] |
|
| 274 | + ); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Set the format of the mail. |
|
| 280 | + * |
|
| 281 | + * @since 1.0.0 |
|
| 282 | + * |
|
| 283 | + * @return string Format of the Mail. |
|
| 284 | + */ |
|
| 285 | + protected function getFormat() |
|
| 286 | + { |
|
| 287 | + return $this->format; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Set the format of the mail. |
|
| 292 | + * |
|
| 293 | + * @since 1.0.0 |
|
| 294 | + * |
|
| 295 | + * @return void |
|
| 296 | + */ |
|
| 297 | + abstract protected function setFormat(); |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Set the template to the default template defined in the configuration. |
|
| 301 | + * |
|
| 302 | + * @since 1.0.0 |
|
| 303 | + * |
|
| 304 | + * @throws RuntimeException |
|
| 305 | + */ |
|
| 306 | + protected function setDefaultTemplate() |
|
| 307 | + { |
|
| 308 | + $defaultTemplate = $this->config->getKey('default_template'); |
|
| 309 | + $this->setTemplate($defaultTemplate); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Create an instance of a template. |
|
| 314 | + * |
|
| 315 | + * @since 1.0.0 |
|
| 316 | + * |
|
| 317 | + * @param string $template Template to instantiate. |
|
| 318 | + * |
|
| 319 | + * @return Template $template Newly created instance. |
|
| 320 | + * @throws RuntimeException |
|
| 321 | + */ |
|
| 322 | + protected function createTemplate($template) |
|
| 323 | + { |
|
| 324 | + $templateFactory = new Factory($this->config, 'templates'); |
|
| 325 | + |
|
| 326 | + return $templateFactory->create($template, [$template, $this->viewBuilder]); |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Set the context of the mail. |
|
| 331 | + * |
|
| 332 | + * @since 1.0.0 |
|
| 333 | + * |
|
| 334 | + * @param array $context Context to set/modify. |
|
| 335 | + * |
|
| 336 | + * @return array Updated context. |
|
| 337 | + */ |
|
| 338 | + abstract protected function setContext(array $context); |
|
| 339 | 339 | } |
@@ -25,40 +25,40 @@ |
||
| 25 | 25 | class Recipients extends ArrayCollection |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Instantiate a Recipients object. |
|
| 30 | - * |
|
| 31 | - * @param array $emails Array of email addresses and/or recipients. |
|
| 32 | - */ |
|
| 33 | - public function __construct(array $emails) |
|
| 34 | - { |
|
| 35 | - parent::__construct([]); |
|
| 36 | - $this->add($emails); |
|
| 37 | - } |
|
| 28 | + /** |
|
| 29 | + * Instantiate a Recipients object. |
|
| 30 | + * |
|
| 31 | + * @param array $emails Array of email addresses and/or recipients. |
|
| 32 | + */ |
|
| 33 | + public function __construct(array $emails) |
|
| 34 | + { |
|
| 35 | + parent::__construct([]); |
|
| 36 | + $this->add($emails); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Add an email address to the Recipients collection. |
|
| 41 | - * |
|
| 42 | - * @param Recipients|EmailAddress|array|string $email Email address to add. |
|
| 43 | - * |
|
| 44 | - * @return static |
|
| 45 | - */ |
|
| 46 | - public function add($email) |
|
| 47 | - { |
|
| 48 | - if ($email instanceof EmailAddress) { |
|
| 49 | - parent::add($email); |
|
| 39 | + /** |
|
| 40 | + * Add an email address to the Recipients collection. |
|
| 41 | + * |
|
| 42 | + * @param Recipients|EmailAddress|array|string $email Email address to add. |
|
| 43 | + * |
|
| 44 | + * @return static |
|
| 45 | + */ |
|
| 46 | + public function add($email) |
|
| 47 | + { |
|
| 48 | + if ($email instanceof EmailAddress) { |
|
| 49 | + parent::add($email); |
|
| 50 | 50 | |
| 51 | - return $this; |
|
| 52 | - } |
|
| 51 | + return $this; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - if (is_array($email) || $email instanceof Recipients) { |
|
| 55 | - array_walk($email, [$this, 'add']); |
|
| 54 | + if (is_array($email) || $email instanceof Recipients) { |
|
| 55 | + array_walk($email, [$this, 'add']); |
|
| 56 | 56 | |
| 57 | - return $this; |
|
| 58 | - } |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - parent::add(new EmailAddress($email)); |
|
| 60 | + parent::add(new EmailAddress($email)); |
|
| 61 | 61 | |
| 62 | - return $this; |
|
| 63 | - } |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | } |
| 65 | 65 | \ No newline at end of file |
@@ -25,96 +25,96 @@ |
||
| 25 | 25 | class EmailAddress |
| 26 | 26 | { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The local part of the email address (before the '@' sign). |
|
| 30 | - * |
|
| 31 | - * @var string |
|
| 32 | - */ |
|
| 33 | - protected $localPart; |
|
| 28 | + /** |
|
| 29 | + * The local part of the email address (before the '@' sign). |
|
| 30 | + * |
|
| 31 | + * @var string |
|
| 32 | + */ |
|
| 33 | + protected $localPart; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * The domain part of the email address (after the '@' sign). |
|
| 37 | - * |
|
| 38 | - * @var Domain |
|
| 39 | - */ |
|
| 40 | - protected $domain; |
|
| 35 | + /** |
|
| 36 | + * The domain part of the email address (after the '@' sign). |
|
| 37 | + * |
|
| 38 | + * @var Domain |
|
| 39 | + */ |
|
| 40 | + protected $domain; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Instantiate an EmailAddress object. |
|
| 44 | - * |
|
| 45 | - * @param EmailAddress|string $email The email to parse. |
|
| 46 | - * |
|
| 47 | - * @throws InvalidEmailAddress If the email is not valid. |
|
| 48 | - */ |
|
| 49 | - public function __construct($email) |
|
| 50 | - { |
|
| 51 | - if ($email instanceof EmailAddress) { |
|
| 52 | - $this->localPart = $email->getLocalPart(); |
|
| 53 | - $this->domain = $email->getDomain(); |
|
| 54 | - } else { |
|
| 55 | - if ( ! $this->isValid($email)) { |
|
| 56 | - throw InvalidEmailAddress::from($email); |
|
| 57 | - } |
|
| 42 | + /** |
|
| 43 | + * Instantiate an EmailAddress object. |
|
| 44 | + * |
|
| 45 | + * @param EmailAddress|string $email The email to parse. |
|
| 46 | + * |
|
| 47 | + * @throws InvalidEmailAddress If the email is not valid. |
|
| 48 | + */ |
|
| 49 | + public function __construct($email) |
|
| 50 | + { |
|
| 51 | + if ($email instanceof EmailAddress) { |
|
| 52 | + $this->localPart = $email->getLocalPart(); |
|
| 53 | + $this->domain = $email->getDomain(); |
|
| 54 | + } else { |
|
| 55 | + if ( ! $this->isValid($email)) { |
|
| 56 | + throw InvalidEmailAddress::from($email); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - try { |
|
| 60 | - $parts = explode('@', $email); |
|
| 61 | - $this->localPart = trim($parts[0]); |
|
| 62 | - $this->domain = new Domain(trim($parts[1])); |
|
| 63 | - } catch (Exception $exception) { |
|
| 64 | - throw InvalidEmailAddress::from($email); |
|
| 65 | - } |
|
| 66 | - } |
|
| 67 | - } |
|
| 59 | + try { |
|
| 60 | + $parts = explode('@', $email); |
|
| 61 | + $this->localPart = trim($parts[0]); |
|
| 62 | + $this->domain = new Domain(trim($parts[1])); |
|
| 63 | + } catch (Exception $exception) { |
|
| 64 | + throw InvalidEmailAddress::from($email); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Returns the local part of the email address. |
|
| 71 | - * |
|
| 72 | - * @return string |
|
| 73 | - */ |
|
| 74 | - public function getLocalPart() |
|
| 75 | - { |
|
| 76 | - return $this->localPart; |
|
| 77 | - } |
|
| 69 | + /** |
|
| 70 | + * Returns the local part of the email address. |
|
| 71 | + * |
|
| 72 | + * @return string |
|
| 73 | + */ |
|
| 74 | + public function getLocalPart() |
|
| 75 | + { |
|
| 76 | + return $this->localPart; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Returns the domain part of the email address. |
|
| 81 | - * |
|
| 82 | - * @return Domain |
|
| 83 | - */ |
|
| 84 | - public function getDomain() |
|
| 85 | - { |
|
| 86 | - return $this->domain; |
|
| 87 | - } |
|
| 79 | + /** |
|
| 80 | + * Returns the domain part of the email address. |
|
| 81 | + * |
|
| 82 | + * @return Domain |
|
| 83 | + */ |
|
| 84 | + public function getDomain() |
|
| 85 | + { |
|
| 86 | + return $this->domain; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Returns the entire email address. |
|
| 91 | - * |
|
| 92 | - * @return string |
|
| 93 | - */ |
|
| 94 | - public function getAddress() |
|
| 95 | - { |
|
| 96 | - return $this->getLocalPart() . '@' . $this->getDomain(); |
|
| 97 | - } |
|
| 89 | + /** |
|
| 90 | + * Returns the entire email address. |
|
| 91 | + * |
|
| 92 | + * @return string |
|
| 93 | + */ |
|
| 94 | + public function getAddress() |
|
| 95 | + { |
|
| 96 | + return $this->getLocalPart() . '@' . $this->getDomain(); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Convert the EmailAddress object to a string. |
|
| 101 | - * |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - public function __toString() |
|
| 105 | - { |
|
| 106 | - return $this->getAddress(); |
|
| 107 | - } |
|
| 99 | + /** |
|
| 100 | + * Convert the EmailAddress object to a string. |
|
| 101 | + * |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + public function __toString() |
|
| 105 | + { |
|
| 106 | + return $this->getAddress(); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Check whether an email is valid. |
|
| 111 | - * |
|
| 112 | - * @param string $email Email to check. |
|
| 113 | - * |
|
| 114 | - * @return bool Whether the email is valid. |
|
| 115 | - */ |
|
| 116 | - protected function isValid($email) |
|
| 117 | - { |
|
| 118 | - return (bool)filter_var($email, FILTER_VALIDATE_EMAIL); |
|
| 119 | - } |
|
| 109 | + /** |
|
| 110 | + * Check whether an email is valid. |
|
| 111 | + * |
|
| 112 | + * @param string $email Email to check. |
|
| 113 | + * |
|
| 114 | + * @return bool Whether the email is valid. |
|
| 115 | + */ |
|
| 116 | + protected function isValid($email) |
|
| 117 | + { |
|
| 118 | + return (bool)filter_var($email, FILTER_VALIDATE_EMAIL); |
|
| 119 | + } |
|
| 120 | 120 | } |
| 121 | 121 | \ No newline at end of file |