These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * @copyright Copyright (c) 2015 ublaboo <[email protected]> |
||
7 | * @author Pavel Janda <[email protected]> |
||
8 | * @package Ublaboo |
||
9 | */ |
||
10 | |||
11 | namespace Ublaboo\Mailing; |
||
12 | |||
13 | use Latte; |
||
14 | use Nette; |
||
15 | use Ublaboo; |
||
16 | use Ublaboo\Mailing\Exception\MailingException; |
||
17 | |||
18 | abstract class Mail |
||
19 | { |
||
20 | use Nette\SmartObject; |
||
21 | |||
22 | public const CONFIG_LOG = 'log'; |
||
23 | public const CONFIG_SEND = 'send'; |
||
24 | public const CONFIG_BOTH = 'both'; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $mails; |
||
30 | |||
31 | /** |
||
32 | * @var Nette\Mail\IMailer |
||
33 | */ |
||
34 | protected $mailer; |
||
35 | |||
36 | /** |
||
37 | * @var Nette\Mail\Message |
||
38 | */ |
||
39 | protected $message; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $args; |
||
45 | |||
46 | /** |
||
47 | * @var Nette\Application\LinkGenerator |
||
48 | */ |
||
49 | protected $linkGenerator; |
||
50 | |||
51 | /** |
||
52 | * @var ILogger |
||
53 | */ |
||
54 | protected $logger; |
||
55 | |||
56 | /** |
||
57 | * @var Nette\Application\UI\ITemplate |
||
58 | */ |
||
59 | protected $template; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $underscore_name; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $log_type; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $mail_images_base_path; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $template_file; |
||
80 | |||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | private $config; |
||
86 | |||
87 | |||
88 | public function __construct( |
||
89 | $config, |
||
90 | $mails, |
||
91 | Nette\Mail\IMailer $mailer, |
||
92 | Nette\Mail\Message $message, |
||
93 | Nette\Application\LinkGenerator $linkGenerator, |
||
94 | Nette\Application\UI\ITemplateFactory $templateFactory, |
||
95 | ILogger $logger, |
||
96 | $args |
||
97 | ) { |
||
98 | $this->config = $config; |
||
99 | $this->mails = $mails; |
||
100 | $this->mailer = $mailer; |
||
101 | $this->message = $message; |
||
102 | $this->linkGenerator = $linkGenerator; |
||
103 | $this->logger = $logger; |
||
104 | $this->args = $args; |
||
105 | |||
106 | $this->template = $templateFactory->createTemplate(); |
||
107 | |||
108 | /** |
||
109 | * Initiate mail composing |
||
110 | */ |
||
111 | if ($this instanceof IComposableMail) { |
||
112 | $this->compose($this->message, $this->args); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | |||
117 | /** |
||
118 | * Set template file |
||
119 | * @return void |
||
120 | */ |
||
121 | public function setTemplateFile($template_file) |
||
122 | { |
||
123 | $this->template_file = (string) $template_file; |
||
124 | } |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Set template variables |
||
129 | * @return void |
||
130 | */ |
||
131 | protected function setTemplateVariables() |
||
132 | { |
||
133 | foreach ($this->args as $key => $value) { |
||
134 | $this->template->$key = $value; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Set absolute base path for images |
||
141 | * @param string $mail_images_base_path |
||
142 | * @return self |
||
143 | */ |
||
144 | public function setBasePath($mail_images_base_path) |
||
145 | { |
||
146 | $this->mail_images_base_path = (string) $mail_images_base_path; |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | |||
152 | /** |
||
153 | * Stick to convention that Email: |
||
154 | * ?/mailing/Mails/FooMail.php |
||
155 | * |
||
156 | * will have template with path of: |
||
157 | * ?/mailing/Mails/templates/foo_mail.latte |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getTemplateFile() |
||
162 | { |
||
163 | /** |
||
164 | * Get child class file path |
||
165 | * @var \ReflectionClass |
||
166 | */ |
||
167 | $reflection = new \ReflectionClass(get_class($this)); |
||
168 | |||
169 | /** |
||
170 | * Split path to directory and file |
||
171 | */ |
||
172 | $class_path = $reflection->getFileName(); |
||
173 | $class_dir = dirname($class_path); |
||
174 | $class_name = pathinfo($class_path, PATHINFO_FILENAME); |
||
175 | |||
176 | /** |
||
177 | * Convert class name to underscore and set latte file extension |
||
178 | */ |
||
179 | $this->underscore_name = lcfirst(preg_replace_callback('/(?<=.)([A-Z])/', function ($m) { |
||
180 | return '_' . strtolower($m[1]); |
||
181 | }, $class_name)); |
||
182 | |||
183 | $template_name = $this->underscore_name . '.latte'; |
||
184 | $this->log_type = $this->underscore_name; |
||
185 | |||
186 | if ($this->template_file) { |
||
187 | return $this->template_file; |
||
188 | } |
||
189 | |||
190 | $template_file = "$class_dir/templates/$template_name"; |
||
191 | |||
192 | if (!file_exists($template_file)) { |
||
193 | throw new MailingException("Error creating template from file [$template_file]", 1); |
||
194 | } |
||
195 | |||
196 | return $template_file; |
||
197 | } |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Render latte template to string and send (and/or log) mail |
||
202 | * @return void |
||
203 | */ |
||
204 | public function send() |
||
205 | { |
||
206 | /** |
||
207 | * Set template variables |
||
208 | */ |
||
209 | $this->setTemplateVariables(); |
||
210 | |||
211 | /** |
||
212 | * Set body/html body |
||
213 | */ |
||
214 | try { |
||
215 | $this->template->setFile($this->getTemplateFile()); |
||
216 | |||
217 | if (version_compare(Latte\Engine::VERSION, '2.4.0', '>=')) { |
||
218 | $this->template->getLatte()->addProvider('uiControl', $this->linkGenerator); |
||
219 | } else { |
||
220 | $this->template->_control = $this->linkGenerator; |
||
0 ignored issues
–
show
|
|||
221 | } |
||
222 | |||
223 | $this->message->setHtmlBody((string) $this->template, $this->mail_images_base_path); |
||
224 | } catch (MailingException $e) { |
||
225 | /** |
||
226 | * If mail template was set and not found, bubble exception up |
||
227 | */ |
||
228 | if ($this->template_file) { |
||
229 | throw $e; |
||
230 | } |
||
231 | /** |
||
232 | * Otherwise just suppose that user has set message body via ::setBody |
||
233 | */ |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * In case mail sending in on, send message |
||
238 | */ |
||
239 | if ($this->config === self::CONFIG_BOTH || $this->config === self::CONFIG_SEND) { |
||
240 | $this->mailer->send($this->message); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * In case mail logging is turned on, log message |
||
245 | */ |
||
246 | if ($this->config === self::CONFIG_LOG || $this->config === self::CONFIG_BOTH) { |
||
247 | $this->logger->log($this->log_type, $this->message); |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: