| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\BehatExtension\Utility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Same principle as core TestMailer class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * but saves emails in {@link TestSessionEnvironment} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * to share the state between PHP calls (CLI vs. browser). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class TestMailer extends \Mailer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var TestSessionEnvironment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $testSessionEnvironment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->testSessionEnvironment = \Injector::inst()->get('TestSessionEnvironment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Send a plain-text email. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * TestMailer will merely record that the email was asked to be sent, without sending anything. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 27 |  | View Code Duplication |     public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->saveEmail(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'Type' => 'plain', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             'To' => $to, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             'From' => $from, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             'Subject' => $subject, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'Content' => $plainContent, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'PlainContent' => $plainContent, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'AttachedFiles' => $attachedFiles, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'CustomHeaders' => $customHeaders, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Send a multi-part HTML email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * TestMailer will merely record that the email was asked to be sent, without sending anything. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 47 |  | View Code Duplication |     public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customHeaders = false, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $plainContent = false, $inlineImages = false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $this->saveEmail(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             'Type' => 'html', | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             'To' => $to, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             'From' => $from, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             'Subject' => $subject, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             'Content' => $htmlContent, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             'PlainContent' => $plainContent, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             'AttachedFiles' => $attachedFiles, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             'CustomHeaders' => $customHeaders, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Clear the log of emails sent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function clearEmails() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $state = $this->testSessionEnvironment->getState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if (isset($state->emails)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             unset($state->emails); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->testSessionEnvironment->applyState($state); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Search for an email that was sent. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param $subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      *               'customHeaders', 'htmlContent', 'inlineImages' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function findEmail($to = null, $from = null, $subject = null, $content = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $matches = $this->findEmails($to, $from, $subject, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 //got the count of matches emails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $emailCount = count($matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 //get the last(latest) one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return $matches ? $matches[$emailCount-1] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Search for all emails. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param $subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param $content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      *               'customHeaders', 'htmlContent', 'inlineImages' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function findEmails($to = null, $from = null, $subject = null, $content = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $matches = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $state = $this->testSessionEnvironment->getState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $emails = isset($state->emails) ? $state->emails : array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         foreach ($emails as $email) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $matched = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             foreach (array('To', 'From', 'Subject', 'Content') as $i => $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 if (!isset($email->$field)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $value = (isset($args[$i])) ? $args[$i] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 if ($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     if ($value[0] == '/') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                         $matched = preg_match($value, $email->$field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                         $matched = ($value == $email->$field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     if (!$matched) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             if ($matched) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 $matches[] = $email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return $matches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     protected function saveEmail($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $state = $this->testSessionEnvironment->getState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         if (!isset($state->emails)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $state->emails = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $state->emails[] = array_filter($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->testSessionEnvironment->applyState($state); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.