GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 735e98...9e3fb1 )
by Pavel
02:30
created

Mail::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 2
b 0
f 0
cc 1
eloc 18
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
15
abstract class Mail extends Nette\Object
16
{
17
18
	const CONFIG_LOG  = 'log';
19
	const CONFIG_SEND = 'send';
20
	const CONFIG_BOTH = 'both';
21
22
23
	/**
24
	 * @var string
25
	 */
26
	private $config;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $mails;
32
33
	/**
34
	 * @var Nette\Mail\IMailer
35
	 */
36
	protected $mailer;
37
38
	/**
39
	 * @var Nette\Mail\Message
40
	 */
41
	protected $message;
42
43
	/**
44
	 * @var array
45
	 */
46
	protected $args;
47
48
	/**
49
	 * @var Nette\Application\LinkGenerator
50
	 */
51
	protected $linkGenerator;
52
53
	/**
54
	 * @var ILogger
55
	 */
56
	protected $logger;
57
58
	/**
59
	 * @var Nette\Application\UI\ITemplate
60
	 */
61
	protected $template;
62
63
	/**
64
	 * @var string
65
	 */
66
	protected $underscore_name;
67
68
	/**
69
	 * @var string
70
	 */
71
	protected $log_type;
72
73
	/**
74
	 * @var string
75
	 */
76
	protected $mail_images_base_path;
77
78
	/**
79
	 * @var string
80
	 */
81
	protected $template_file;
82
83
84
	public function __construct(
85
		$config,
86
		$mails,
87
		Nette\Mail\IMailer $mailer,
88
		Nette\Mail\Message $message,
89
		Nette\Application\LinkGenerator $linkGenerator,
90
		Nette\Application\UI\ITemplateFactory $templateFactory,
91
		ILogger $logger,
92
		$args
93
	) {
94
		$this->config = $config;
95
		$this->mails = $mails;
96
		$this->mailer = $mailer;
97
		$this->message = $message;
98
		$this->linkGenerator = $linkGenerator;
99
		$this->logger = $logger;
100
		$this->args = $args;
101
102
		$this->template = $templateFactory->createTemplate();
103
104
		/**
105
		 * Initiate mail composing
106
		 */
107
		$this->compose($this->message, $this->args);
108
	}
109
110
111
	/**
112
	 * Composing message (adding <from>, <to>, etc)
113
	 * @param  Nette\Mail\Message $message
114
	 * @param  array|NULL         $params
115
	 * @return viod
116
	 */
117
	public function compose(Nette\Mail\Message $message, $params = NULL)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
	{
119
	}
120
121
122
	/**
123
	 * Set template file
124
	 * @return void
125
	 */
126
	public function setTemplateFile($template_file)
127
	{
128
		$this->template_file = (string) $template_file;
129
	}
130
131
132
	/**
133
	 * Set template variables
134
	 * @return void
135
	 */
136
	protected function setTemplateVariables()
137
	{
138
		foreach ($this->args as $key => $value) {
139
			$this->template->$key = $value;
140
		}
141
	}
142
143
144
	/**
145
	 * Set absolute base path for images
146
	 * @param string $mail_images_base_path
147
	 */
148
	public function setBasePath($mail_images_base_path)
149
	{
150
		$this->mail_images_base_path = (string) $mail_images_base_path;
151
152
		return $this;
153
	}
154
155
156
	/**
157
	 * Stick to convention that Email:
158
	 * 		?/mailing/Mails/FooMail.php	
159
	 * 
160
	 * will have template with path of:
161
	 * 		?/mailing/Mails/templates/foo_mail.latte
162
	 * 
163
	 * @return string
164
	 */
165
	public function getTemplateFile()
166
	{
167
		if ($this->template_file) {
168
			return $this->template_file;
169
		}
170
171
		/**
172
		 * Get child class file path
173
		 * @var \ReflectionClass
174
		 */
175
		$reflection = new \ReflectionClass(get_class($this));
176
		
177
		/**
178
		 * Split path to directory and file
179
		 */
180
		$class_path = $reflection->getFileName();
181
		$class_dir = dirname($class_path);
182
		$class_name = pathinfo($class_path, PATHINFO_FILENAME);
183
184
		/**
185
		 * Convert class name to underscore and set latte file extension
186
		 */
187
		$this->underscore_name = lcfirst(preg_replace_callback('/(?<=.)([A-Z])/', function ($m) {
188
			return '_' . strtolower($m[1]);
189
		}, $class_name));
190
191
		$template_name = $this->underscore_name . '.latte';
192
		$this->log_type = $this->underscore_name;
193
194
		$template_file = "$class_dir/templates/$template_name";
195
196
		if (!file_exists($template_file)) {
197
			throw new MailingException("Error creating template from file [$template_file]", 1);
198
		}
199
200
		return $template_file;
201
	}
202
203
204
	/**
205
	 * Render latte template to string and send (and/or log) mail
206
	 * @return void
207
	 */
208
	public function send()
209
	{
210
		/**
211
		 * Set template variables
212
		 */
213
		$this->setTemplateVariables();
214
215
		/**
216
		 * Set body/html body
217
		 */
218
		try {
219
			$this->template->setFile($this->getTemplateFile());
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