/** * Attaches the preconfigured mail listeners to the mail service * * @param MailListenerAwareInterface $service * @param ServiceLocatorInterface $sm * @throws InvalidArgumentException */ protected function attachMailListeners(MailListenerAwareInterface $service, ServiceLocatorInterface $sm) { $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) && $sm->has($listener)) { $listener = $sm->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); } }
public function testSetMailListeners() { $this->assertCount(0, $this->mailOptions->getMailListeners()); $this->assertSame($this->mailOptions, $this->mailOptions->setMailListeners([1, 2, 3])); $this->assertCount(3, $this->mailOptions->getMailListeners()); }