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 | 16 | public function __construct(Validator $validator, Config $config) |
|
44 | { |
||
45 | 16 | parent::__construct(); |
|
46 | 16 | $this->validator = $validator; |
|
47 | 16 | $this->config = $config; |
|
48 | 16 | } |
|
49 | |||
50 | /** |
||
51 | * Execute the console command. |
||
52 | * |
||
53 | * @return mixed |
||
54 | */ |
||
55 | 16 | public function handle() |
|
56 | { |
||
57 | 16 | $this->setArgumentDefaults(); |
|
58 | |||
59 | 16 | $validation = $this->validator::make([ |
|
60 | 16 | 'email' => $this->recipient, |
|
61 | 16 | 'driver' => $this->driver, |
|
62 | 16 | 'preset' => $this->preset |
|
63 | 16 | ], $this->rules() |
|
64 | ); |
||
65 | |||
66 | 16 | if ($validation->fails()) { |
|
67 | collect($validation->errors()->all())->each(function($error){ |
||
68 | 5 | $this->error($error); |
|
69 | 5 | }); |
|
70 | 5 | return 1; |
|
71 | } |
||
72 | |||
73 | 11 | $this->config->set('mail.driver', $this->driver); |
|
74 | |||
75 | 11 | $mailable = new TestMailable($this->recipient); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
76 | |||
77 | 11 | $this->isOnQueue() |
|
78 | 7 | ? Mail::queue($mailable->onConnection($this->connection)->onQueue($this->stack)) |
|
79 | 4 | : Mail::send($mailable); |
|
80 | |||
81 | 11 | $this->comment("A test email ($this->driver) has been sent to $this->recipient"); |
|
82 | 11 | } |
|
83 | |||
84 | 16 | protected function setArgumentDefaults(): void |
|
85 | { |
||
86 | 16 | $this->setPreset(); |
|
87 | 16 | $this->setRecipient(); |
|
88 | 16 | $this->setDriver(); |
|
89 | 16 | $this->setConnection(); |
|
90 | 16 | $this->setStack(); |
|
91 | 16 | } |
|
92 | |||
93 | /** |
||
94 | * @param string $key |
||
95 | * @param mixed $default |
||
96 | * @return mixed |
||
97 | */ |
||
98 | 16 | protected function getPresetValue($key, $default = null) |
|
99 | { |
||
100 | 16 | return $this->config->get( |
|
101 | 16 | implode('.', [$this->getConfigPath(), $this->preset, $key]), |
|
102 | $default |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | 16 | protected function getConfigPath(): string |
|
107 | { |
||
108 | 16 | return 'mail-test.presets'; |
|
109 | } |
||
110 | |||
111 | 16 | protected function getConfigKeys(): array |
|
112 | { |
||
113 | 16 | return array_keys( |
|
114 | 16 | $this->config->get($this->getConfigPath()) |
|
115 | ); |
||
116 | } |
||
117 | |||
118 | 16 | protected function setPreset(): void |
|
119 | { |
||
120 | 16 | $this->preset = $this->option('preset'); |
|
121 | 16 | } |
|
122 | |||
123 | 16 | protected function setRecipient(): void |
|
124 | { |
||
125 | 16 | $this->recipient = $this->argument('recipient') ?? |
|
126 | 11 | $this->getPresetValue('recipient', $this->config->get('mail.from.address')); |
|
127 | 16 | } |
|
128 | |||
129 | 16 | protected function setDriver(): void |
|
130 | { |
||
131 | 16 | $this->driver = $this->option('driver') ?? |
|
132 | 14 | $this->getPresetValue('driver', $this->config->get('mail.driver')); |
|
133 | 16 | } |
|
134 | |||
135 | 16 | protected function setConnection(): void |
|
136 | { |
||
137 | 16 | $this->connection = $this->option('connection') ?? |
|
138 | 13 | $this->getPresetValue('connection', $this->config->get('queue.default')); |
|
139 | 16 | } |
|
140 | |||
141 | 16 | protected function setStack(): void |
|
142 | { |
||
143 | 16 | $this->stack = $this->option('stack') ?: $this->getPresetValue('stack', 'default'); |
|
144 | 16 | } |
|
145 | |||
146 | 11 | protected function isOnQueue(): bool |
|
147 | { |
||
148 | 11 | return $this->hasQueueOptions() || $this->hasQueuePresets(); |
|
149 | } |
||
150 | |||
151 | 11 | protected function hasQueueOptions(): bool |
|
152 | { |
||
153 | 11 | return $this->option('queue') || $this->option('stack') || $this->option('connection'); |
|
154 | } |
||
155 | |||
156 | 6 | protected function hasQueuePresets(): bool |
|
157 | { |
||
158 | 6 | return $this->getPresetValue('queue') || $this->getPresetValue('connection') || $this->getPresetValue('stack'); |
|
159 | } |
||
160 | |||
161 | 16 | protected function rules(): array |
|
162 | { |
||
163 | return [ |
||
164 | 16 | 'email' => 'email', |
|
165 | 16 | 'driver' => 'required', |
|
166 | 16 | 'preset' => 'nullable|sometimes|in:'.implode(',', $this->getConfigKeys()), |
|
167 | ]; |
||
168 | } |
||
169 | } |
||
170 |