protected function attachMailListeners(MailListenerAwareInterface $service, ContainerInterface $container)
{
$listeners = $this->mailOptions->getMailListeners();
foreach ($listeners as $listener) {
// Try to fetch the listener from the ServiceManager or lazily create an instance
if (is_string($listener) && $container->has($listener)) {
$listener = $container->get($listener);
} elseif (is_string($listener) && class_exists($listener)) {
$listener = new $listener();
}
// At this point, the listener should be an instance of MailListenerInterface, otherwise it is invalid
if (!$listener instanceof MailListenerInterface) {
throw new InvalidArgumentException(sprintf('Provided listener of type "%s" is not valid. ' . 'Expected "string" or "AcMailer\\Listener\\MailListenerInterface"', is_object($listener) ? get_class($listener) : gettype($listener)));
}
$service->attachMailListener($listener);
}
}