Test Failed
Push — master ( 8c09b3...bc5138 )
by Pierre
03:23
created

Smtp::exMsg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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