From 4f6185e6c5d55e48dbf810d41ba525938384f54c Mon Sep 17 00:00:00 2001 From: Sander Hartman Date: Wed, 8 Jan 2020 08:20:31 +0100 Subject: [PATCH] Remove unneeded properties --- Mail/Template/TransportBuilder.php | 333 ++++------------------------- 1 file changed, 44 insertions(+), 289 deletions(-) diff --git a/Mail/Template/TransportBuilder.php b/Mail/Template/TransportBuilder.php index 8e298db..0c5e11c 100644 --- a/Mail/Template/TransportBuilder.php +++ b/Mail/Template/TransportBuilder.php @@ -1,31 +1,25 @@ templateFactory = $templateFactory; - $this->objectManager = $objectManager; - $this->_senderResolver = $senderResolver; - $this->mailTransportFactory = $mailTransportFactory; - $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager + parent::__construct($templateFactory, $message, $senderResolver, $objectManager, $mailTransportFactory, $messageFactory, $emailMessageInterfaceFactory, $mimeMessageInterfaceFactory, $mimePartInterfaceFactory, $addressConverter); + $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $objectManager ->get(EmailMessageInterfaceFactory::class); - $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager + $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $objectManager ->get(MimeMessageInterfaceFactory::class); - $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager + $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $objectManager ->get(MimePartInterfaceFactory::class); - $this->addressConverter = $addressConverter ?: $this->objectManager + $this->addressConverter = $addressConverter ?: $objectManager ->get(AddressConverter::class); - } - - /** - * Add cc address - * - * @param array|string $address - * @param string $name - * - * @return $this - */ - public function addCc($address, $name = '') - { - $this->addAddressByType('cc', $address, $name); - - return $this; - } - - /** - * Add to address - * - * @param array|string $address - * @param string $name - * - * @return $this - * @throws InvalidArgumentException - */ - public function addTo($address, $name = '') - { - $this->addAddressByType('to', $address, $name); - - return $this; - } - - /** - * Add bcc address - * - * @param array|string $address - * - * @return $this - * @throws InvalidArgumentException - */ - public function addBcc($address) - { - $this->addAddressByType('bcc', $address); - - return $this; - } - - /** - * Set Reply-To Header - * - * @param string $email - * @param string|null $name - * - * @return $this - * @throws InvalidArgumentException - */ - public function setReplyTo($email, $name = null) - { - $this->addAddressByType('replyTo', $email, $name); - - return $this; - } - - /** - * Set mail from address - * - * @param string|array $from - * - * @return $this - * @throws InvalidArgumentException - * @see setFromByScope() - * - * @deprecated 102.0.1 This function sets the from address but does not provide - * a way of setting the correct from addresses based on the scope. - */ - public function setFrom($from) - { - return $this->setFromByScope($from); - } - - /** - * Set mail from address by scopeId - * - * @param string|array $from - * @param string|int $scopeId - * - * @return $this - * @throws InvalidArgumentException - * @throws MailException - * @since 102.0.1 - */ - public function setFromByScope($from, $scopeId = null) - { - $result = $this->_senderResolver->resolve($from, $scopeId); - $this->addAddressByType('from', $result['email'], $result['name']); - - return $this; - } - - /** - * Set template identifier - * - * @param string $templateIdentifier - * - * @return $this - */ - public function setTemplateIdentifier($templateIdentifier) - { - $this->templateIdentifier = $templateIdentifier; - - return $this; - } - - /** - * Set template model - * - * @param string $templateModel - * - * @return $this - */ - public function setTemplateModel($templateModel) - { - $this->templateModel = $templateModel; - return $this; - } - - /** - * Set template vars - * - * @param array $templateVars - * - * @return $this - */ - public function setTemplateVars($templateVars) - { - $this->templateVars = $templateVars; - - return $this; - } - - /** - * Set template options - * - * @param array $templateOptions - * @return $this - */ - public function setTemplateOptions($templateOptions) - { - $this->templateOptions = $templateOptions; - - return $this; - } - - /** - * Get mail transport - * - * @return TransportInterface - * @throws LocalizedException - */ - public function getTransport() - { - try { - $this->prepareMessage(); - $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]); - } finally { - $this->reset(); - } - - return $mailTransport; + $this->partFactory = $objectManager->get(PartFactory::class); } /** @@ -354,21 +100,10 @@ protected function reset() $this->templateIdentifier = null; $this->templateVars = null; $this->templateOptions = null; + $this->attachments = []; return $this; } - /** - * Get template - * - * @return TemplateInterface - */ - protected function getTemplate() - { - return $this->templateFactory->get($this->templateIdentifier, $this->templateModel) - ->setVars($this->templateVars) - ->setOptions($this->templateOptions); - } - /** * Prepare message. * @@ -394,8 +129,9 @@ protected function prepareMessage() ); } $mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]); + $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart]; $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create( - ['parts' => [$mimePart]] + ['parts' => $parts] ); $this->messageData['subject'] = html_entity_decode( @@ -431,4 +167,23 @@ private function addAddressByType(string $addressType, $email, ?string $name = n ); } } + + /** + * @param string|null $content + * @param string|null $fileName + * @param string|null $fileType + * @return TransportBuilder + */ + public function addAttachment(?string $content, ?string $fileName, ?string $fileType) + { + $attachmentPart = $this->partFactory->create(); + $attachmentPart->setContent($content) + ->setType($fileType) + ->setFileName($fileName) + ->setDisposition(Mime::DISPOSITION_ATTACHMENT) + ->setEncoding(Mime::ENCODING_BASE64); + $this->attachments[] = $attachmentPart; + + return $this; + } }