Completed
Push — master ( 83cea8...e47c19 )
by Anton
17s queued 12s
created

Mailer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 35.7%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 90
ccs 10
cts 28
cp 0.357
rs 10
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 14 3
B create() 0 35 7
A init() 0 7 2
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Mailer;
13
14
use Bluz\Common\Exception\ComponentException;
15
use Bluz\Common\Exception\ConfigurationException;
16
use Bluz\Common\Options;
17
use Bluz\Proxy\Translator;
18
use PHPMailer\PHPMailer\Exception;
0 ignored issues
show
Bug introduced by
The type PHPMailer\PHPMailer\Exception 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...
19
use PHPMailer\PHPMailer\PHPMailer;
0 ignored issues
show
Bug introduced by
The type PHPMailer\PHPMailer\PHPMailer 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...
20
21
/**
22
 * Wrapper over PHPMailer
23
 *
24
 * @package  Bluz\Mailer
25
 * @author   Pavel Machekhin
26
 * @link     https://github.com/bluzphp/framework/wiki/Mailer
27
 */
28
class Mailer
29
{
30 1
    use Options;
31
32
    /**
33
     * Check Mailer configuration
34
     *
35
     * @throws ConfigurationException
36
     * @return void
37
     */
38 2
    public function init(): void
39
    {
40 2
        if (!$this->getOption('from', 'email')) {
41 1
            throw new ConfigurationException(
42
                "Missed `from.email` option in `mailer` configuration. <br/>\n" .
43
                "Read more: <a href='https://github.com/bluzphp/framework/wiki/Mailer'>" .
44 1
                'https://github.com/bluzphp/framework/wiki/Mailer</a>'
45
            );
46
        }
47 1
    }
48
49
    /**
50
     * Creates new instance of PHPMailer and set default options from config
51
     *
52
     * @return PHPMailer
53
     * @throws ComponentException
54
     * @throws Exception
55
     */
56 1
    public function create(): PHPMailer
57
    {
58
        // can initial, can't use
59 1
        if (!class_exists(PHPMailer::class)) {
60 1
            throw new ComponentException(
61
                "PHPMailer library is required for `Bluz\\Mailer` package. <br/>\n" .
62
                "Read more: <a href='https://github.com/bluzphp/framework/wiki/Mailer'>" .
63 1
                "https://github.com/bluzphp/framework/wiki/Mailer</a>"
64
            );
65
        }
66
67
        $mail = new PHPMailer();
68
        $mail->WordWrap = 920; // RFC 2822 Compliant for Max 998 characters per line
69
70
        $fromEmail = $this->getOption('from', 'email');
71
        $fromName = $this->getOption('from', 'name') ?: '';
72
73
        // setup options from config
74
        $mail->setFrom($fromEmail, $fromName, false);
75
76
        // setup options
77
        if ($settings = $this->getOption('settings')) {
78
            foreach ($settings as $name => $value) {
79
                $mail->set($name, $value);
80
            }
81
        }
82
83
        // setup custom headers
84
        if ($headers = $this->getOption('headers')) {
85
            foreach ($headers as $header => $value) {
86
                $mail->addCustomHeader($header, $value);
87
            }
88
        }
89
90
        return $mail;
91
    }
92
93
    /**
94
     * Send email
95
     *
96
     * @todo Add mail to queue
97
     *
98
     * @param  PHPMailer $mail
99
     *
100
     * @return bool
101
     * @throws MailerException
102
     * @throws Exception
103
     */
104
    public function send(PHPMailer $mail)
105
    {
106
        if ($template = $this->getOption('subjectTemplate')) {
107
            /** @var string $template */
108
            $mail->Subject = Translator::translate($template, $mail->Subject);
109
        }
110
111
        if (!$mail->send()) {
112
            // Why you don't use "Exception mode" of PHPMailer
113
            // Because we need our Exception in any case
114
            throw new MailerException('Error mail send: ' . $mail->ErrorInfo);
115
        }
116
117
        return true;
118
    }
119
}
120