Passed
Branch master (634da9)
by refat
03:15
created

Email::recipients()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 4
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
use PHPMailer\PHPMailer\PHPMailer;
6
use PHPMailer\PHPMailer\SMTP;
7
use PHPMailer\PHPMailer\Exception;
8
use System\Error;
9
10
class Email
11
{
12
    /**
13
     * Application Object
14
     *
15
     * @var \System\Application
16
     */
17
    private $app;
18
19
    /**
20
     * PHPMailer Object
21
     *
22
     * @var PHPMailer
23
     */
24
    private $mail;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param \System\Application $app
30
     */
31
    public function __construct(Application $app)
32
    {
33
        $this->app = $app;
34
35
        $this->mail = new PHPMailer(true);
36
37
        $this->setUp();
38
    }
39
40
    /**
41
     * Set up the configrations
42
     *
43
     * @return void
44
     */
45
    private function setUp()
46
    {
47
        $this->mail->SMTPDebug = $this->error->allowDisplayingError() ? SMTP::DEBUG_SERVER : 0;
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
48
        $this->mail->isSMTP();
49
        $this->mail->Host = $_ENV['EMAIL_HOST'];
50
        $this->mail->SMTPAuth = $_ENV['EMAIL_SMTPAUTH'];
51
        $this->mail->Username = $_ENV['EMAIL_USERNAME'];
52
        $this->mail->Password = $_ENV['EMAIL_PASSWORD'];
53
        $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
54
        $this->mail->Port = $_ENV['EMAIL_PORT'];
55
    }
56
57
    /**
58
     * To add addresses or attachments easily to the object
59
     *
60
     * @return void
61
     */
62
    private function add($input, $add)
63
    {
64
        if (!is_array($input)) {
65
            $input = [$input];
66
        }
67
68
        foreach ($input as $key => $value) {
69
            if (is_numeric($key)) {
70
                $this->mail->$add($value);
71
            } else {
72
                $this->mail->$add($value, $key);
73
            }
74
        }
75
    }
76
77
    /**
78
     * Add recipients to email
79
     *
80
     * @return $this
81
     */
82
    public function recipients($addresses, array $replayTo = [], $cc = null, $bcc = null)
83
    {
84
        $this->mail->setFrom($_ENV['EMAIL_ADMIN'], $_ENV['EMAIL_NAME']);
85
86
        $this->addresses($addresses);
87
88
        if (!empty($replayTo)) {
89
            $this->mail->addReplyTo(array_values($replayTo)[0], array_keys($replayTo)[0]);
90
        }
91
92
        if ($cc) {
93
            $this->mail->addCC($cc);
94
        }
95
96
        if ($bcc) {
97
            $this->mail->addBCC($bcc);
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * Add addresses
105
     *
106
     * @return void
107
     */
108
    private function addresses($addresses)
109
    {
110
        $this->add($addresses, 'addAddress');
111
    }
112
113
    /**
114
     * Add attachments to email
115
     *
116
     * @return $this
117
     */
118
    public function attachments($attachments)
119
    {
120
        $this->add($attachments, 'addAttachment');
121
122
        return $this;
123
    }
124
125
    /**
126
     * Add content to email
127
     *
128
     * @return $this
129
     */
130
    public function content($html, $subject, $body, $altBody)
131
    {
132
        $this->mail->isHTML($html);
133
        $this->mail->Subject = $subject;
134
        $this->mail->Body = $body;
135
        $this->mail->AltBody = $altBody;
136
137
        return $this;
138
    }
139
140
    /**
141
     * Send email
142
     *
143
     * @return void
144
     */
145
    public function send()
146
    {
147
        pre($this->mail);
0 ignored issues
show
Documentation introduced by
$this->mail is of type object<PHPMailer\PHPMailer\PHPMailer>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
        try {
149
            // $this->mail->send();
150
        } catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\PHPMailer\PHPMai...s->mail->ErrorInfo}"; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
151
            echo "Message could not be sent. Mailer Error: {$this->mail->ErrorInfo}";
152
        }
153
    }
154
}
155