Completed
Pull Request — master (#457)
by Anton
11:41
created

Mailer::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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