AcMailer\Service\Factory\MailServiceAbstractFactory::__invoke PHP Method

__invoke() public method

Create an object
public __invoke ( Interop\Container\ContainerInterface $container, string $requestedName, array $options = null ) : object
$container Interop\Container\ContainerInterface
$requestedName string
$options array
return object
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $specificServiceName = explode('.', $requestedName)[2];
        $this->mailOptions = $container->get(sprintf('%s.%s.%s', self::ACMAILER_PART, MailOptionsAbstractFactory::SPECIFIC_PART, $specificServiceName));
        // Create the service
        $message = $this->createMessage();
        $transport = $this->createTransport($container);
        $renderer = $this->createRenderer($container);
        $mailService = new MailService($message, $transport, $renderer);
        // Set subject
        $mailService->setSubject($this->mailOptions->getMessageOptions()->getSubject());
        // Set body, either by using a template or a raw body
        $body = $this->mailOptions->getMessageOptions()->getBody();
        if ($body->getUseTemplate()) {
            $defaultLayoutConfig = $body->getTemplate()->getDefaultLayout();
            if (isset($defaultLayoutConfig['path'])) {
                $params = isset($defaultLayoutConfig['params']) ? $defaultLayoutConfig['params'] : [];
                $captureTo = isset($defaultLayoutConfig['template_capture_to']) ? $defaultLayoutConfig['template_capture_to'] : 'content';
                $mailService->setDefaultLayout(new DefaultLayout($defaultLayoutConfig['path'], $params, $captureTo));
            }
            $mailService->setTemplate($body->getTemplate()->toViewModel(), ['charset' => $body->getCharset()]);
        } else {
            $mailService->setBody($body->getContent(), $body->getCharset());
        }
        // Attach files
        $files = $this->mailOptions->getMessageOptions()->getAttachments()->getFiles();
        $mailService->addAttachments($files);
        // Attach files from dir
        $dir = $this->mailOptions->getMessageOptions()->getAttachments()->getDir();
        if ($dir['iterate'] === true && is_string($dir['path']) && is_dir($dir['path'])) {
            $files = $dir['recursive'] === true ? new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir['path'], \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) : new \DirectoryIterator($dir['path']);
            /* @var \SplFileInfo $fileInfo */
            foreach ($files as $fileInfo) {
                if ($fileInfo->isDir()) {
                    continue;
                }
                $mailService->addAttachment($fileInfo->getPathname());
            }
        }
        // Attach mail listeners
        $this->attachMailListeners($mailService, $container);
        return $mailService;
    }

Usage Example

 /**
  * @expectedException \AcMailer\Exception\InvalidArgumentException
  */
 public function testInvalidListenersThrowException()
 {
     $options = ['mail_listeners' => [new \stdClass(), 'invalid_service', '\\Nonsens\\Foo']];
     $this->initServiceLocator($options);
     $this->mailServiceFactory->__invoke($this->serviceLocator, 'acmailer.mailservice.default');
 }