Completed
Branch develop (b42970)
by Elf
02:00
created

Mailer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 186
ccs 42
cts 49
cp 0.8571
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSwiftMailerManager() 0 4 1
A setSwiftMailerManager() 0 6 1
A registerMailDriverHandler() 0 6 1
A callMailDriverHandler() 0 10 3
A getSwiftMailerForMessage() 0 10 2
A getSwiftMailer() 0 4 1
A setSwiftMailer() 0 7 1
B send() 0 30 3
A sendSwiftMessage() 0 16 3
A forceReconnection() 0 8 2
1
<?php
2
3
namespace ElfSundae\Multimail;
4
5
use Closure;
6
use Illuminate\Contracts\Mail\Mailable as MailableContract;
7
use Illuminate\Mail\Events\MessageSending;
8
use Illuminate\Mail\Mailer as BaseMailer;
9
use Swift_Mailer;
10
11
class Mailer extends BaseMailer
12
{
13
    /**
14
     * The Swift Mailer Manager instance.
15
     *
16
     * @var \ElfSundae\Multimail\SwiftMailerManager
17
     */
18
    protected $swiftManager;
19
20
    /**
21
     * The registered mail driver handler.
22
     *
23
     * @var \Closure|string
24
     */
25
    protected $mailDriverHandler;
26
27
    /**
28
     * Get the Swift Mailer Manager instance.
29
     *
30
     * @return \ElfSundae\Multimail\SwiftMailerManager
31
     */
32 5
    public function getSwiftMailerManager()
33
    {
34 5
        return $this->swiftManager;
35
    }
36
37
    /**
38
     * Set the Swift Mailer Manager instance.
39
     *
40
     * @param  \ElfSundae\Multimail\SwiftMailerManager  $manager
41
     * @return $this
42
     */
43 7
    public function setSwiftMailerManager(SwiftMailerManager $manager)
44
    {
45 7
        $this->swiftManager = $manager;
46
47 7
        return $this;
48
    }
49
50
    /**
51
     * Register the mail driver handler.
52
     *
53
     * @param  \Closure|string  $handler
54
     * @return $this
55
     */
56 2
    public function registerMailDriverHandler($handler)
57
    {
58 2
        $this->mailDriverHandler = $handler;
59
60 2
        return $this;
61
    }
62
63
    /**
64
     * Call the registered mail driver handler.
65
     *
66
     * @param  mixed  ...$args
67
     * @return mixed
68
     */
69 3
    protected function callMailDriverHandler(...$args)
70
    {
71 3
        if ($this->mailDriverHandler instanceof Closure) {
72 2
            return $this->container->call($this->mailDriverHandler, $args);
73
        }
74
75 1
        if (is_string($this->mailDriverHandler)) {
76
            return $this->container->call($this->mailDriverHandler, $args, 'mailDriver');
77
        }
78 1
    }
79
80
    /**
81
     * Get a Swift Mailer instance for the given message.
82
     *
83
     * @param  mixed  $message
84
     * @return \Swift_Mailer
85
     */
86 3
    protected function getSwiftMailerForMessage($message)
87
    {
88 3
        $swift = $this->callMailDriverHandler($message, $this);
89
90 3
        if ($swift instanceof Swift_Mailer) {
91
            return $swift;
92
        }
93
94 3
        return $this->swiftManager->mailer($swift);
95
    }
96
97
    /**
98
     * Send a new message using a view.
99
     *
100
     * @param  string|array  $view
101
     * @param  array  $data
102
     * @param  \Closure|string  $callback
103
     */
104 3
    public function send($view, array $data = [], $callback = null)
105
    {
106 3
        if ($view instanceof MailableContract) {
107
            return $view->send($this);
108
        }
109
110
        // First we need to parse the view, which could either be a string or an array
111
        // containing both an HTML and plain text versions of the view which should
112
        // be used when sending an e-mail. We will extract both of them out here.
113 3
        list($view, $plain, $raw) = $this->parseView($view);
114
115 3
        $data['message'] = $message = $this->createMessage();
116
117
        // Once we have retrieved the view content for the e-mail we will set the body
118
        // of this message using the HTML type, which will provide a simple wrapper
119
        // to creating view based emails that are able to receive arrays of data.
120 3
        $this->addContent($message, $view, $plain, $raw, $data);
121
122 3
        $this->callMessageBuilder($callback, $message);
123
124 3
        if (isset($this->to['address'])) {
125
            $message->to($this->to['address'], $this->to['name'], true);
126
        }
127
128 3
        $swift = $this->getSwiftMailerForMessage($message);
129
130 3
        $message = $message->getSwiftMessage();
131
132 3
        $this->sendSwiftMessage($message, $swift);
133 3
    }
134
135
    /**
136
     * Send a Swift Message instance.
137
     *
138
     * @param  \Swift_Message  $message
139
     * @param  \Swift_Mailer  $swift
140
     */
141 3
    protected function sendSwiftMessage($message, Swift_Mailer $swift = null)
142
    {
143 3
        if ($this->events) {
144
            $this->events->fire(new MessageSending($message));
145
        }
146
147 3
        if (is_null($swift)) {
148
            $swift = $this->getSwiftMailer();
149
        }
150
151
        try {
152 3
            return $swift->send($message, $this->failedRecipients);
153
        } finally {
154 3
            $this->forceReconnection($swift);
155
        }
156
    }
157
158
    /**
159
     * Force the transport to re-connect.
160
     *
161
     * This will prevent errors in daemon queue situations.
162
     *
163
     * @param  \Swift_Mailer  $swift
164
     */
165 3
    protected function forceReconnection(Swift_Mailer $swift = null)
166
    {
167 3
        if (is_null($swift)) {
168
            $swift = $this->getSwiftMailer();
169
        }
170
171 3
        $swift->getTransport()->stop();
172 3
    }
173
174
    /**
175
     * Get the Swift Mailer instance.
176
     *
177
     * @return \Swift_Mailer
178
     */
179 1
    public function getSwiftMailer()
180
    {
181 1
        return $this->swiftManager->mailer();
182
    }
183
184
    /**
185
     * Set the Swift Mailer instance.
186
     *
187
     * @param  string|\Swift_Mailer  $swift
188
     */
189 2
    public function setSwiftMailer($swift)
190
    {
191 2
        $this->swiftManager->setDefaultMailer($swift);
192
193
        // Our $swift is managed by the SwiftMailerManager singleton,
194
        // so just let $this->swift go.
195 2
    }
196
}
197