Mail::prepare()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php 
2
3
namespace Yaro\LogEnvelope\Drivers;
4
5
use Illuminate\Support\Facades\Mail as MailFacade;
6
use Yaro\LogEnvelope\Mail\LogEmail;
7
8
class Mail extends AbstractDriver
9
{
10
    
11
    protected function prepare() 
12
    {
13
        $this->config['from_name']  = $this->config['from_name'] ?: 'Log Envelope';
14
        $this->config['from_email'] = $this->config['from_email'] ?: 'logenvelope@'. $this->data['host'];
15
    } // end prepare
16
    
17
    protected function check() 
18
    {
19
        return $this->isEnabled() && (isset($this->config['to']) && $this->config['to']);
20
    } // end check
21
    
22
    public function send()
23
    {
24
        if (!$this->check()) {
25
            return;
26
        }
27
        
28
        $data = $this->data;
29
        $config = $this->config;
30
        
31
        if ($this->isMailablePossible()) {
32
            MailFacade::queue(new LogEmail($data, $config));
33
            return;
34
        }
35
        
36
        MailFacade::queue('log-envelope::main', $data, function($message) use ($data, $config) {
37
            $subject = sprintf('[%s] @ %s: %s', $data['class'], $data['host'], $data['exception']);
38
            
39
             // to protect from gmail's anchors automatic generation
40
            $message->setBody(
41
                preg_replace(
42
                    ['~\.~', '~http~'],
43
                    ['<span>.</span>', '<span>http</span>'],
44
                    $message->getBody()
45
                )
46
            );
47
            
48
             $message->to($config['to'])
49
                     ->from($config['from_email'], $config['from_name'])
50
                     ->subject($subject);
51
        });
52
    } // end send
53
    
54
    private function isMailablePossible()
55
    {
56
        return class_exists('Illuminate\Mail\Mailable');
57
    } // end isMailablePossible
58
    
59
}
60