Skip to content

Commit 20e9e16

Browse files
ker0xRomain Monteil
authored and
Romain Monteil
committed
Update multiple services example
This PR propose to replace SwiftMailer with the Mailer component and to add the Logger service for a better understanding how to inject multiple services
1 parent f64cd98 commit 20e9e16

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

service_container.rst

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,15 @@ made. To do that, you create a new class::
295295
namespace App\Updates;
296296

297297
use App\Service\MessageGenerator;
298+
use Symfony\Component\Mailer\MailerInterface;
299+
use Symfony\Component\Mime\Email;
298300

299301
class SiteUpdateManager
300302
{
301303
private $messageGenerator;
302304
private $mailer;
303305

304-
public function __construct(MessageGenerator $messageGenerator, \Swift_Mailer $mailer)
306+
public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer)
305307
{
306308
$this->messageGenerator = $messageGenerator;
307309
$this->mailer = $mailer;
@@ -311,18 +313,19 @@ made. To do that, you create a new class::
311313
{
312314
$happyMessage = $this->messageGenerator->getHappyMessage();
313315

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-
);
316+
$email = (new Email())
317+
->from('admin@example.com')
318+
->to('manager@example.com')
319+
->subject('Site update just happened!')
320+
->text('Someone just updated the site. We told them: '.$happyMessage);
320321

321-
return $this->mailer->send($message) > 0;
322+
$this->mailer->send($email);
323+
324+
// ...
322325
}
323326
}
324327

325-
This needs the ``MessageGenerator`` *and* the ``Swift_Mailer`` service. That's no
328+
This needs the ``MessageGenerator`` *and* the ``Mailer`` service. That's no
326329
problem! In fact, this new service is ready to be used. In a controller, for example,
327330
you can type-hint the new ``SiteUpdateManager`` class and use it::
328331

0 commit comments

Comments
 (0)