Passed
Push — dev5a ( 720ced...bf2271 )
by Ron
07:37
created

SetEmailProperties::saveEmailSettings()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 8
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
namespace App\Domains\Admin;
4
5
use Illuminate\Mail\Mailer;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Mail;
9
10
use App\Mail\TestEmail;
11
12
use Swift_Mailer;
13
use Swift_SmtpTransport;
14
15
class SetEmailProperties extends SettingsDomain
16
{
17 4
    public function saveEmailSettings($request)
18
    {
19 4
        $username = $request->authentication ? $request->username : null;
20 4
        $password = $request->authentication ? $request->password : null;
21
        $mailSettings = [
22 4
            'mail.from.address' => $request->from_address,
23 4
            'mail.username'     => $username,
24 4
            'mail.password'     => $password,
25 4
            'mail.host'         => $request->host,
26 4
            'mail.port'         => $request->port,
27 4
            'mail.encryption'   => $request->encryption
28
        ];
29
30 4
        foreach($mailSettings as $key => $value)
31
        {
32 4
            $this->updateSettings($key, $value);
33
        }
34
35 4
        return true;
36
    }
37
38
    public function sendTestEmail($request, $emailAddress)
39
    {
40
        $password = $request->password === 'RandomString' ? config('mail.password') : $request->password;
41
42
        $transport = new Swift_SmtpTransport($request->host, $request->port);
43
        $transport->setUsername($request->username);
44
        $transport->setPassword($password);
45
        $transport->setEncryption($request->encryption);
46
47
        $swiftMailer = new Swift_Mailer($transport);
48
49
        $mail = new Mailer('Test Email', app()->get('view'), $swiftMailer);
50
        $mail->alwaysFrom($request->from_address);
51
        $mail->alwaysReplyTo($request->from_address);
52
53
        try
54
        {
55
            $mail->to($emailAddress)->send(new TestEmail);
56
        }
57
        catch (\Exception $e)
58
        {
59
            return $e->getMessage();
60
        }
61
62
        return true;
63
    }
64
}
65