1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace MailMarketing; |
5
|
|
|
|
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use MailMarketing\Drivers\CampaignMonitor; |
9
|
|
|
use MailMarketing\Drivers\MailChimp; |
10
|
|
|
use MailMarketing\Drivers\MailMarketingInterface; |
11
|
|
|
|
12
|
|
|
class MailMarketingManager |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The application instance. |
16
|
|
|
*/ |
17
|
|
|
protected Application $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The array of resolved services. |
21
|
|
|
*/ |
22
|
|
|
protected array $services = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The registered custom driver creators. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected array $customCreators = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new Mail marketing manager instance. |
33
|
|
|
* @param Application $app |
34
|
|
|
*/ |
35
|
6 |
|
public function __construct(Application $app) |
36
|
|
|
{ |
37
|
6 |
|
$this->app = $app; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function extend($driver, \Closure $callback) |
41
|
|
|
{ |
42
|
|
|
$this->customCreators[$driver] = $callback->bindTo($this, $this); |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
public function getDefaultDriver() |
48
|
|
|
{ |
49
|
4 |
|
return $this->app['config']['mail-marketing.default']; |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
protected function getConfig(string $name) |
53
|
|
|
{ |
54
|
5 |
|
return $this->app['config']["mail-marketing.{$name}"] ?? null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Alternative of 'driver' function |
59
|
|
|
* @param null $service |
|
|
|
|
60
|
|
|
* |
61
|
|
|
* @return MailMarketingInterface |
62
|
|
|
*/ |
63
|
2 |
|
public function service($service = null): MailMarketingInterface |
64
|
|
|
{ |
65
|
2 |
|
return $this->driver($service); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get a driver instance. |
70
|
|
|
* |
71
|
|
|
* @param string|null $driver |
72
|
|
|
* @return MailMarketingInterface |
73
|
|
|
*/ |
74
|
5 |
|
public function driver(?string $driver = null): MailMarketingInterface |
75
|
|
|
{ |
76
|
5 |
|
return $this->get($driver ?? $this->getDefaultDriver()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create a driver instance. |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* @return MailMarketingInterface |
84
|
|
|
*/ |
85
|
5 |
|
protected function get(string $name): MailMarketingInterface |
86
|
|
|
{ |
87
|
5 |
|
return $this->services[$name] ?? ($this->services[$name] = $this->resolve($name)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $name |
92
|
|
|
* @return MailMarketingInterface |
93
|
|
|
*/ |
94
|
5 |
|
protected function resolve(string $name): MailMarketingInterface |
95
|
|
|
{ |
96
|
5 |
|
$config = $this->getConfig($name); |
97
|
|
|
|
98
|
5 |
|
if (is_null($config)) { |
99
|
2 |
|
throw new InvalidArgumentException("Mail Marketing service [{$name}] is not defined."); |
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
if (isset($this->customCreators[$name])) { |
103
|
|
|
return $this->callCustomCreator($name, $config); |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
$driverMethod = 'create' . ucfirst($name) . 'Driver'; |
107
|
|
|
|
108
|
5 |
|
if (method_exists($this, $driverMethod)) { |
109
|
4 |
|
return $this->{$driverMethod}($config); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
throw new InvalidArgumentException("Driver [{$name}] is not supported. [{$driverMethod}]"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Call a custom driver creator. |
117
|
|
|
* |
118
|
|
|
* @param array $config |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
protected function callCustomCreator(string $name, array $config): MailMarketingInterface |
122
|
|
|
{ |
123
|
|
|
return $this->customCreators[$name]($this->app, $config); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
protected function createMailchimpDriver(array $config): MailChimp |
127
|
|
|
{ |
128
|
3 |
|
return new MailChimp($config); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
protected function createCampaignmonitorDriver(array $config): CampaignMonitor |
132
|
|
|
{ |
133
|
1 |
|
return new CampaignMonitor($config); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Dynamically call the default driver instance. |
138
|
|
|
* |
139
|
|
|
* @param string $method |
140
|
|
|
* @param array $parameters |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
1 |
|
public function __call($method, $parameters) |
144
|
|
|
{ |
145
|
1 |
|
return $this->service()->$method(...$parameters); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|