Ejemplo n.º 1
0
 /**
  * @param string $name
  *
  * @return \Swift_Mailer
  */
 private static function mailer(string $name = null) : \Swift_Mailer
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $config = DI::config()->getIfExists('email/' . ($name ?: 'default'));
     if (!$config) {
         $transport = \Swift_MailTransport::newInstance();
         $return = \Swift_Mailer::newInstance($transport);
     } elseif (is_callable($config)) {
         $return = $config();
     } else {
         $uri = new Uri($config);
         switch ($uri->getScheme()) {
             case 'smtp':
                 $transport = \Swift_SmtpTransport::newInstance($uri->getHost(), $uri->getPort());
                 if ($uri->getUser()) {
                     $transport->setUsername($uri->getUser());
                 }
                 if ($uri->getPassword()) {
                     $transport->setPassword($uri->getPassword());
                 }
                 if ($uri->getQuery('auth')) {
                     $transport->setAuthMode($uri->getQuery('auth'));
                 }
                 if ($uri->getQuery('encryption')) {
                     $transport->setEncryption($uri->getQuery('encryption'));
                 }
                 break;
             case 'echo':
                 $transport = EchoTransport::newInstance();
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf("Undefined email mailer type '%s'", $uri->getScheme()));
                 break;
         }
         $return = \Swift_Mailer::newInstance($transport);
         if ($uri->getQuery('plugins')) {
             foreach ($uri->getQuery('plugins') as $plugin) {
                 $return->registerPlugin(new $plugin());
             }
         }
     }
     return DI::set(__METHOD__, $name, $return);
 }
Ejemplo n.º 2
0
 /**
  * Test the pass extract
  *
  * @param string $uriString
  * @param array $parts
  * @dataProvider validUriStringProviderWithPart
  */
 public function testPass(string $uriString, array $parts)
 {
     $uri = new Uri($uriString);
     if (isset($parts['pass'])) {
         $this->assertEquals($parts['pass'], $uri->getPassword());
     } else {
         $this->assertNull($uri->getPassword());
     }
 }