Completed
Push — master ( 8be84a...55d4ba )
by Guido
03:26
created

GvEmail::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
namespace Gvera\Helpers\email;
3
4
use Gvera\Helpers\config\Config;
5
use PHPMailer\PHPMailer\Exception;
6
use PHPMailer\PHPMailer\PHPMailer;
7
8
/**
9
 * Class GVEmail
10
 * @package Gvera\Helpers\email\GVEmail
11
 * This class is a wrapper from the PhpMailer class just for easiness purposes.
12
 * @Inject config
13
 */
14
class GvEmail
15
{
16
    private PHPMailer $mailer;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
17
18
    private bool $isHtml = false;
19
    private string $subject;
20
    private string $body;
21
    private string $alternativeBody;
22
    public Config $config;
23
24
25
    public function addAddress($address, $name = '')
26
    {
27
        $this->mailer->addAddress($address, $name);
28
    }
29
30
    public function addCC($cCopy, $name = '')
31
    {
32
        $this->mailer->addCC($cCopy, $name);
33
    }
34
35
    public function addBcc($bcCopy, $name = '')
36
    {
37
        $this->mailer->addBCC($bcCopy, $name);
38
    }
39
40
    public function addAttachment($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment')
41
    {
42
        $this->mailer->addAttachment($path, $name, $encoding, $type, $disposition);
43
    }
44
45
    /**
46
     * @throws Exception
47
     * @throws \Exception
48
     */
49
    public function send()
50
    {
51
52
        $this->validate();
53
54
        $emailConfig = $this->getEmailConfiguration();
55
        $this->mailer = new PHPMailer();
56
        $this->mailer->isSMTP();
57
        $this->mailer->isHTML($this->isHtml);
58
        $this->mailer->Host = $emailConfig['host'];
59
        $this->mailer->SMTPAuth = (bool)$emailConfig['smtp_auth'];
60
        $this->mailer->From = $emailConfig['username'];
61
        $this->mailer->Password = $emailConfig['password'];
62
        $this->mailer->SMTPSecure = $emailConfig['smtp_secure'];
63
        $this->mailer->Port = $emailConfig['port'];
64
65
        if (!$this->mailer->send()) {
66
            throw new \Exception('Message could not be sent ' . $this->mailer->ErrorInfo);
67
        } else {
68
            //your implementation here
69
        }
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getEmailConfiguration(): array
76
    {
77
        return $this->config->getConfigItem('email');
78
    }
79
80
    /**
81
     * Set the value of isHtml
82
     *
83
     * @return  self
84
     */
85
    public function setIsHtml($isHtml): GvEmail
86
    {
87
        $this->isHtml = $isHtml;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Set the value of subject
94
     *
95
     * @return  self
96
     */
97
    public function setSubject($subject): GvEmail
98
    {
99
        $this->subject = $subject;
100
101
        return $this;
102
    }
103
104
    /**
105
     * Set the value of body
106
     *
107
     * @return  self
108
     */
109
    public function setBody($body): GvEmail
110
    {
111
        $this->body = $body;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Set the value of alternativeBody
118
     *
119
     * @param $alternativeBody
120
     * @return  self
121
     */
122
    public function setAlternativeBody($alternativeBody): GvEmail
123
    {
124
        $this->alternativeBody = $alternativeBody;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @throws \Exception
131
     */
132
    private function validate()
133
    {
134
        if (!$this->isValid()) {
135
            throw new \Exception(
136
                'The email must have a body, an alternative body, and a subject to be valid'
137
            );
138
        }
139
    }
140
141
    /**
142
     * @return boolean
143
     */
144
    private function isValid(): bool
145
    {
146
        return
147
            $this->body !== null &&
148
            $this->alternativeBody !== null &&
149
            $this->subject !== null;
150
    }
151
}
152