|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SfCod\EmailEngineBundle\Mailer; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use SfCod\EmailEngineBundle\Exception\RepositoryUnavailableException; |
|
7
|
|
|
use SfCod\EmailEngineBundle\Repository\RepositoryInterface; |
|
8
|
|
|
use SfCod\EmailEngineBundle\Sender\MessageOptionsInterface; |
|
9
|
|
|
use SfCod\EmailEngineBundle\Sender\SenderInterface; |
|
10
|
|
|
use SfCod\EmailEngineBundle\Template\ParametersAwareInterface; |
|
11
|
|
|
use SfCod\EmailEngineBundle\Template\Params\ParameterResolverInterface; |
|
12
|
|
|
use SfCod\EmailEngineBundle\Template\RepositoryAwareInterface; |
|
13
|
|
|
use SfCod\EmailEngineBundle\Template\TemplateInterface; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Mailer |
|
18
|
|
|
* |
|
19
|
|
|
* @author Virchenko Maksim <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @package SfCod\EmailEngineBundle\Sender |
|
22
|
|
|
*/ |
|
23
|
|
|
class Mailer |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Senders for email sending |
|
27
|
|
|
* |
|
28
|
|
|
* @var SenderInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $senders = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ContainerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $container; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* EmailSender constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param ContainerInterface $container |
|
41
|
|
|
* @param LoggerInterface $logger |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(ContainerInterface $container) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->container = $container; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get senders |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getSenders(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->senders; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set senders |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $senders |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setSenders(array $senders): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->senders = $senders; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Send email template |
|
70
|
|
|
* |
|
71
|
|
|
* @param TemplateInterface $template |
|
72
|
|
|
* @param array|string $emails |
|
73
|
|
|
* @param null|MessageOptionsInterface $options |
|
74
|
|
|
* |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function send(TemplateInterface $template, $emails, ?MessageOptionsInterface $options = null): int |
|
78
|
|
|
{ |
|
79
|
|
|
$sentCount = 0; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($this->senders as $config) { |
|
82
|
|
|
// try { |
|
83
|
|
|
$concreteTemplate = clone $template; |
|
84
|
|
|
$sender = array_merge($config['sender'], ['options' => $options]); |
|
85
|
|
|
$concreteSender = $this->makeSender($sender['class'], $sender['options'] ?? []); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
if ($concreteTemplate instanceof RepositoryAwareInterface) { |
|
88
|
|
|
$concreteTemplate->setRepository($this->makeRepository($config['repository']['class'], $concreteTemplate, $config['repository']['arguments'])); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($concreteTemplate instanceof ParametersAwareInterface) { |
|
92
|
|
|
$concreteTemplate->setParameterResolver($this->container->get(ParameterResolverInterface::class)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($concreteSender->send($concreteTemplate, $emails)) { |
|
96
|
|
|
++$sentCount; |
|
97
|
|
|
|
|
98
|
|
|
break; |
|
99
|
|
|
} |
|
100
|
|
|
// } catch (RepositoryUnavailableException $e) { |
|
101
|
|
|
// if ($this->container->get('kernel')->isDebug()) { |
|
102
|
|
|
// $this->container->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
|
103
|
|
|
// } |
|
104
|
|
|
// |
|
105
|
|
|
// // Try next sender |
|
106
|
|
|
// } |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $sentCount; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Make email engine sender |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $sender |
|
116
|
|
|
* @param array $options |
|
117
|
|
|
* |
|
118
|
|
|
* @return SenderInterface |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function makeSender(string $sender, array $options = []): SenderInterface |
|
121
|
|
|
{ |
|
122
|
|
|
/** @var SenderInterface $sender */ |
|
123
|
|
|
$sender = $this->container->get($sender); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
if (false === empty($options)) { |
|
126
|
|
|
$sender->setOptions($options); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $sender; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Make email engine repository |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $repository |
|
136
|
|
|
* @param TemplateInterface $template |
|
137
|
|
|
* @param array $arguments |
|
138
|
|
|
* |
|
139
|
|
|
* @return RepositoryInterface |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function makeRepository(string $repository, TemplateInterface $template, array $arguments = []): RepositoryInterface |
|
142
|
|
|
{ |
|
143
|
|
|
/** @var RepositoryInterface $repository */ |
|
144
|
|
|
$repository = $this->container->get($repository); |
|
|
|
|
|
|
145
|
|
|
$repository->connect($template, $arguments); |
|
146
|
|
|
|
|
147
|
|
|
return $repository; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.