Passed
Push — develop ( e979a2...c4b4be )
by Daniel
08:10
created

Mailer::createNewMail()   C

Complexity

Conditions 17
Paths 37

Size

Total Lines 57
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 17.289

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 57
ccs 27
cts 30
cp 0.9
rs 5.2166
c 0
b 0
f 0
cc 17
nc 37
nop 3
crap 17.289

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * ZfMailer
4
 *
5
 * @author     Daniel Wolkenhauer <[email protected]>
6
 * @copyright  Copyright (c) 1997-2019 Daniel Wolkenhauer
7
 * @link       http://dw-labs.de/zfmailer
8
 * @version    0.1.0
9
 */
10
11
namespace ZfMailer\Service;
12
13
use Zend\Mime\Message as MimeMessage;
14
use Zend\Mime\Mime;
15
use Zend\Mime\Part as MimePart;
16
17
18
/**
19
* Mailer
20
* Klasse für den Versand der E-Mails zuständig
21
* 
22
* @package ZfMailer
23
* @subpackage Service
24
*/
25
class Mailer extends AbstractMailer
26
{
27
28
  /**
29
   * @var string Template für Text E-Mails
30
   */
31
  private $textTemplate = 'mail/default-text';
32
33
  /**
34
   * @var string Template für HTML E-Mails
35
   */
36
  private $htmlTemplate = 'mail/default-html';
37
38
  /**
39
   * Neue Nachricht
40
   *
41
   * @todo #ZFMAIL-2 weiter Header einfügen: X-Mailer, Org, usw.
42
   * @param string $to Empfänger der Nachricht
43
   * @param string $subject Betreff der Nachricht
44
   * @param string|null $from Absender der Nachricht
45
   * @return \Zend\Mail\Message|boolean Mail
46
   */
47 1
  public function createNewMail($to, $subject, $from = null)
48
  {
49
50
    // prüfen, ob Absender vorhanden ist
51 1
    if (!isset($from) || empty($from)) {
52
53 1
      $from = $this->getOptions()->getDefaultFrom();
54
55 1
      if (!isset($from) || empty($from)) {
56 1
        $this->setErrorMessage('Es wurde kein Absender angegeben oder konfiguriert.');
57 1
        return false;
58
      }
59
60
      // ToDo: prüfen, ob das hier nötig ist!
61 1
      $from = $this->getOptions()->getDefaultFrom();
62
63
    }
64
65
    // Empfänger prüfen
66 1
    if (!isset($to) || empty($to)) {
67 1
      $this->setErrorMessage('Es wurde kein Empfänger angegeben.');
68 1
      return false;
69
    }
70
71
    // Betreff prüfen
72 1
    if (!isset($subject) || empty($subject)) {
73 1
      $this->setErrorMessage('Es wurde kein Betreff angegeben.');
74 1
      return false;
75
    }
76
77 1
    $message = $this->getMailMessage();
78 1
    $message->setFrom($from);
79 1
    $message->setTo($to);
80 1
    $message->setSubject($subject);
81
82 1
    $xMailer = $this->getOptions()->getXMailer();
83 1
    $organization = $this->getOptions()->getOrganization();
84 1
    $returnPath = $this->getOptions()->getReturnPath();
85 1
    $replyTo = $this->getOptions()->getReplyTo();
86
87 1
    if (isset($xMailer) && !empty($xMailer)) {
88 1
      $message->getHeaders()->addHeaders(array('X-Mailer' => $xMailer));
89
    }
90
91 1
    if (isset($organization) && !empty($organization)) {
92
      $message->getHeaders()->addHeaders(array('Organization' => $organization));
93
    }
94
95 1
    if (isset($returnPath) && !empty($returnPath)) {
96
      $message->getHeaders()->addHeaders(array('Return-Path' => $returnPath));
97
    }
98
99 1
    if (isset($replyTo) && !empty($replyTo)) {
100
      $message->getHeaders()->addHeaders(array('Reply-To' => $replyTo));
101
    }
102
103 1
    return $message;
104
105
  }
106
107
  /**
108
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert das Template
109
   * und gibt ein fertiges Zend\Mail\Message Objekt zurück
110
   * 
111
   * @todo #ZFMAIL-3 Datentyp von $contentValues prüfen, falls kein Array, Fehler
112
   * @param array $contentValues Array mit Werten, die in deie E-Mail eingefügt werden
113
   * @param string|null $textTemplate Template für eine E-Mail im Textformat
114
   * @return \Zend\Mail\Message|boolean fertiges Mail-Objekt
115
   */
116 1
  public function prepareAsText(array $contentValues, string $textTemplate = null)
117
  {
118
119 1
    $message = $this->getMailMessage();
120
121 1
    if (isset($textTemplate) || !empty($textTemplate)) {
122 1
      $this->textTemplate = $textTemplate;
123
    }
124
125 1
    $renderer = $this->getRenderer();
126 1
    $body = $renderer->render($this->textTemplate, $contentValues);
127 1
    $message->setBody($body);
128
    
129 1
  }
130
131
  /**
132
   * Bereitet die E-Mail vor, fügt die Content-Variablen ein, rendert die Templates
133
   * fügt die Teile für Text- und HTML-Inhalte zusammen und gibt ein fertiges
134
   * Zend\Mail\Message Objekt zurück
135
   * 
136
   * @param array $contentValues Array mit Werten, die in die E-Mail eingefügt werden
137
   * @param string|null $textTemplate Template für den Text-Teil der E-Mail
138
   * @param string|null $htmlTemplate Template für den HTML-Teil der E-Mail
139
   */
140 1
  public function prepareAsMultipart(array $contentValues, string $textTemplate = null, string $htmlTemplate = null)
141
  {
142 1
    $defaultEncoding = $this->getOptions()->getEncoding();
143
144 1
    $message = $this->getMailMessage();
145 1
    $renderer = $this->getRenderer();
146
147 1
    if (isset($textTemplate) || !empty($textTemplate)) {
148 1
      $this->textTemplate = $textTemplate;
149
    }
150
151 1
    if (isset($htmlTemplate) || !empty($htmlTemplate)) {
152 1
      $this->htmlTemplate = $htmlTemplate;
153
    }
154
155 1
    $textContent = $renderer->render($this->textTemplate, $contentValues);
156 1
    $htmlContent = $renderer->render($this->htmlTemplate, $contentValues);
157
158 1
    $textPart = new MimePart($textContent);
159 1
    $textPart->type = Mime::TYPE_TEXT;
160 1
    $textPart->charset = $defaultEncoding;
161 1
    $textPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
162
163 1
    $htmlPart = new MimePart($htmlContent);
164 1
    $htmlPart->type = Mime::TYPE_HTML;
165 1
    $htmlPart->charset = $defaultEncoding;
166 1
    $htmlPart->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
167
168 1
    $mailBody = new MimeMessage();
169 1
    $mailBody->setParts([$textPart, $htmlPart]);
170
171 1
    $message->setBody($mailBody);
172 1
    $message->getHeaders()->get('content-type')->setType('multipart/alternative');
0 ignored issues
show
Bug introduced by
The method setType() does not exist on ArrayIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
    $message->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setType() does not exist on Zend\Mail\Header\HeaderInterface. It seems like you code against a sub-type of Zend\Mail\Header\HeaderInterface such as Zend\Mail\Header\ContentType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
    $message->getHeaders()->get('content-type')->/** @scrutinizer ignore-call */ setType('multipart/alternative');
Loading history...
173
174 1
    if (isset($defaultEncoding) && !empty($defaultEncoding)) {
175 1
      $message->setEncoding($defaultEncoding);
176
    }
177
    
178 1
  }
179
180
  /**
181
   * Versendet die E-Mails
182
   * @return Boolean Gibt True zurück, wenn die E-Mail erfolgreich versendet wurde, sonst False
183
   */
184
  public function sendEmail()
185
  {
186
187
    try {
188
      $this->getTransport()->send($this->getMailMessage());
189
      return true;
190
    } catch (RuntimeException $e) {
0 ignored issues
show
Bug introduced by
The type ZfMailer\Service\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
191
      //echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
192
      $this->setErrorMessage($e->getMessage());
193
      return false;
194
    }
195
    
196
  }
197
198
}
199