Passed
Push — master ( 75ec53...0424dc )
by Iman
04:49
created

Mailer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B sendMail() 0 22 6
A setConfigs() 0 7 1
A send() 0 17 3
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\EmailTemplates;
4
5
use crocodicstudio\crudbooster\helpers\CRUDBooster;
6
use crocodicstudio\crudbooster\Modules\SettingModule\SettingRepo;
7
use Illuminate\Support\Facades\Config;
8
9
class Mailer
10
{
11
    private $attachments;
12
13
    private $reciever;
14
15
    public function send($config)
16
    {
17
        $this->setConfigs();
18
19
        $this->reciever = $config['to'];
20
        $template = $config['template'];
21
22
        $template = CRUDBooster::first('cms_email_templates', ['slug' => $template]);
23
        $html = $template->content;
24
        foreach ($config['data'] as $key => $val) {
25
            $html = str_replace('['.$key.']', $val, $html);
26
            $template->subject = str_replace('['.$key.']', $val, $template->subject);
27
        }
28
        $subject = $template->subject;
29
        $this->attachments = ($config['attachments']) ?: [];
30
31
        $this->sendMail($html, $subject, $template);
32
    }
33
34
    private function setConfigs()
35
    {
36
        Config::set('mail.driver', SettingRepo::getSetting('smtp_driver'));
37
        Config::set('mail.host', SettingRepo::getSetting('smtp_host'));
38
        Config::set('mail.port', SettingRepo::getSetting('smtp_port'));
39
        Config::set('mail.username', SettingRepo::getSetting('smtp_username'));
40
        Config::set('mail.password', SettingRepo::getSetting('smtp_password'));
41
    }
42
43
    /**
44
     * @param $html
45
     * @param $subject
46
     * @param $template
47
     */
48
    private function sendMail($html, $subject, $template)
49
    {
50
        \Mail::send("crudbooster::emails.blank", ['content' => $html], function ($message) use ($subject, $template) {
51
            $message->priority(1);
52
            $message->to($this->reciever);
53
54
            if ($template->from_email) {
55
                $from_name = ($template->from_name) ?: SettingRepo::getSetting('appname');
56
                $message->from($template->from_email, $from_name);
57
            }
58
59
            if ($template->cc_email) {
60
                $message->cc($template->cc_email);
61
            }
62
63
            if (count($this->attachments)) {
64
                foreach ($this->attachments as $attachment) {
65
                    $message->attach($attachment);
66
                }
67
            }
68
69
            $message->subject($subject);
70
        });
71
    }
72
}