Completed
Push — master ( 83a0ec...c47512 )
by Elf
01:58
created

SwiftMailerManager   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 68.63%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 23
c 5
b 0
f 0
lcom 2
cbo 3
dl 0
loc 211
ccs 35
cts 51
cp 0.6863
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getTransportManager() 0 4 1
A setTransportManager() 0 6 1
A getMailers() 0 4 1
A getDriverForMailer() 0 4 2
A getDefaultDriver() 0 4 1
A setDefaultDriver() 0 6 1
A mailer() 0 4 1
A mailerForMessage() 0 10 2
A resetMailers() 0 7 1
A createDriver() 0 4 1
A registerDriverSelector() 0 4 1
A callDriverSelector() 0 10 3
A resetMailer() 0 9 2
A validDriverName() 0 10 3
A setDefaultMailer() 0 8 2
1
<?php
2
3
namespace ElfSundae\Multimail;
4
5
use Closure;
6
use Illuminate\Support\Manager;
7
use Swift_Mailer;
8
use Swift_Message;
9
10
class SwiftMailerManager extends Manager
11
{
12
    /**
13
     * The Transport manager.
14
     *
15
     * @var \ElfSundae\Multimail\TransportManager
16
     */
17
    protected $transportManager;
18
19
    /**
20
     * The registered custom driver selector.
21
     *
22
     * @var \Closure|string
23
     */
24
    protected $driverSelector;
25
26
    /**
27
     * Get the Transport manager.
28
     *
29
     * @return \ElfSundae\Multimail\TransportManager
30
     */
31
    public function getTransportManager()
32
    {
33
        return $this->transportManager;
34
    }
35
36
    /**
37
     * Set the Transport manager.
38
     *
39
     * @param  \ElfSundae\Multimail\TransportManager  $manager
40
     * @return $this
41
     */
42 6
    public function setTransportManager(TransportManager $manager)
43
    {
44 6
        $this->transportManager = $manager;
45
46 6
        return $this;
47
    }
48
49
    /**
50
     * Get a Swift Mailer instance.
51
     *
52
     * @param  string|null  $driver
53
     * @return \Swift_Mailer
54
     */
55 2
    public function mailer($driver = null)
56
    {
57 2
        return $this->driver($driver);
58
    }
59
60
    /**
61
     * Get a Swift Mailer instance for the given message.
62
     *
63
     * @param  \Swift_Message  $message
64
     * @return \Swift_Mailer
65
     */
66 1
    public function mailerForMessage(Swift_Message $message)
67
    {
68 1
        $driver = $this->callDriverSelector($message, $this);
69
70 1
        if ($driver instanceof Swift_Mailer) {
71
            return $driver;
72
        }
73
74 1
        return $this->mailer($driver);
75
    }
76
77
    /**
78
     * Get all of the created Swift Mailer instances.
79
     *
80
     * @return array
81
     */
82
    public function getMailers()
83
    {
84
        return $this->drivers;
85
    }
86
87
    /**
88
     * Get the name of mail driver for the given Swift Mailer instance.
89
     *
90
     * @param  \Swift_Mailer  $mailer
91
     * @return string|null
92
     */
93
    public function getDriverForMailer(Swift_Mailer $mailer)
94
    {
95
        return array_search($mailer, $this->drivers) ?: null;
96
    }
97
98
    /**
99
     * Reset a Swift Mailer instance.
100
     *
101
     * @param  string|\Swift_Mailer  $mailer
102
     * @return $this
103
     */
104 1
    public function resetMailer($mailer)
105
    {
106 1
        if ($driver = $this->validDriverName($mailer)) {
107 1
            unset($this->drivers[$driver]);
108 1
            $this->transportManager->resetDriver($driver);
109
        }
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * Reset all of the created Swift Mailer instances.
116
     *
117
     * @return $this
118
     */
119 1
    public function resetMailers()
120
    {
121 1
        $this->drivers = [];
122 1
        $this->transportManager->resetDrivers();
123
124 1
        return $this;
125
    }
126
127
    /**
128
     * Validate the given driver or mailer, and return the driver name.
129
     *
130
     * @param  mixed  $driver
131
     * @return string|null
132
     */
133 1
    protected function validDriverName($driver)
134
    {
135 1
        if ($driver instanceof Swift_Mailer) {
136
            $driver = $this->getDriverForMailer($driver);
137
        }
138
139 1
        if (is_string($driver)) {
140 1
            return $driver;
141
        }
142
    }
143
144
    /**
145
     * Create a new Swift Mailer instance.
146
     *
147
     * @param  string  $driver
148
     * @return \Swift_Mailer
149
     */
150 2
    protected function createDriver($driver)
151
    {
152 2
        return new Swift_Mailer($this->transportManager->driver($driver));
153
    }
154
155
    /**
156
     * Get the default mail driver name.
157
     *
158
     * @return string
159
     */
160 1
    public function getDefaultDriver()
161
    {
162 1
        return $this->transportManager->getDefaultDriver();
163
    }
164
165
    /**
166
     * Set the default mail driver name.
167
     *
168
     * @param  string  $driver
169
     * @return $this
170
     */
171 1
    public function setDefaultDriver($driver)
172
    {
173 1
        $this->transportManager->setDefaultDriver($driver);
174
175 1
        return $this;
176
    }
177
178
    /**
179
     * Set the default Swift Mailer.
180
     *
181
     * @param  string|\Swift_Mailer  $mailer
182
     * @return $this
183
     */
184
    public function setDefaultMailer($mailer)
185
    {
186
        if ($driver = $this->validDriverName($mailer)) {
187
            $this->setDefaultDriver($driver);
188
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Register the custom driver selector.
195
     *
196
     * @param  \Closure|string  $selector
197
     * @return $this
198
     */
199 1
    public function registerDriverSelector($selector)
200
    {
201 1
        $this->driverSelector = $selector;
202 1
    }
203
204
    /**
205
     * Call the custom driver selector.
206
     *
207
     * @param  mixed  ...$args
208
     * @return mixed
209
     */
210 1
    protected function callDriverSelector(...$args)
211
    {
212 1
        if ($this->driverSelector instanceof Closure) {
213 1
            return call_user_func($this->driverSelector, ...$args);
214
        }
215
216
        if (is_string($this->driverSelector)) {
217
            return $this->app->make($this->driverSelector)->mailDriver(...$args);
218
        }
219
    }
220
}
221