Passed
Push — master ( 41c09c...5f5681 )
by Iman
06:15
created

Mailer::sendEmailQueue()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
nc 1
nop 1
dl 0
loc 21
c 0
b 0
f 0
cc 3
rs 9.3142
1
<?php
2
3
namespace crocodicstudio\crudbooster\helpers;
4
5
class Mailer
6
{
7
    private $attachments;
8
9
    private $reciever;
10
11
    public function send($config)
12
    {
13
        $this->setConfigs();
14
15
        $this->reciever = $config['to'];
16
        $template = $config['template'];
17
18
        $template = CRUDBooster::first('cms_email_templates', ['slug' => $template]);
19
        $html = $template->content;
20
        foreach ($config['data'] as $key => $val) {
21
            $html = str_replace('['.$key.']', $val, $html);
22
            $template->subject = str_replace('['.$key.']', $val, $template->subject);
23
        }
24
        $subject = $template->subject;
25
        $this->attachments = ($config['attachments']) ?: [];
26
27
        $this->sendMail($html, $subject, $template);
28
    }
29
30
    private function setConfigs()
31
    {
32
        Config::set('mail.driver', SettingRepo::getSetting('smtp_driver'));
0 ignored issues
show
Bug introduced by
The type crocodicstudio\crudbooster\helpers\SettingRepo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type crocodicstudio\crudbooster\helpers\Config was not found. Did you mean Config? If so, make sure to prefix the type with \.
Loading history...
33
        Config::set('mail.host', SettingRepo::getSetting('smtp_host'));
34
        Config::set('mail.port', SettingRepo::getSetting('smtp_port'));
35
        Config::set('mail.username', SettingRepo::getSetting('smtp_username'));
36
        Config::set('mail.password', SettingRepo::getSetting('smtp_password'));
37
    }
38
39
    /**
40
     * @param $html
41
     * @param $subject
42
     * @param $template
43
     */
44
    private function sendMail($html, $subject, $template)
45
    {
46
        \Mail::send("crudbooster::emails.blank", ['content' => $html], function ($message) use ($subject, $template) {
47
            $message->priority(1);
48
            $message->to($this->reciever);
49
50
            if ($template->from_email) {
51
                $from_name = ($template->from_name) ?: SettingRepo::getSetting('appname');
52
                $message->from($template->from_email, $from_name);
53
            }
54
55
            if ($template->cc_email) {
56
                $message->cc($template->cc_email);
57
            }
58
59
            if (count($this->attachments)) {
60
                foreach ($this->attachments as $attachment) {
61
                    $message->attach($attachment);
62
                }
63
            }
64
65
            $message->subject($subject);
66
        });
67
    }
68
}