Smtp::checkConfig()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 42
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Component\Mailer;
6
7
use Exception;
8
use Nymfonya\Component\Container;
9
use Nymfonya\Component\Config;
10
use Swift_SmtpTransport;
11
use Swift_Mailer;
12
use Swift_Message;
13
14
class Smtp
15
{
16
17
    const _MAILER = 'mailer';
18
    const _SENDER = 'sender';
19
    const _SMTP = 'smtp';
20
    const _HOST = 'host';
21
    const _PORT = 'port';
22
    const _USERNAME = 'username';
23
    const _PASSWORD = 'password';
24
    const _ENCRYPTION = 'encryption';
25
    const _ERROR_PREFIX = 'Mailer Smtp : ';
26
    const _ERROR_FROM = 'Missing from';
27
    const _ERROR_TO = 'Missing to';
28
    const _ERROR_BAD_CONF = 'Missing config mailer entry';
29
    const _ERROR_INVALID_CONF = 'Invalid config';
30
31
32
    /**
33
     * transport mailer
34
     *
35
     * @var Swift_SmtpTransport
36
     */
37
    private $transport;
38
39
    /**
40
     * mailer instance
41
     *
42
     * @var Swift_Mailer
43
     */
44
    private $mailer;
45
46
    /**
47
     * mail message
48
     *
49
     * @var Swift_Message
50
     */
51
    private $message;
52
53
    /**
54
     * mail message from
55
     *
56
     * @var array
57
     */
58
    private $from;
59
60
    /**
61
     * mail message to
62
     *
63
     * @var array
64
     */
65
    private $to;
66
67
    /**
68
     * mailer send error
69
     *
70
     * @var Boolean
71
     */
72
    private $error;
73
74
75
    /**
76
     * instanciate
77
     *
78
     * @param Container $container
79
     */
80
    public function __construct(Container $container)
81
    {
82
        $config = $container->getService(Config::class);
83
        if (false === $config->hasEntry(self::_MAILER)) {
84
            throw new Exception($this->exMsg(self::_ERROR_BAD_CONF));
85
        }
86
        $this->init($config->getSettings(self::_MAILER));
87
    }
88
89
    /**
90
     * set message initiators
91
     *
92
     * @param array $from
93
     * @return Smtp
94
     */
95
    public function setFrom(array $from): Smtp
96
    {
97
        $this->from = $from;
98
        return $this;
99
    }
100
101
    /**
102
     * set message recipients
103
     *
104
     * @param array $to
105
     * @return Smtp
106
     */
107
    public function setTo(array $to): Smtp
108
    {
109
        $this->to = $to;
110
        return $this;
111
    }
112
113
    /**
114
     * prepare message
115
     *
116
     * @param string $title
117
     * @param string $body
118
     * @throws Exception
119
     * @return Smtp
120
     */
121
    public function setMessage(string $title, string $body): Smtp
122
    {
123
        if (empty($this->from)) {
124
            throw new Exception($this->exMsg(self::_ERROR_FROM));
125
        }
126
        if (empty($this->to)) {
127
            throw new Exception($this->exMsg(self::_ERROR_TO));
128
        }
129
        $this->message = new Swift_Message($title);
130
        $this->message
131
            ->setFrom($this->from)
132
            ->setTo($this->to)
133
            ->setBody($body);
134
        return $this;
135
    }
136
137
    /**
138
     * true means message was not sent
139
     *
140
     * @return boolean
141
     */
142
    public function isError(): bool
143
    {
144
        return $this->error === true;
145
    }
146
147
    /**
148
     * send message
149
     *
150
     * @return Smtp
151
     */
152
    public function sendMessage(): Smtp
153
    {
154
        $this->mailer->send($this->message);
155
        return $this;
156
    }
157
158
    /**
159
     * init default params
160
     *
161
     * @param array $mailerConfig
162
     * @return Smtp
163
     */
164
    protected function init(array $mailerConfig): Smtp
165
    {
166
        $this->from = (isset($mailerConfig[self::_SENDER]))
167
            ? $mailerConfig[self::_SENDER]
168
            : [];
169
        $this->to = [];
170
        $transportConfig = $mailerConfig[self::_SMTP];
171
        $this->setTransport($transportConfig)->setMailer();
172
        return $this;
173
    }
174
175
    /**
176
     * prepare mailer transport
177
     *
178
     * @param array $transportConfig
179
     * @return Smtp
180
     */
181
    protected function setTransport(array $transportConfig): Smtp
182
    {
183
        $this->checkConfig($transportConfig);
184
        list($host, $port, $username, $password, $encryption) = [
185
            $transportConfig[self::_HOST],
186
            $transportConfig[self::_PORT],
187
            $transportConfig[self::_USERNAME],
188
            $transportConfig[self::_PASSWORD],
189
            $transportConfig[self::_ENCRYPTION]
190
        ];
191
        $this->transport = new Swift_SmtpTransport($host, $port, $encryption);
192
        $this->transport->setUsername($username)->setPassword($password);
193
        return $this;
194
    }
195
196
    /**
197
     * check if transport config is ready
198
     *
199
     * @param array $transportConfig
200
     * @throws Exception
201
     * @return Smtp
202
     */
203
    protected function checkConfig(array $transportConfig): Smtp
204
    {
205
        $isValid = isset($transportConfig[self::_HOST])
206
            && isset($transportConfig[self::_PORT])
207
            && isset($transportConfig[self::_USERNAME])
208
            && isset($transportConfig[self::_PASSWORD])
209
            && isset($transportConfig[self::_ENCRYPTION]);
210
        if (false === $isValid) {
211
            throw new Exception($this->exMsg(self::_ERROR_INVALID_CONF));
212
        }
213
        return $this;
214
    }
215
216
    /**
217
     * prepare mailer
218
     *
219
     * @return Smtp
220
     */
221
    protected function setMailer(): Smtp
222
    {
223
        $mailSendResult = $this->mailer = new Swift_Mailer($this->transport);
224
        $this->error = ($mailSendResult === 0);
225
        return $this;
226
    }
227
228
    /**
229
     * return prefixed exception message
230
     *
231
     * @param string $errorMessage
232
     * @return string
233
     */
234
    protected function exMsg(string $msg): string
235
    {
236
        return self::_ERROR_PREFIX . $msg;
237
    }
238
}
239