-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.php
More file actions
92 lines (79 loc) · 3.18 KB
/
Plugin.php
File metadata and controls
92 lines (79 loc) · 3.18 KB
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\DriverPostmark;
use App;
use Event;
use System\Classes\PluginBase;
use System\Models\MailSetting;
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
/**
* DriverPostmark Plugin Information File
*/
class Plugin extends PluginBase
{
public $elevated = true;
const MODE_POSTMARK = 'postmark';
public function pluginDetails()
{
return [
'name' => 'winter.driverpostmark::lang.plugin.name',
'description' => 'winter.driverpostmark::lang.plugin.description',
'homepage' => 'https://github.com/wintercms/wn-driverpostmark-plugin',
'author' => 'Winter CMS',
'icon' => 'icon-leaf',
];
}
public function register()
{
Event::listen('mailer.beforeRegister', function ($mailManager) {
$mailManager->extend(self::MODE_POSTMARK, function ($config) {
$factory = new PostmarkTransportFactory();
if (!isset($config['secret'])) {
$config = $this->app['config']->get('services.postmark', []);
}
return $factory->create(new Dsn(
'postmark+'.($config['scheme'] ?? 'api'),
$config['endpoint'] ?? 'default',
$config['secret']
));
});
$settings = MailSetting::instance();
if ($settings->send_mode === self::MODE_POSTMARK) {
$config = App::make('config');
$config->set('mail.mailers.postmark.transport', self::MODE_POSTMARK);
$config->set('services.postmark.secret', $settings->postmark_secret);
}
});
}
public function boot()
{
MailSetting::extend(function ($model) {
$model->bindEvent('model.beforeValidate', function () use ($model) {
$model->rules['postmark_secret'] = 'required_if:send_mode,' . self::MODE_POSTMARK;
});
$model->postmark_secret = config('services.postmark.secret', env('POSTMARK_SECRET'));
});
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_POSTMARK => 'Postmark']));
$widget->addTabFields([
'postmark_secret' => [
'tab' => 'system::lang.mail.general',
'label' => 'winter.driverpostmark::lang.postmark_secret',
'type' => 'sensitive',
'commentAbove' => 'winter.driverpostmark::lang.postmark_secret_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[postmark]'
]
],
]);
});
}
}