11<?php
2+
23namespace Swisscom \CommunicationDispatcher \Channel ;
34
45/*
56 * This file is part of the Swisscom.CommunicationDispatcher package.
67 */
78
9+ use Neos \Flow \Annotations as Flow ;
810use Neos \Flow \ResourceManagement \PersistentResource ;
11+ use Neos \Flow \ResourceManagement \ResourceManager ;
912use Neos \SwiftMailer \Message ;
13+ use Swift_Attachment ;
14+ use Swift_Image ;
15+ use Swift_Message ;
1016use Swisscom \CommunicationDispatcher \Domain \Model \Dto \Recipient ;
11- use Swisscom \CommunicationDispatcher \Domain \Repository \AssetRepository ;
12- use Neos \Flow \Annotations as Flow ;
13- use Neos \Flow \ResourceManagement \ResourceManager ;
1417use Swisscom \CommunicationDispatcher \Exception ;
1518
1619/**
1720 * @Flow\Scope("prototype")
1821 */
1922class EmailChannel implements ChannelInterface
2023{
21- /**
22- * @Flow\Inject
23- * @var AssetRepository
24- */
25- protected $ assetRepository ;
2624
2725 /**
2826 * @Flow\Inject
@@ -63,7 +61,7 @@ function __construct(array $options = [])
6361 * @return void
6462 * @throws Exception
6563 */
66- public function send (Recipient $ recipient , $ subject , $ text , $ options = array () )
64+ public function send (Recipient $ recipient , string $ subject , string $ text , array $ options = [] )
6765 {
6866 $ toEmail = $ recipient ->getEmail ();
6967 $ toName = $ recipient ->getName ();
@@ -87,11 +85,11 @@ public function send(Recipient $recipient, $subject, $text, $options = array())
8785 $ mail ->setBody ($ text , 'text/html ' , 'utf-8 ' );
8886 $ mail ->addPart ($ plaintext , 'text/plain ' , 'utf-8 ' );
8987 foreach ($ attachedResources as $ resource ) {
90- if ($ resource instanceof \ Neos \ Flow \ ResourceManagement \ PersistentResource) {
88+ if ($ resource instanceof PersistentResource) {
9189 if ($ swiftAttachment = $ this ->createSwiftAttachmentFromPersistentResource ($ resource )) {
9290 $ mail ->attach ($ swiftAttachment );
9391 }
94- } elseif ($ resource instanceof \ Swift_Attachment) {
92+ } elseif ($ resource instanceof Swift_Attachment) {
9593 $ mail ->attach ($ resource );
9694 }
9795 }
@@ -102,30 +100,15 @@ public function send(Recipient $recipient, $subject, $text, $options = array())
102100 }
103101 }
104102
105- /**
106- * @param PersistentResource $resource
107- * @return \Swift_Attachment
108- */
109- public function createSwiftAttachmentFromPersistentResource (PersistentResource $ resource )
110- {
111- // No exception handling here. This provides flexibility to handle it outside or by aspects
112- $ path = $ resource ->createTemporaryLocalCopy ();
113- $ attachment = \Swift_Attachment::fromPath ($ path , $ resource ->getMediaType ());
114- $ attachment ->setFilename ($ resource ->getFilename ());
115-
116- return $ attachment ;
117- }
118-
119103 /**
120104 * Embed images. I.e:
121105 * <img height="40px" src="###IMAGE:'{template.logo}'###" alt="Logo"/>
122106 *
123- * @param $html
124- * @param \Swift_Message $mail
125- *
107+ * @param string $html
108+ * @param Swift_Message $mail
126109 * @return string $html
127110 */
128- private function embedResources ($ html , &$ mail )
111+ private function embedResources (string $ html , Swift_Message &$ mail ): string
129112 {
130113 $ html = preg_replace_callback ('/###IMAGE:(.+?)###/ ' , function ($ matches ) use ($ mail ) {
131114 return $ this ->embedImageResourceCallback ($ matches , $ mail );
@@ -139,17 +122,16 @@ private function embedResources($html, &$mail)
139122
140123 /**
141124 * @param array $matches
142- * @param \Swift_Message $mail $mail
143- *
125+ * @param \Swift_Message $mail
144126 * @return string
145127 */
146- private function embedImageResourceCallback ($ matches , &$ mail )
128+ private function embedImageResourceCallback (array $ matches , Swift_Message &$ mail ): string
147129 {
148130 $ cid = '' ;
149131 if (isset ($ matches [1 ])) {
150132 $ source = trim ($ matches [1 ], '\'' );
151133 try {
152- $ cid = $ mail ->embed (\ Swift_Image::fromPath ($ source ));
134+ $ cid = $ mail ->embed (Swift_Image::fromPath ($ source ));
153135 } catch (\Exception $ e ) {
154136 // Nothing to do here
155137 }
@@ -159,10 +141,9 @@ private function embedImageResourceCallback($matches, &$mail)
159141
160142 /**
161143 * @param array $matches
162- *
163144 * @return string
164145 */
165- private function embedPlainResourceCallback ($ matches )
146+ private function embedPlainResourceCallback (array $ matches ): string
166147 {
167148 $ plain = '' ;
168149 if (isset ($ matches [1 ])) {
@@ -175,4 +156,18 @@ private function embedPlainResourceCallback($matches)
175156 }
176157 return $ plain ;
177158 }
159+
160+ /**
161+ * @param PersistentResource $resource
162+ * @return Swift_Attachment
163+ */
164+ public function createSwiftAttachmentFromPersistentResource (PersistentResource $ resource ): Swift_Attachment
165+ {
166+ // No exception handling here. This provides flexibility to handle it outside or by aspects
167+ $ path = $ resource ->createTemporaryLocalCopy ();
168+ $ attachment = Swift_Attachment::fromPath ($ path , $ resource ->getMediaType ());
169+ $ attachment ->setFilename ($ resource ->getFilename ());
170+
171+ return $ attachment ;
172+ }
178173}
0 commit comments