Passed
Push — develop ( 27f190...bef79d )
by Daniel
02:27
created

Mailer::getDefaultMessage()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 28
ccs 0
cts 13
cp 0
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 4
crap 42
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;
0 ignored issues
show
Bug introduced by
The type Zend\Mime\Message was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Zend\Mime\Mime;
0 ignored issues
show
Bug introduced by
The type Zend\Mime\Mime was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Zend\Mime\Part as MimePart;
0 ignored issues
show
Bug introduced by
The type Zend\Mime\Part was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Zend\View\Renderer\RendererInterface;
0 ignored issues
show
Bug introduced by
The type Zend\View\Renderer\RendererInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
26
{
27
28
  protected $errorMessage = null;
29
  protected $renderer = null;
30
  protected $mailMessage = null;
31
  protected $transport = null;
32
  
33
  private function getDefaultMessage($to, $subject, $body, $from = null)
34
  {
35
36
    if (!isset($body) || empty($body)) {
37
      return false;
38
    }
39
40
    if (!isset($to) || empty($to)) {
41
      return false;
42
    }
43
44
    $message = $this->getMailMessage();
45
    $message->setSubject($subject)
46
            ->setBody($body)
47
            ->setTo($to);
48
49
    if (isset($from)) {
50
      $message->setFrom($from);
51
    }
52
53
    // if (isset($encoding)) {
54
    //   $message->setEncoding($encoding);
55
    // }
56
57
    // Todo: in Factory verschieben und aus config auslesen
58
    $message->getHeaders()->addHeaders(array('X-Mailer' => 'ZfMailer 1.0.1'));
59
60
    return $message;
61
62
  }
63
64
  public function createTextMail($to, $subject, $templateOrModel, $values = array(), $from, $encoding = 'UTF-8')
65
  {
66
    $renderer = $this->getRenderer();
67
    $body = $renderer->render($templateOrModel, $values);
68
69
    $mail = $this->getDefaultMessage($to, $subject, $body, $from);
70
    $mail->setEncoding($encoding);
71
    
72
    return $mail;
73
  }
74
75
  public function createMultiPartMail($to, $subject, $txtTemplate, $htmlTemplate, $contentValues = array(), $from = null, $encoding = 'UTF-8')
76
  {
77
78
    // Todo: siehe https://github.com/zendframework/zf2/issues/4917
79
    // Todo: Variablen prüfen
80
81
    $renderer = $this->getRenderer();
82
83
    $txtContent = $renderer->render($txtTemplate, $contentValues);
84
    $htmlContent = $renderer->render($htmlTemplate, $contentValues);
85
86
    $text = new MimePart($txtContent);
87
    $text->type = "text/plain";
88
    $text->charset = 'utf-8';
89
90
    $html = new MimePart($htmlContent);
91
    $html->type = "text/html";
92
    $html->charset = 'utf-8';
93
94
    $body = new MimeMessage();
95
    $body->setParts(array($text, $html));
96
97
    $mail = $this->getDefaultMessage($to, $subject, $body, $from);
98
    $mail->getHeaders()->get('content-type')->setType('multipart/alternative');
99
    $mail->setEncoding($encoding);
100
101
    return $mail;
102
103
  }
104
105
  public function createMailWithAttachment()
106
  {
107
    return 'Das ist eine Mail mit Anhang';
108
  }
109
110
  public function sendEmail()
111
  {
112
113
    try {
114
      $this->getTransport()->send($this->getMailMessage());
115
      return true;
116
    } 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...
117
      //echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
118
      $this->setErrorMessage($e->getMessage());
119
      return false;
120
    }
121
    
122
  }
123
124
  public function setRenderer(RendererInterface $renderer)
125
  {
126
    $this->renderer = $renderer;
127
    return $this;
128
  }
129
130
  public function getRenderer()
131
  {
132
    return $this->renderer;
133
  }
134
135
  public function setMailMessage($mailMessage)
136
  {
137
    $this->mailMessage = $mailMessage;
138
    return $this;
139
  }
140
141
  public function getMailMessage()
142
  {
143
    return $this->mailMessage;
144
  }
145
146
  public function setTransport($transport)
147
  {
148
    $this->transport = $transport;
149
    return $this;
150
  }
151
152
  public function getTransport()
153
  {
154
    return $this->transport;
155
  }
156
157
  public function setErrorMessage($errorMessage)
158
  {
159
    $this->errorMessage = $errorMessage;
160
    return $this;
161
  }
162
163
  public function getErrorMessage()
164
  {
165
    return $this->errorMessage;
166
  }
167
168
}
169