Completed
Push — master ( 735e98...9e3fb1 )
by Pavel
02:30
created

src/Mail.php (1 issue)

assigning incompatible types to properties.

Bug Documentation Major

Upgrade to new PHP Analysis Engine

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
14
abstract class Mail extends Nette\Object
15
{
16
17
	const CONFIG_LOG  = 'log';
18
	const CONFIG_SEND = 'send';
19
	const CONFIG_BOTH = 'both';
20
21
22
	/**
23
	 * @var string
24
	 */
25
	private $config;
26
27
	/**
28
	 * @var array
29
	 */
30
	protected $mails;
31
32
	/**
33
	 * @var Nette\Mail\IMailer
34
	 */
35
	protected $mailer;
36
37
	/**
38
	 * @var Nette\Mail\Message
39
	 */
40
	protected $message;
41
42
	/**
43
	 * @var array
44
	 */
45
	protected $args;
46
47
	/**
48
	 * @var Nette\Application\LinkGenerator
49
	 */
50
	protected $linkGenerator;
51
52
	/**
53
	 * @var ILogger
54
	 */
55
	protected $logger;
56
57
	/**
58
	 * @var Nette\Application\UI\ITemplate
59
	 */
60
	protected $template;
61
62
	/**
63
	 * @var stirng
64
	 */
65
	protected $underscore_name;
66
67
	/**
68
	 * @var string
69
	 */
70
	protected $log_type;
71
72
	/**
73
	 * @var string
74
	 */
75
	protected $mail_images_base_path;
76
77
	/**
78
	 * @var string
79
	 */
80
	protected $template_file;
81
82
83
	public function __construct(
84
		$config,
85
		$mails,
86
		Nette\Mail\IMailer $mailer,
87
		Nette\Mail\Message $message,
88
		Nette\Application\LinkGenerator $linkGenerator,
89
		Nette\Application\UI\ITemplateFactory $templateFactory,
90
		ILogger $logger,
91
		$args
92
	) {
93
		$this->config = $config;
94
		$this->mails = $mails;
95
		$this->mailer = $mailer;
96
		$this->message = $message;
97
		$this->linkGenerator = $linkGenerator;
98
		$this->logger = $logger;
99
		$this->args = $args;
100
101
		$this->template = $templateFactory->createTemplate();
102
103
		/**
104
		 * Initiate mail composing
105
		 */
106
		$this->compose($this->message, $this->args);
107
	}
108
109
110
	/**
111
	 * Set template file
112
	 * @return void
113
	 */
114
	public function setTemplateFile($template_file)
115
	{
116
		$this->template_file = (string) $template_file;
117
	}
118
119
120
	/**
121
	 * Set template variables
122
	 * @return void
123
	 */
124
	protected function setTemplateVariables()
125
	{
126
		foreach ($this->args as $key => $value) {
127
			$this->template->$key = $value;
128
		}
129
	}
130
131
132
	/**
133
	 * Set absolute base path for images
134
	 * @param string $mail_images_base_path
135
	 */
136
	public function setBasePath($mail_images_base_path)
137
	{
138
		$this->mail_images_base_path = (string) $mail_images_base_path;
139
140
		return $this;
141
	}
142
143
144
	/**
145
	 * Stick to convention that Email:
146
	 * 		?/mailing/Mails/FooMail.php	
147
	 * 
148
	 * will have template with path of:
149
	 * 		?/mailing/Mails/templates/foo_mail.latte
150
	 * 
151
	 * @return string
152
	 */
153
	public function getTemplateFile()
154
	{
155
		if ($this->template_file) {
156
			return $this->template_file;
157
		}
158
159
		/**
160
		 * Get child class file path
161
		 * @var \ReflectionClass
162
		 */
163
		$reflection = new \ReflectionClass(get_class($this));
164
		
165
		/**
166
		 * Split path to directory and file
167
		 */
168
		$class_path = $reflection->getFileName();
169
		$class_dir = dirname($class_path);
170
		$class_name = pathinfo($class_path, PATHINFO_FILENAME);
171
172
		/**
173
		 * Convert class name to underscore and set latte file extension
174
		 */
175
		$this->underscore_name = lcfirst(preg_replace_callback('/(?<=.)([A-Z])/', function($m) {
0 ignored issues
show
Documentation Bug introduced by Pavel Janda
It seems like lcfirst(preg_replace_cal...m[1]); }, $class_name)) of type string is incompatible with the declared type object<Ublaboo\Mailing\stirng> of property $underscore_name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
176
			return '_' . strtolower($m[1]);
177
		}, $class_name));
178
179
		$template_name = $this->underscore_name . '.latte';
180
		$this->log_type = $this->underscore_name;
181
182
		$template_file = "$class_dir/templates/$template_name";
183
184
		if (!file_exists($template_file)) {
185
			throw new MailException("Error creating template from file [$template_file]", 1);
186
		}
187
188
		return $template_file;
189
	}
190
191
192
	/**
193
	 * Render latte template to string and send (and/or log) mail
194
	 * @return void
195
	 */
196
	public function send()
197
	{
198
		/**
199
		 * Set template variables
200
		 */
201
		$this->setTemplateVariables();
202
203
		/**
204
		 * Set body/html body
205
		 */
206
		try {
207
			$this->template->setFile($this->getTemplateFile());
208
			$this->message->setHtmlBody((string) $this->template, $this->mail_images_base_path);
209
		} catch (MailException $e) {
210
			/**
211
			 * If mail template was set and not found, bubble exception up
212
			 */
213
			if ($this->template_file) {
214
				throw $e;
215
			}
216
			/**
217
			 * Otherwise just suppose that user has set message body via ::setBody
218
			 */
219
		}
220
221
		/**
222
		 * In case mail sending in on, send message
223
		 */
224
		if ($this->config === self::CONFIG_BOTH || $this->config === self::CONFIG_SEND) {
225
			$this->mailer->send($this->message);
226
		}
227
228
		/**
229
		 * In case mail logging is turned on, log message
230
		 */
231
		if ($this->config === self::CONFIG_LOG || $this->config === self::CONFIG_BOTH) {
232
			$this->logger->log($this->log_type, $this->message);
233
		}
234
	}
235
236
}
237
238
239
class MailException extends \Exception
240
{
241
}
242