Passed
Push — develop ( 3345f1...54db29 )
by Daniel
02:19
created

Mailer::sendEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
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
   * Neue Nachricht
30
   *
31
   * @param string $from Absender der Nachricht
32
   * @param string $to Empfänger der Nachricht
33
   * @param string $subject Betreff der Nachricht
34
   * @return Zend\Mail\Message Mail
0 ignored issues
show
Bug introduced by
The type ZfMailer\Service\Zend\Mail\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...
35
   */
36
  public function createNewMail($from = null, $to = null, $subject = null)
0 ignored issues
show
Unused Code introduced by
The parameter $to is not used and could be removed. ( Ignorable by Annotation )

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

36
  public function createNewMail($from = null, /** @scrutinizer ignore-unused */ $to = null, $subject = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $from is not used and could be removed. ( Ignorable by Annotation )

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

36
  public function createNewMail(/** @scrutinizer ignore-unused */ $from = null, $to = null, $subject = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $subject is not used and could be removed. ( Ignorable by Annotation )

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

36
  public function createNewMail($from = null, $to = null, /** @scrutinizer ignore-unused */ $subject = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

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