| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Zortje\Emailer\Adapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Mailgun\Mailgun; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Zortje\Emailer\Email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class MailgunAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Zortje\Emailer\Adapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class MailgunAdapter implements ServiceAdapterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var Mailgun | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $mailgun; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $domain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * MailgunAdapter constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param Mailgun $mailgun | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string  $domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |     public function __construct(Mailgun $mailgun, string $domain) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |         $this->mailgun = $mailgun; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |         $this->domain  = $domain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function send(Email $email) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->mailgun->sendMessage($this->domain, $this->getMessage($email)); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Get message from email to be used by Mailgun API wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param Email $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected function getMessage(Email $email): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $from = $this->formatFrom($email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $to   = $this->formatToType('to', $email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (empty($to)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             throw new \InvalidArgumentException('An email must have at least a single recipient'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $message = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'from'    => $from, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'to'      => $to, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'subject' => $email->getSubject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |          * HTML and text content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $html = $email->getHtml(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $text = $email->getText(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (empty($html) && empty($text)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new \InvalidArgumentException('A message body in HTML or text is missing'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (!empty($html)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $message['html'] = $html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (!empty($text)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $message['text'] = $text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |          * CC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $cc = $this->formatToType('cc', $email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if (!empty($cc)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $message['cc'] = $cc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |          * BCC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $bcc = $this->formatToType('bcc', $email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (!empty($bcc)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $message['bcc'] = $bcc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |          * Headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         foreach ($email->getHeaders() as $header => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $message["h:$header"] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |          * Tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $tag = $email->getTag(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if (!empty($tag)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $message['o:tag'] = $tag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         // @todo Handle campaign ('o:campaign') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // @todo Handle attachment ('attachment') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         // @todo Handle inline ('o:inline') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Format 'from' to be used by the Mailgun API wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param Email $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @return string Formatted 'from' string, Example: 'Bob <[email protected]>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 2 |  |     protected function formatFrom(Email $email): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 2 |  |         $from = $email->getFrom(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 2 |  |         if (empty($from['name']) || empty($from['email'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |             throw new \InvalidArgumentException('From parameter is missing'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |         return "{$from['name']} <{$from['email']}>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Format 'to' types to be used by the Mailgun API wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param string $type To type: 'to', 'cc' or 'bcc' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param Email  $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @return string Formatted 'to' of type string, Example: 'Bob <[email protected]>' (separated by commas for multiple recipients) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 3 |  |     protected function formatToType(string $type, Email $email): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 3 |  |         $tos = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 3 |  |         foreach ($email->getTo() as $to) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 2 |  |             if ($to['type'] === $type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 2 |  |                 $tos[] = "{$to['name']} <{$to['email']}>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 3 |  |         return implode(', ', $tos); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |  |