1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resohead\LaravelTestMail; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Mail; |
7
|
|
|
use Illuminate\Contracts\Mail\Mailable; |
8
|
|
|
use Illuminate\Support\Facades\Validator; |
9
|
|
|
use Resohead\LaravelTestMail\TestMailable; |
10
|
|
|
use Resohead\LaravelTestMail\SendTestEmailJob; |
11
|
|
|
use \Illuminate\Contracts\Config\Repository as Config; |
12
|
|
|
|
13
|
|
|
class TestMailCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'mail:test {recipient?} {--preset=} {--queue} {--driver=} {--connection=} {--stack=}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Send a test email'; |
28
|
|
|
|
29
|
|
|
protected $validator; |
30
|
|
|
protected $config; |
31
|
|
|
|
32
|
|
|
protected $preset; |
33
|
|
|
protected $recipient; |
34
|
|
|
protected $driver; |
35
|
|
|
protected $stack; |
36
|
|
|
protected $connection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new command instance. |
40
|
|
|
* |
41
|
|
|
* @return void |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct(Validator $validator, Config $config) |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
$this->validator = $validator; |
47
|
|
|
$this->config = $config; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Execute the console command. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function handle() |
56
|
|
|
{ |
57
|
|
|
$this->setArgumentDefaults(); |
58
|
|
|
|
59
|
|
|
$validation = $this->validator::make([ |
60
|
|
|
'email' => $this->recipient, |
61
|
|
|
'driver' => $this->driver, |
62
|
|
|
'preset' => $this->preset |
63
|
|
|
], $this->rules() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if ($validation->fails()) { |
67
|
|
|
collect($validation->errors()->all())->each(function($error){ |
68
|
|
|
$this->error($error); |
69
|
|
|
}); |
70
|
|
|
return 1; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->config->set('mail.driver', $this->driver); |
74
|
|
|
|
75
|
|
|
$mailable = new TestMailable($this->recipient); |
76
|
|
|
|
77
|
|
|
$this->isOnQueue() |
78
|
|
|
? Mail::queue($mailable->onConnection($this->connection)->onQueue($this->stack)) |
79
|
|
|
: Mail::send($mailable); |
80
|
|
|
|
81
|
|
|
$this->comment("A test email ($this->driver) has been sent to $this->recipient"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function setArgumentDefaults() |
85
|
|
|
{ |
86
|
|
|
$this->setPreset(); |
87
|
|
|
$this->setRecipient(); |
88
|
|
|
$this->setDriver(); |
89
|
|
|
$this->setConnection(); |
90
|
|
|
$this->setStack(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getPresetValue($key, $default = null) |
94
|
|
|
{ |
95
|
|
|
return $this->config->get( |
96
|
|
|
implode('.', [$this->getConfigPath(), $this->preset, $key]), |
97
|
|
|
$default |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getConfigPath() |
102
|
|
|
{ |
103
|
|
|
return 'mail-test.presets'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getConfigKeys() |
107
|
|
|
{ |
108
|
|
|
return array_keys( |
109
|
|
|
$this->config->get($this->getConfigPath()) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function setPreset() |
114
|
|
|
{ |
115
|
|
|
$this->preset = $this->option('preset'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function setRecipient() |
119
|
|
|
{ |
120
|
|
|
$this->recipient = $this->argument('recipient') ?? |
121
|
|
|
$this->getPresetValue('recipient', $this->config->get('mail.from.address')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function setDriver() |
125
|
|
|
{ |
126
|
|
|
$this->driver = $this->option('driver') ?? |
127
|
|
|
$this->getPresetValue('driver', $this->config->get('mail.driver')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function setConnection() |
131
|
|
|
{ |
132
|
|
|
$this->connection = $this->option('connection') ?? |
133
|
|
|
$this->getPresetValue('connection', $this->config->get('queue.default')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function setStack() |
137
|
|
|
{ |
138
|
|
|
$this->stack = $this->option('stack') ?: $this->getPresetValue('stack', 'default'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function isOnQueue(): bool |
142
|
|
|
{ |
143
|
|
|
return $this->hasQueueOptions() || $this->hasQueuePresets(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function hasQueueOptions() |
147
|
|
|
{ |
148
|
|
|
return $this->option('queue') || $this->option('stack') || $this->option('connection'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function hasQueuePresets() |
152
|
|
|
{ |
153
|
|
|
return $this->getPresetValue('queue') || $this->getPresetValue('connection') || $this->getPresetValue('stack'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function rules(): array |
157
|
|
|
{ |
158
|
|
|
return [ |
159
|
|
|
'email' => 'email', |
160
|
|
|
'driver' => 'required', |
161
|
|
|
'preset' => 'nullable|sometimes|in:'.implode(',', $this->getConfigKeys()), |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.