-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
92 lines (79 loc) · 3.2 KB
/
Plugin.php
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php namespace Winter\DriverSendGrid;
use App;
use Event;
use System\Classes\PluginBase;
use System\Models\MailSetting;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
/**
* SendgridDriver Plugin Information File
*/
class Plugin extends PluginBase
{
public $elevated = true;
const MODE_SENDGRID = 'sendgrid';
public function pluginDetails()
{
return [
'name' => 'winter.driversendgrid::lang.plugin_name',
'description' => 'winter.driversendgrid::lang.plugin_description',
'homepage' => 'https://github.com/wintercms/wn-driversendgrid-plugin',
'author' => 'Winter CMS',
'icon' => 'icon-leaf',
];
}
public function register()
{
Event::listen('mailer.beforeRegister', function ($mailManager) {
$mailManager->extend(self::MODE_SENDGRID, function ($config) {
$factory = new SendgridTransportFactory();
if (!isset($config['api_key'])) {
$config = $this->app['config']->get('services.sendgrid', []);
}
return $factory->create(new Dsn(
'sendgrid+'.($config['scheme'] ?? 'api'),
$config['endpoint'] ?? 'default',
$config['api_key']
));
});
$settings = MailSetting::instance();
if ($settings->send_mode === self::MODE_SENDGRID) {
$config = App::make('config');
$config->set('mail.mailers.sendgrid.transport', self::MODE_SENDGRID);
$config->set('services.sendgrid.api_key', $settings->sendgrid_api_key);
}
});
}
public function boot()
{
MailSetting::extend(function ($model) {
$model->bindEvent('model.beforeValidate', function () use ($model) {
$model->rules['sendgrid_api_key'] = 'required_if:send_mode,' . self::MODE_SENDGRID;
});
$model->sendgrid_api_key = config('services.sendgrid.api_key', env('SENDGRID_API_KEY'));
});
Event::listen('backend.form.extendFields', function ($widget) {
if (!$widget->getController() instanceof \System\Controllers\Settings) {
return;
}
if (!$widget->model instanceof MailSetting) {
return;
}
$field = $widget->getField('send_mode');
$field->options(array_merge($field->options(), [self::MODE_SENDGRID => 'SendGrid']));
$widget->addTabFields([
'sendgrid_api_key' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.driversendgrid::lang.sendgrid_api_key',
'type' => 'sensitive',
'commentAbove' => 'winter.driversendgrid::lang.sendgrid_api_key_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[sendgrid]'
]
],
]);
});
}
}