-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
43 lines (35 loc) · 1.07 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package email
import (
"github.com/goal-web/contracts"
"github.com/goal-web/supports/exceptions"
"github.com/goal-web/supports/utils"
)
type Factory struct {
config Config
mailers map[string]contracts.Mailer
drivers map[string]contracts.MailerDriver
}
func (factory *Factory) Mailer(name ...string) contracts.Mailer {
mailer := factory.config.Default
if len(name) > 0 {
mailer = name[0]
}
return factory.getMailer(mailer)
}
func (factory *Factory) getMailer(name string) contracts.Mailer {
if factory.mailers[name] == nil {
config := factory.config.Mailers[name]
if config == nil {
panic(Exception{Exception: exceptions.New("factory.getMailer: mailer does not exist", config)})
}
if driver, ok := factory.drivers[utils.GetStringField(config, "driver")]; ok {
factory.mailers[name] = driver(name, config)
} else {
panic(Exception{Exception: exceptions.New("factory.getMailer: driver does not exist", config)})
}
}
return factory.mailers[name]
}
func (factory *Factory) Extend(name string, driver contracts.MailerDriver) {
factory.drivers[name] = driver
}