SwiftMailerManager   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 65%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 17
c 6
b 0
f 0
lcom 1
cbo 3
dl 0
loc 159
ccs 26
cts 40
cp 0.65
rs 10

12 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 resetMailer() 0 9 2
A resetMailers() 0 7 1
A validDriverName() 0 10 3
A createDriver() 0 4 1
A getDefaultDriver() 0 4 1
A mailer() 0 4 1
A setDefaultDriver() 0 6 1
A setDefaultMailer() 0 8 2
1
<?php
2
3
namespace ElfSundae\Multimail;
4
5
use Illuminate\Support\Manager;
6
use Swift_Mailer;
7
8
class SwiftMailerManager extends Manager
9
{
10
    /**
11
     * The Transport manager.
12
     *
13
     * @var \ElfSundae\Multimail\TransportManager
14
     */
15
    protected $transportManager;
16
17
    /**
18
     * Get the Transport manager.
19
     *
20
     * @return \ElfSundae\Multimail\TransportManager
21
     */
22
    public function getTransportManager()
23
    {
24
        return $this->transportManager;
25
    }
26
27
    /**
28
     * Set the Transport manager.
29
     *
30
     * @param  \ElfSundae\Multimail\TransportManager  $manager
31
     * @return $this
32
     */
33 5
    public function setTransportManager(TransportManager $manager)
34
    {
35 5
        $this->transportManager = $manager;
36
37 5
        return $this;
38
    }
39
40
    /**
41
     * Get a Swift Mailer instance.
42
     *
43
     * @param  string|null  $driver
44
     * @return \Swift_Mailer
45
     */
46 1
    public function mailer($driver = null)
47
    {
48 1
        return $this->driver($driver);
49
    }
50
51
    /**
52
     * Get all of the created Swift Mailer instances.
53
     *
54
     * @return array
55
     */
56
    public function getMailers()
57
    {
58
        return $this->drivers;
59
    }
60
61
    /**
62
     * Get the name of mail driver for the given Swift Mailer instance.
63
     *
64
     * @param  \Swift_Mailer  $mailer
65
     * @return string|null
66
     */
67
    public function getDriverForMailer(Swift_Mailer $mailer)
68
    {
69
        return array_search($mailer, $this->drivers) ?: null;
70
    }
71
72
    /**
73
     * Reset a Swift Mailer instance.
74
     *
75
     * @param  string|\Swift_Mailer  $mailer
76
     * @return $this
77
     */
78 1
    public function resetMailer($mailer)
79
    {
80 1
        if ($driver = $this->validDriverName($mailer)) {
81 1
            unset($this->drivers[$driver]);
82 1
            $this->transportManager->resetDriver($driver);
83 1
        }
84
85 1
        return $this;
86
    }
87
88
    /**
89
     * Reset all of the created Swift Mailer instances.
90
     *
91
     * @return $this
92
     */
93 1
    public function resetMailers()
94
    {
95 1
        $this->drivers = [];
96 1
        $this->transportManager->resetDrivers();
97
98 1
        return $this;
99
    }
100
101
    /**
102
     * Validate the given driver or mailer, and return the driver name.
103
     *
104
     * @param  mixed  $driver
105
     * @return string|null
106
     */
107 1
    protected function validDriverName($driver)
108
    {
109 1
        if ($driver instanceof Swift_Mailer) {
110
            $driver = $this->getDriverForMailer($driver);
111
        }
112
113 1
        if (is_string($driver)) {
114 1
            return $driver;
115
        }
116
    }
117
118
    /**
119
     * Create a new Swift Mailer instance.
120
     *
121
     * @param  string  $driver
122
     * @return \Swift_Mailer
123
     */
124 1
    protected function createDriver($driver)
125
    {
126 1
        return new Swift_Mailer($this->transportManager->driver($driver));
127
    }
128
129
    /**
130
     * Get the default mail driver name.
131
     *
132
     * @return string
133
     */
134 1
    public function getDefaultDriver()
135
    {
136 1
        return $this->transportManager->getDefaultDriver();
137
    }
138
139
    /**
140
     * Set the default mail driver name.
141
     *
142
     * @param  string  $driver
143
     * @return $this
144
     */
145 1
    public function setDefaultDriver($driver)
146
    {
147 1
        $this->transportManager->setDefaultDriver($driver);
148
149 1
        return $this;
150
    }
151
152
    /**
153
     * Set the default Swift Mailer.
154
     *
155
     * @param  string|\Swift_Mailer  $mailer
156
     * @return $this
157
     */
158
    public function setDefaultMailer($mailer)
159
    {
160
        if ($driver = $this->validDriverName($mailer)) {
161
            $this->setDefaultDriver($driver);
162
        }
163
164
        return $this;
165
    }
166
}
167