Completed
Pull Request — develop (#1)
by Daniel
01:11
created

Mailer::getDefaultMessage()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 13
cp 0
rs 8.8177
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;
14
use Zend\Mime\Mime;
15
use Zend\Mime\Part as MimePart;
16
use Zend\View\Renderer\RendererInterface;
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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()
0 ignored issues
show
Coding Style introduced by
function sendEmail() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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 class ZfMailer\Service\RuntimeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

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()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
164
  {
165
    return $this->errorMessage;
166
  }
167
168
}
169