@@ -295,34 +295,46 @@ made. To do that, you create a new class::
295
295
namespace App\Updates;
296
296
297
297
use App\Service\MessageGenerator;
298
+ use Psr\Log\LoggerInterface;
299
+ use Symfony\Component\Mailer\MailerInterface;
300
+ use Symfony\Component\Mime\Email;
298
301
299
302
class SiteUpdateManager
300
303
{
301
304
private $messageGenerator;
302
305
private $mailer;
306
+ private $logger;
303
307
304
- public function __construct(MessageGenerator $messageGenerator, \Swift_Mailer $mailer)
308
+ public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer, LoggerInterface $logger )
305
309
{
306
310
$this->messageGenerator = $messageGenerator;
307
311
$this->mailer = $mailer;
312
+ $this-logger = $logger;
308
313
}
309
314
310
- public function notifyOfSiteUpdate()
315
+ public function notifyOfSiteUpdate(): bool
311
316
{
312
317
$happyMessage = $this->messageGenerator->getHappyMessage();
313
318
314
- $message = (new \Swift_Message('Site update just happened!'))
315
- ->setFrom('admin@example.com' )
316
- ->setTo('manager @example.com')
317
- ->addPart(
318
- 'Someone just updated the site. We told them: '.$happyMessage
319
- );
319
+ try {
320
+ $email = (new Email() )
321
+ ->from('admin @example.com')
322
+ ->to('manager@example.com')
323
+ ->subject('Site update just happened!')
324
+ ->text('Someone just updated the site. We told them: '.$happyMessage );
320
325
321
- return $this->mailer->send($message) > 0;
326
+ $this->mailer->send($email);
327
+
328
+ return true;
329
+ } catch (\Exception $exception) {
330
+ $this->logger->error($exception->getMessage());
331
+
332
+ return false;
333
+ }
322
334
}
323
335
}
324
336
325
- This needs the ``MessageGenerator `` *and * the ``Swift_Mailer `` service . That's no
337
+ This needs the ``MessageGenerator `` *and * both ``Mailer `` and `` Logger `` services . That's no
326
338
problem! In fact, this new service is ready to be used. In a controller, for example,
327
339
you can type-hint the new ``SiteUpdateManager `` class and use it::
328
340
0 commit comments