Add MimePartFacade
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 30 May 2016 16:40:54 +0000 (18:40 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 30 May 2016 16:41:10 +0000 (18:41 +0200)
wcfsetup/install/files/lib/system/email/mime/MimePartFacade.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/email/mime/MimePartFacade.class.php b/wcfsetup/install/files/lib/system/email/mime/MimePartFacade.class.php
new file mode 100644 (file)
index 0000000..02356d0
--- /dev/null
@@ -0,0 +1,92 @@
+<?php
+namespace wcf\system\email\mime;
+use wcf\system\email\Mailbox;
+
+/**
+ * This facade eases creating a "standard" email
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2016 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.email.mime
+ * @category   Community Framework
+ * @since      2.2
+ */
+class MimePartFacade extends AbstractMimePart implements IRecipientAwareMimePart {
+       /**
+        * the mime part to provide in the email
+        * @var AbstractMimePart
+        */
+       protected $mimePart;
+       
+       /**
+        * Creates a new MimePartFacade.
+        * 
+        * @see         MultipartAlternativeMimePart
+        * @see         MultipartMixedMimePart
+        * @param       AbstractMimePart[]      $texts          Versions of the text part in descending priority (i.e. inside multipart/alternative)
+        * @param       AbstractMimePart[]      $attachments    Attachments (i.e. inside multipart/mixed)
+        */
+       public function __construct(array $texts, array $attachments = []) {
+               if (count($texts) > 1) {
+                       $this->mimePart = new MultipartAlternativeMimePart();
+                       $priority = PHP_INT_MAX;
+                       foreach ($texts as $text) {
+                               $this->mimePart->addMimePart($text, $priority);
+                               $priority -= 1000;
+                       }
+               }
+               else {
+                       $this->mimePart = $texts[0];
+               }
+               
+               if (!empty($attachments)) {
+                       $mixed = new MultipartMixedMimePart();
+                       $mixed->addMimePart($this->mimePart);
+                       foreach ($attachments as $attachment) {
+                               $mixed->addMimePart($attachment);
+                       }
+                       $this->mimePart = $mixed;
+               }
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function setRecipient(Mailbox $mailbox = null) {
+               if ($this->mimePart instanceof IRecipientAwareMimePart) {
+                       $this->mimePart->setRecipient($mailbox);
+               }
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getContentType() {
+               return $this->mimePart->getContentType();
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getContentTransferEncoding() {
+               return $this->mimePart->getContentTransferEncoding();
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getContent() {
+               return $this->mimePart->getContent();
+       }
+       
+       /**
+        * Returns the inner mime part.
+        * 
+        * @return      AbstractMimePart
+        */
+       public function getMimePart() {
+               return $this->mimePart;
+       }
+}