11<?php namespace Illuminate \Mail ;
22
33use Closure ;
4+ use Illuminate \Queue \Jobs \Job ;
45use Illuminate \Support \Str ;
56use Illuminate \Log \Writer ;
67use Illuminate \View \Factory ;
1314use Symfony \Component \Mime \Address ;
1415use Symfony \Component \Mime \Email ;
1516
17+ /**
18+ * Tested At:
19+ * {@see MailMailerTest}
20+ */
1621class Mailer {
1722
1823 /**
@@ -164,10 +169,10 @@ public function queueOn(string $queue, array|string $view, array $data, string|C
164169 * @param string|array $view
165170 * @param array $data
166171 * @param \Closure|string $callback
167- * @param string $queue
172+ * @param ? string $queue
168173 * @return mixed
169174 */
170- public function later (int $ delay , $ view , array $ data , $ callback , $ queue = null ): mixed
175+ public function later (int $ delay , string | array $ view , array $ data , Closure | string $ callback , ? string $ queue = null ): mixed
171176 {
172177 $ callback = $ this ->buildQueueCallable ($ callback );
173178
@@ -184,7 +189,7 @@ public function later(int $delay, $view, array $data, $callback, $queue = null):
184189 * @param \Closure|string $callback
185190 * @return mixed
186191 */
187- public function laterOn ($ queue , $ delay , $ view , array $ data , $ callback ): mixed
192+ public function laterOn (string $ queue , int $ delay , string | array $ view , array $ data , Closure | string $ callback ): mixed
188193 {
189194 return $ this ->later ($ delay , $ view , $ data , $ callback , $ queue );
190195 }
@@ -195,9 +200,11 @@ public function laterOn($queue, $delay, $view, array $data, $callback): mixed
195200 * @param mixed $callback
196201 * @return mixed
197202 */
198- protected function buildQueueCallable ($ callback ): mixed
203+ protected function buildQueueCallable (mixed $ callback ): mixed
199204 {
200- if ( ! $ callback instanceof Closure) return $ callback ;
205+ if (!$ callback instanceof Closure) {
206+ return $ callback ;
207+ }
201208
202209 return serialize (new SerializableClosure ($ callback ));
203210 }
@@ -209,10 +216,9 @@ protected function buildQueueCallable($callback): mixed
209216 * @param array $data
210217 * @return void
211218 */
212- public function handleQueuedMessage ($ job , $ data ): void
219+ public function handleQueuedMessage (Job $ job , array $ data ): void
213220 {
214221 $ this ->send ($ data ['view ' ], $ data ['data ' ], $ this ->getQueuedCallable ($ data ));
215-
216222 $ job ->delete ();
217223 }
218224
@@ -241,19 +247,24 @@ protected function getQueuedCallable(array $data): mixed
241247 * @param array $data
242248 * @return void
243249 */
244- protected function addContent ($ message , $ view , $ plain , $ data ): void
250+ protected function addContent (Message $ message , ? string $ view , ? string $ plain , array $ data ): void
245251 {
246- if (isset ( $ view) )
252+ if ($ view !== null )
247253 {
248- $ message ->setBody ($ this ->getView ($ view , $ data ), ' text/html ' );
254+ $ message ->html ($ this ->renderView ($ view , $ data ));
249255 }
250256
251- if (isset ( $ plain) )
257+ if ($ plain !== null )
252258 {
253- $ message ->addPart ($ this ->getView ($ plain , $ data ), ' text/plain ' );
259+ $ message ->text ($ this ->renderView ($ plain , $ data ));
254260 }
255261 }
256262
263+ protected function renderView (string $ view , array $ data ): string
264+ {
265+ return $ this ->views ->make ($ view , $ data )->render ();
266+ }
267+
257268 /**
258269 * Parse the given view name or array.
259270 *
@@ -262,26 +273,29 @@ protected function addContent($message, $view, $plain, $data): void
262273 *
263274 * @throws \InvalidArgumentException
264275 */
265- protected function parseView ($ view ): array
276+ protected function parseView (string | array $ view ): array
266277 {
267- if (is_string ($ view )) return array ($ view , null );
278+ if (is_string ($ view )) {
279+ return [$ view , null ];
280+ }
268281
269282 // If the given view is an array with numeric keys, we will just assume that
270283 // both a "pretty" and "plain" view were provided, so we will return this
271284 // array as is, since must should contain both views with numeric keys.
272285 if (is_array ($ view ) && isset ($ view [0 ]))
273286 {
274- return $ view ;
287+ return [ $ view[ 0 ], $ view [ 1 ] ?? null ] ;
275288 }
276289
277290 // If the view is an array, but doesn't contain numeric keys, we will assume
278291 // the the views are being explicitly specified and will extract them via
279292 // named keys instead, allowing the developers to use one or the other.
280293 elseif (is_array ($ view ))
281294 {
282- return array (
283- array_get ($ view , 'html ' ), array_get ($ view , 'text ' )
284- );
295+ return [
296+ $ view ['html ' ] ?? null ,
297+ $ view ['text ' ] ?? null
298+ ];
285299 }
286300
287301 throw new \InvalidArgumentException ("Invalid view. " );
@@ -323,21 +337,17 @@ protected function logMessage(Email $message): void
323337 * @param string|\Closure $callback
324338 * @param \Illuminate\Mail\Message $message
325339 * @return mixed
326- *
327- * @throws \InvalidArgumentException
328340 */
329341 protected function callMessageBuilder (string |Closure $ callback , Message $ message ): mixed
330342 {
331343 if ($ callback instanceof Closure)
332344 {
333345 return call_user_func ($ callback , $ message );
334346 }
335- elseif ( is_string ( $ callback ))
347+ else
336348 {
337349 return $ this ->container [$ callback ]->mail ($ message );
338350 }
339-
340- throw new \InvalidArgumentException ("Callback is not valid. " );
341351 }
342352
343353 /**
@@ -360,25 +370,13 @@ protected function createMessage(): Message
360370 return $ message ;
361371 }
362372
363- /**
364- * Render the given view.
365- *
366- * @param string $view
367- * @param array $data
368- * @return \Illuminate\View\View
369- */
370- protected function getView ($ view , $ data )
371- {
372- return $ this ->views ->make ($ view , $ data )->render ();
373- }
374-
375373 /**
376374 * Tell the mailer to not really send messages.
377375 *
378376 * @param bool $value
379377 * @return void
380378 */
381- public function pretend ($ value = true ): void
379+ public function pretend (bool $ value = true ): void
382380 {
383381 $ this ->pretending = $ value ;
384382 }
0 commit comments