Completed
Push — master ( 2bee40...1e6e9e )
by Joao
02:19
created

src/Envelope.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\Mail;
4
5
use ByJG\Mail\Wrapper\AmazonSesWrapper;
6
use ByJG\Mail\Wrapper\MailgunApiWrapper;
7
use ByJG\Mail\Wrapper\MailWrapperInterface;
8
use ByJG\Mail\Wrapper\MandrillApiWrapper;
9
use ByJG\Mail\Wrapper\PHPMailerWrapper;
10
use ByJG\Mail\Wrapper\SendMailWrapper;
11
use ErrorException;
12
use InvalidArgumentException;
13
14
class Envelope
15
{
16
    protected $_from = "";
17
    protected $_to = [];
18
    protected $_subject = "";
19
    protected $_replyTo = "";
20
    protected $_cc = [];
21
    protected $_bcc = [];
22
    protected $_body = "";
23
    protected $_isHtml = false;
24
    protected $_isEmbbed = false;
25
    protected $_attachment = [];
26
27
    /**
28
     *
29
     * @param string $from
30
     * @param string $to
31
     * @param string $subject
32
     * @param string $body
33
     * @param bool $isHtml
34
     */
35
    public function __construct($from = "", $to = "", $subject = "", $body = "", $isHtml = true)
36
    {
37
        $this->_from = Util::getFullEmail($from);
38
        $this->_subject = $subject;
39
        $this->_isHtml = $isHtml;
40
        $this->_body = $body;
41
42
        if (!empty($to)) {
43
            $this->addTo($to);
44
        }
45
    }
46
47
    /**
48
     *
49
     * @param string $contentName
50
     * @param string $filePath
51
     * @param string $contentType
52
     */
53
    public function addAttachment($contentName, $filePath, $contentType)
54
    {
55
        $this->_attachment[$contentName] = ['content' => $filePath, 'content-type' => $contentType];
56
    }
57
58
    public function getFrom()
59
    {
60
        return $this->_from;
61
    }
62
63
    public function setFrom($email, $name = null)
64
    {
65
        $this->_from = Util::getFullEmail($email, $name);
66
    }
67
68
    public function getTo()
69
    {
70
        return $this->_to;
71
    }
72
73
    public function addTo($email, $name = "")
74
    {
75
        $this->_to[] = Util::getFullEmail($email, $name);
76
    }
77
78
    public function getSubject()
79
    {
80
        return $this->_subject;
81
    }
82
83
    public function setSubject($value)
84
    {
85
        $this->_subject = $value;
86
    }
87
88
    public function getReplyTo()
89
    {
90
        return $this->_replyTo == "" ? $this->getFrom() : $this->_replyTo;
91
    }
92
93
    public function setReplyTo($email)
94
    {
95
        $this->_replyTo = Util::getFullEmail($email);
96
    }
97
98
    public function getCC()
99
    {
100
        return $this->_cc;
101
    }
102
103
    public function addCC($email, $name = null)
104
    {
105
        $this->_cc[] = Util::getFullEmail($email, $name);
106
    }
107
108
    public function getBCC()
109
    {
110
        return $this->_bcc;
111
    }
112
113
    public function addBCC($email, $name = null)
114
    {
115
        $this->_bcc[] = Util::getFullEmail($email, $name);
116
    }
117
118
    public function getBody()
119
    {
120
        return $this->_body;
121
    }
122
123
    public function setBody($value)
124
    {
125
        $this->_body = $value;
126
    }
127
128
    public function getBodyText()
129
    {
130
        $body = preg_replace(
131
            [
132
                '~<div.*?>(.*?)</div>~',
133
                '~<p.*?>(.*?)</p>~',
134
                '~<br.*?>~'
135
            ], [
136
            "$1\n",
137
            "$1\n",
138
            "\n"
139
        ], $this->_body
140
        );
141
142
        return strip_tags($body);
143
    }
144
145
    public function isHtml($value = null)
146
    {
147
        if (!is_null($value) && is_bool($value)) {
148
            $this->_isHtml = $value;
149
        }
150
151
        return $this->_isHtml;
152
    }
153
154
    public function isEmbbed($value = null)
155
    {
156
        if (!is_null($value) && is_bool($value)) {
157
            $this->_isEmbbed = $value;
158
        }
159
160
        return $this->_isEmbbed;
161
    }
162
163
    public function getAttachments()
164
    {
165
        return $this->_attachment;
166
    }
167
168
    public function send(MailWrapperInterface $mailer, $to = "")
169
    {
170
        if (0 === count($this->getTo()) && $to == "") {
171
            throw new ErrorException("Destination Email was not provided");
172
        } elseif ($to != "") {
173
            $this->addTo($to);
174
        }
175
176
        if ($this->getFrom() == "") {
177
            throw new ErrorException("Source Email was not provided");
178
        }
179
180
        $mailer->send($this);
181
    }
182
183
    /**
184
     *
185
     * @param MailConnection $connection
186
     * @return MailWrapperInterface
187
     * @throws InvalidArgumentException
188
     */
189
    public static function mailerFactory(MailConnection $connection)
190
    {
191
        $protocol = $connection->getProtocol();
192
        if (in_array($protocol, ['smtp', 'ssl', 'tls'])) {
193
            $mail = new PHPMailerWrapper($connection);
194
        } elseif ($protocol === "ses") {
195
            $mail = new AmazonSesWrapper($connection);
196
        } elseif ($protocol === "mandrill") {
197
            $mail = new MandrillApiWrapper($connection);
198
        } elseif ($protocol === "sendmail") {
199
            $mail = new SendMailWrapper();
0 ignored issues
show
The call to SendMailWrapper::__construct() misses a required argument $connection.

This check looks for function calls that miss required arguments.

Loading history...
200
        } elseif ($protocol === "mailgun") {
201
            $mail = new MailgunApiWrapper($connection);
202
        } else {
203
            throw new InvalidArgumentException("Connection '" . $connection->getProtocol() . "' is not valid");
204
        }
205
206
        return $mail;
207
    }
208
}
209