I was wondering how to write a service that is executed only on kernel.terminate
.
For instance when I call:
$message = (new \Swift_Message('A new book has been added'))
->setFrom('system@example.com')
->setTo('contact@les-tilleuls.coop')
->setBody(sprintf('The book #%d has been added.', $book->getId()));
$this->mailer->send($message);
I know that the kernel will send a response to the client and only AFTER that will send the email.
I have a lot of service that could be called on kernel.terminate
but I don't know how to write them in order to be called only on that event.
For now, I am writing write code in a subscriber:
public static function getSubscribedEvents()
{
return [
KernelEvents::TERMINATE => [['doHeavyStuff', EventPriorities::POST_RESPOND]]
];
}
But working this way mean that I have to deal only with the Request and the Response and I do not want to depend on the Response.
if($method===Request::METHOD_PUT && isset($response['@type']) && $response['@type']==='Partner'){
$this->notificationManager->sendNotificationAboutCart($response['partner']['id'],
$response['partner']['name']);
}
I do not know if it is clear, but it would be great to call the notificationManager
wherever I want in my code and that manager works only on kernel.terminate
.