|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Mailer; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Exceptions\ConfigException; |
|
18
|
|
|
use Quantum\Exceptions\MailerException; |
|
19
|
|
|
use Quantum\Exceptions\LangException; |
|
20
|
|
|
use Quantum\Exceptions\AppException; |
|
21
|
|
|
use Quantum\Exceptions\DiException; |
|
22
|
|
|
use Quantum\Loader\Setup; |
|
23
|
|
|
use ReflectionException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* class MailerManager |
|
27
|
|
|
* @package Quantum\Libraries\Mailer |
|
28
|
|
|
*/ |
|
29
|
|
|
class MailerManager |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Available mail adapters |
|
34
|
|
|
*/ |
|
35
|
|
|
const ADAPTERS = [ |
|
36
|
|
|
'smtp', |
|
37
|
|
|
'sendinblue', |
|
38
|
|
|
'sendgrid', |
|
39
|
|
|
'mandrill', |
|
40
|
|
|
'mailgun', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var MailerInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private static $adapter; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get Handler |
|
50
|
|
|
* @return MailerInterface |
|
51
|
|
|
* @throws ConfigException |
|
52
|
|
|
* @throws DiException |
|
53
|
|
|
* @throws MailerException |
|
54
|
|
|
* @throws ReflectionException |
|
55
|
|
|
* @throws LangException |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function getHandler(): MailerInterface |
|
58
|
|
|
{ |
|
59
|
|
|
if (self::$adapter !== null) { |
|
60
|
|
|
return self::$adapter; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!config()->has('mailer')) { |
|
64
|
|
|
config()->import(new Setup('config', 'mailer')); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$mailerAdapter = config()->get('mailer.current'); |
|
68
|
|
|
|
|
69
|
|
|
if (!in_array($mailerAdapter, self::ADAPTERS)) { |
|
70
|
|
|
throw MailerException::unsupportedAdapter($mailerAdapter); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$currentMailer = config()->get('mailer.current'); |
|
74
|
|
|
|
|
75
|
|
|
$mailerAdapterClassName = __NAMESPACE__ . '\\Adapters\\' . ucfirst($currentMailer) . 'Adapter'; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
return self::$adapter = $mailerAdapterClassName::getInstance(config()->get('mailer.' . $currentMailer)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $method |
|
82
|
|
|
* @param array|null $arguments |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
* @throws AppException |
|
85
|
|
|
*/ |
|
86
|
|
|
public function __call(string $method, ?array $arguments) |
|
87
|
|
|
{ |
|
88
|
|
|
if (!method_exists(self::$adapter, $method)) { |
|
89
|
|
|
throw AppException::methodNotSupported($method, get_class(self::$adapter)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return self::$adapter->$method(...$arguments); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|