| Conditions | 15 | 
| Paths | 4109 | 
| Total Lines | 116 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php | ||
| 77 | public function sendMessage($message) | ||
| 78 |     { | ||
| 79 |         try { | ||
| 80 |             if ($this->apiKey === null) { | ||
| 81 |                 throw new InvalidConfigException('API Key is missing'); | ||
| 82 | } | ||
| 83 |             if ($this->apiSecret === null) { | ||
| 84 |                 throw new InvalidConfigException('API Secret is missing'); | ||
| 85 | } | ||
| 86 | $settings = [ | ||
| 87 | 'secured' => $this->secured, | ||
| 88 | 'version' => $this->apiVersion, | ||
| 89 | ]; | ||
| 90 | |||
| 91 |             if ($this->apiUrl !== null) { | ||
| 92 | $settings['url'] = $this->apiUrl; | ||
| 93 | } | ||
| 94 | |||
| 95 | $client = new Client($this->apiKey, $this->apiSecret, $this->enable, $settings); | ||
| 96 | |||
| 97 | $fromEmails = Message::convertEmails($message->getFrom()); | ||
| 98 | $toEmails = Message::convertEmails($message->getTo()); | ||
| 99 | |||
| 100 | $mailJetMessage = [ | ||
| 101 | // 'FromEmail' => $fromEmails[0]['Email'], | ||
| 102 | 'From' => $fromEmails[0], | ||
| 103 | 'To' => $toEmails, | ||
| 104 | ]; | ||
| 105 | /* | ||
| 106 |             if (isset($fromEmails[0]['Name']) === true) { | ||
| 107 | $mailJetMessage['FromName'] = $fromEmails[0]['Name']; | ||
| 108 | } | ||
| 109 | */ | ||
| 110 | |||
| 111 | /* | ||
| 112 | $sender = $message->getSender(); | ||
| 113 |             if (empty($sender) === false) { | ||
| 114 | $sender = Message::convertEmails($sender); | ||
| 115 | $mailJetMessage['Sender'] = $sender[0]; | ||
| 116 | } | ||
| 117 | */ | ||
| 118 | |||
| 119 | |||
| 120 | $cc = $message->getCc(); | ||
| 121 |             if (empty($cc) === false) { | ||
| 122 | $cc = Message::convertEmails($cc); | ||
| 123 | $mailJetMessage['Cc'] = $cc; | ||
| 124 | } | ||
| 125 | |||
| 126 | $bcc = $message->getBcc(); | ||
| 127 |             if (empty($cc) === false) { | ||
| 128 | $bcc = Message::convertEmails($bcc); | ||
| 129 | $mailJetMessage['Bcc'] = $bcc; | ||
| 130 | } | ||
| 131 | |||
| 132 | $attachments = $message->getAttachments(); | ||
| 133 |             if ($attachments !== null) { | ||
| 134 | $mailJetMessage['Attachments'] = $attachments; | ||
| 135 | } | ||
| 136 | |||
| 137 | //Get inilined attachments | ||
| 138 | $inlinedAttachments = $message->getInlinedAttachments(); | ||
| 139 |             if ($inlinedAttachments !== null) { | ||
| 140 | $mailJetMessage['InlinedAttachments'] = $inlinedAttachments; | ||
| 141 | } | ||
| 142 | |||
| 143 | $headers = $message->getHeaders(); | ||
| 144 |             if (empty($headers) === false) { | ||
| 145 | $mailJetMessage['Headers'] = $headers; | ||
| 146 | } | ||
| 147 | $mailJetMessage['TrackOpens'] = $message->getTrackOpens(); | ||
| 148 | $mailJetMessage['TrackClicks'] = $message->getTrackClicks(); | ||
| 149 | |||
| 150 | $templateModel = $message->getTemplateModel(); | ||
| 151 |             if (empty($templateModel) === false) { | ||
| 152 | $mailJetMessage['Variables'] = $templateModel; | ||
| 153 | } | ||
| 154 | |||
| 155 | $templateId = $message->getTemplateId(); | ||
| 156 |             if ($templateId === null) { | ||
| 157 | $mailJetMessage['Subject'] = $message->getSubject(); | ||
| 158 | $textBody = $message->getTextBody(); | ||
| 159 |                 if (empty($textBody) === false) { | ||
| 160 | $mailJetMessage['TextPart'] = $textBody; | ||
| 161 | } | ||
| 162 | $htmlBody = $message->getHtmlBody(); | ||
| 163 |                 if (empty($htmlBody) === false) { | ||
| 164 | $mailJetMessage['HTMLPart'] = $htmlBody; | ||
| 165 | } | ||
| 166 | $sendResult = $client->post(Resources::$Email, [ | ||
| 167 | 'body' => [ | ||
| 168 | 'Messages' => [ | ||
| 169 | $mailJetMessage, | ||
| 170 | ] | ||
| 171 | ] | ||
| 172 | ]); | ||
| 173 |             } else { | ||
| 174 | $mailJetMessage['TemplateID'] = $templateId; | ||
| 175 | $processLanguage = $message->getTemplateLanguage(); | ||
| 176 |                 if ($processLanguage === true) { | ||
| 177 | $mailJetMessage['TemplateLanguage'] = $processLanguage; | ||
| 178 | } | ||
| 179 | $sendResult = $client->post(Resources::$Email, [ | ||
| 180 | 'body' => [ | ||
| 181 | 'Messages' => [ | ||
| 182 | $mailJetMessage, | ||
| 183 | ] | ||
| 184 | ] | ||
| 185 | ]); | ||
| 186 | } | ||
| 187 | //TODO: handle error codes and log stuff | ||
| 188 | return $sendResult->success(); | ||
| 189 |         } catch (Exception $e) { | ||
| 190 | throw $e; | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 196 | } |