-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
61 lines (52 loc) · 2.02 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
<?php namespace PlanetaDelEste\PostmarkDriver;
use Backend\Widgets\Form;
use Coconuts\Mail\PostmarkTransport;
use Event;
use GuzzleHttp\Client;
use System\Classes\PluginBase;
use System\Models\MailSetting;
/**
* Class Plugin
* @package PlanetaDelEste\PostmarkDriver
*/
class Plugin extends PluginBase
{
const MODE_POSTMARK = 'postmark';
public function boot()
{
Event::listen('backend.form.extendFields', function (Form $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' => 'planetadeleste.postmarkdriver::lang.field.secret',
'comment' => 'planetadeleste.postmarkdriver::lang.field.secret_comment',
'trigger' => [
'action' => 'show',
'field' => 'send_mode',
'condition' => 'value[postmark]'
]
],
]);
});
\App::extend('swift.transport', function (\Illuminate\Mail\TransportManager $manager) {
return $manager->extend(self::MODE_POSTMARK, function () {
$settings = MailSetting::instance();
$client = new Client(config('postmark.guzzle'));
return new PostmarkTransport($client, $settings->postmark_secret);
});
});
MailSetting::extend(function ($model) {
$model->bindEvent('model.beforeValidate', function () use ($model) {
$model->rules['postmark_secret'] = 'required_if:send_mode,' . self::MODE_POSTMARK;
});
});
}
}