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