1 | <?php |
||
21 | class Mailer |
||
22 | { |
||
23 | /** |
||
24 | * Senders for email sending |
||
25 | * |
||
26 | * @var SenderInterface[] |
||
27 | */ |
||
28 | private $senders = []; |
||
29 | |||
30 | /** |
||
31 | * @var ContainerInterface |
||
32 | */ |
||
33 | private $container; |
||
34 | |||
35 | /** |
||
36 | * EmailSender constructor. |
||
37 | * |
||
38 | * @param ContainerInterface $container |
||
39 | */ |
||
40 | public function __construct(ContainerInterface $container) |
||
41 | { |
||
42 | $this->container = $container; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Get senders |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | public function getSenders(): array |
||
51 | { |
||
52 | return $this->senders; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Set senders |
||
57 | * |
||
58 | * @param array $senders |
||
59 | */ |
||
60 | public function setSenders(array $senders): void |
||
61 | { |
||
62 | $this->senders = $senders; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Send email template |
||
67 | * |
||
68 | * @param TemplateInterface $template |
||
69 | * @param array|string $emails |
||
70 | * @param null|MessageOptionsInterface $options |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | public function send(TemplateInterface $template, $emails, ?MessageOptionsInterface $options = null): int |
||
75 | { |
||
76 | $sentCount = 0; |
||
77 | |||
78 | foreach ($this->senders as $config) { |
||
79 | try { |
||
80 | $concreteTemplate = clone $template; |
||
81 | $concreteSender = $this->makeSender(array_merge($config['sender'], ['options' => $options])); |
||
82 | |||
83 | if ($concreteTemplate instanceof ContainerAwareInterface) { |
||
84 | $concreteTemplate->setContainer($this->container); |
||
85 | } |
||
86 | |||
87 | if ($concreteTemplate instanceof RepositoryAwareInterface) { |
||
88 | $concreteTemplate->setRepository($this->makeRepository($config['repository'], $concreteTemplate)); |
||
89 | } |
||
90 | |||
91 | if ($concreteSender->send($concreteTemplate, $emails)) { |
||
92 | ++$sentCount; |
||
93 | |||
94 | break; |
||
95 | } |
||
96 | } catch (RepositoryUnavailableException $e) { |
||
97 | if ($this->container->get('kernel')->isDebug()) { |
||
98 | $this->container->get('logger')->error($e); |
||
99 | } |
||
100 | |||
101 | // Try next sender |
||
102 | } |
||
103 | } |
||
104 | |||
105 | return $sentCount; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Make email engine sender |
||
110 | * |
||
111 | * @param array $config |
||
112 | * |
||
113 | * @return SenderInterface |
||
114 | */ |
||
115 | protected function makeSender(array $config): SenderInterface |
||
116 | { |
||
117 | /** @var SenderInterface $sender */ |
||
118 | $sender = new $config['class'](); |
||
119 | |||
120 | if ($config['options']) { |
||
121 | $sender->setOptions($config['options']); |
||
122 | } |
||
123 | |||
124 | if ($sender instanceof ContainerAwareInterface) { |
||
125 | $sender->setContainer($this->container); |
||
126 | } |
||
127 | |||
128 | return $sender; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Make email engine repository |
||
133 | * |
||
134 | * @param array $config |
||
135 | * @param TemplateInterface $template |
||
136 | * |
||
137 | * @return RepositoryInterface |
||
138 | */ |
||
139 | protected function makeRepository(array $config, TemplateInterface $template): RepositoryInterface |
||
146 | } |
||
147 |