1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** MailServiceFactory.php */ |
11
|
|
|
namespace Core\Mail; |
12
|
|
|
|
13
|
|
|
use Core\Options\MailServiceOptions; |
14
|
|
|
use Interop\Container\ContainerInterface; |
15
|
|
|
use Zend\Mail\Transport\FileOptions; |
16
|
|
|
use Zend\Mail\Transport\Sendmail; |
17
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
18
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
19
|
|
|
use Zend\Mail\Transport\Smtp; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class MailServiceFactory |
23
|
|
|
* |
24
|
|
|
* @author Anthonius Munthi <[email protected]> |
25
|
|
|
* @package Core\Mail |
26
|
|
|
*/ |
27
|
|
|
class MailServiceFactory implements FactoryInterface |
28
|
|
|
{ |
29
|
|
|
public function __invoke( ContainerInterface $container, $requestedName, array $options = null ) |
30
|
|
|
{ |
31
|
|
|
$config = $container->get('Config'); |
32
|
|
|
$mails = isset($config['mails']) ? $config['mails'] : []; |
33
|
|
|
|
34
|
|
|
/* @var \Auth\Options\ModuleOptions $authOptions */ |
35
|
|
|
$authOptions = $container->get('Auth/Options'); |
36
|
|
|
|
37
|
|
|
/* @var \Core\Options\MailServiceOptions $mailServiceOptions */ |
38
|
|
|
$mailServiceOptions = $container->get('Core/MailServiceOptions'); |
39
|
|
|
|
40
|
|
|
$configArray = [ |
41
|
|
|
'from' => [ |
42
|
|
|
'name' => $authOptions->getFromName(), |
43
|
|
|
'email' => $authOptions->getFromEmail() |
44
|
|
|
], |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$configArray['transport'] = $this->getTransport($mailServiceOptions); |
48
|
|
|
$configArray = array_merge($configArray, $mails); |
49
|
|
|
|
50
|
|
|
$config = new MailServiceConfig($configArray); |
51
|
|
|
$service = new MailService($container,$config->toArray()); |
52
|
|
|
$config->configureServiceManager($service); |
53
|
|
|
foreach($config->toArray() as $name=>$value){ |
54
|
|
|
$method = 'set'.$name; |
55
|
|
|
if(method_exists($service,$method)){ |
56
|
|
|
call_user_func([$service,$method],$value); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $service; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getTransport(MailServiceOptions $mailServiceOptions) |
64
|
|
|
{ |
65
|
|
|
$type = $mailServiceOptions->getTransportClass(); |
66
|
|
|
if (MailService::TRANSPORT_SMTP == $type) { |
67
|
|
|
return new Smtp($mailServiceOptions); |
68
|
|
|
}elseif(MailService::TRANSPORT_FILE == $type){ |
69
|
|
|
$fileOptions = new FileOptions(); |
70
|
|
|
$fileOptions->setPath($mailServiceOptions->getPath()); |
71
|
|
|
return new FileTransport($fileOptions); |
72
|
|
|
}elseif(MailService::TRANSPORT_SENDMAIL == $type){ |
73
|
|
|
return new Sendmail(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new ServiceNotCreatedException( |
77
|
|
|
sprintf( |
78
|
|
|
'"%s" is not a valid email transport type. Please use smtp or file as email transport', |
79
|
|
|
$type |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|