Completed
Pull Request — devel (#144)
by Litera
03:43
created

Emailer::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 12
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Services;
4
5
use App\Models\SettingsModel;
6
use Nette\Mail\IMailer;
7
use Nette\Mail\Message;
8
use Tracy\Debugger;
9
10
/**
11
 * Emailer
12
 *
13
 * Class for hadling and sending e-mails
14
 *
15
 * @created 2011-09-16
16
 * @author Tomas Litera <[email protected]>
17
 */
18 1
class Emailer
19
{
20
	/** @var SmtpMailer */
21
	private $mailer;
22
23
	/** @var SettingsModel */
24
	private $settings;
25
26
	/* Constructor */
27
	public function __construct(SettingsModel $settings, IMailer $mailer)
28
	{
29 1
		$this->mailer = $mailer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $mailer of type object<Nette\Mail\IMailer> is incompatible with the declared type object<App\Services\SmtpMailer> of property $mailer.

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...
30 1
		$this->settings = $settings;
31 1
	}
32
33
	/**
34
	 * Send an e-mail to recipient
35
	 *
36
	 * @param	array	recipient e-mail and name
37
	 * @param	string	subject
38
	 * @param	string	message
39
	 * @param	array	bcc
40
	 * @return	bool	true | false (log the exception)
41
	 */
42
	public function sendMail($recipients, $subject, $body, array $bccMail = NULL)
43
	{
44 1
		$message = new Message;
45 1
		$message->setFrom('[email protected]', 'Srazy VS');
46
47 1
		foreach($recipients as $recipient) {
48 1
			if(array_key_exists('surname', $recipient)) {
49 1
				$name = trim($recipient->name . ' ' . $recipient->surname);
50
			} else {
51
				$name = $recipient->name;
52
			}
53
54 1
			$message->addTo(
55 1
				$recipient->email,
56 1
				$name
57
			);
58
		}
59
		// add bcc
60 1
		if(!empty($bccMail)) {
61 1
			foreach ($bccMail as $bccMail => $bccName) {
0 ignored issues
show
Bug introduced by
The expression $bccMail of type integer|string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
62 1
				$message->addBcc($bccMail, $bccName);
63
			}
64
		}
65
		// create subject
66 1
		$message->subject = $subject;
67
		// create HTML body
68 1
		$message->htmlBody = $body;
69
		// create alternative message without HTML tags
70 1
		$message->body = strip_tags($body);
71
		// sending e-mail or error status
72
		try {
73 1
			$this->mailer->send($message);
74 1
			return true;
75
		} catch(Exception $e) {
0 ignored issues
show
Bug introduced by
The class App\Services\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
76
			Debugger::log($e, 'error');
77
			return false;
78
		}
79
	}
80
81
	/**
82
	 * Get e-mail template from settings
83
	 *
84
	 * @param	string	type of template
85
	 * @return	array	subject and message
86
	 */
87
	public function getTemplate($type)
88
	{
89 1
		$json = $this->settings->getMailJSON($type);
90
91 1
		$subject = html_entity_decode($json->subject);
92 1
		$message = html_entity_decode($json->message);
93
94
		return array(
95 1
			'subject' => $subject,
96 1
			'message' => $message,
97
		);
98
	}
99
100
	/**
101
	 * Sends an e-mail to lecture master
102
	 *
103
	 * @param	array	recipient mail and name
104
	 * @param	string	guid
105
	 * @param	string	program | block
106
	 * @return	mixed	true | error information
107
	 */
108
	public function tutor($recipients, $guid, $type)
109
	{
110 1
		$lang['block']['cs'] = "bloku";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lang was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lang = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
111 1
		$lang['program']['cs'] = "programu";
112
113 1
		$tutorFormUrl = PRJ_DIR . "annotation/edit/{$type}/{$guid}";
114
115
		// e-mail templates
116 1
		$template = $this->getTemplate('tutor');
117 1
		$subject = $template['subject'];
118 1
		$message = $template['message'];
119
120
		// replacing text variables
121 1
		$subject = preg_replace('/%%\[typ-anotace\]%%/', $lang[$type]['cs'], $subject);
122 1
		$message = preg_replace('/%%\[typ-anotace\]%%/', $lang[$type]['cs'], $message);
123 1
		$message = preg_replace('/%%\[url-formulare\]%%/', $tutorFormUrl, $message);
124
125
		// send it
126 1
		return $this->sendMail($recipients, $subject, $message);
127
	}
128
129
	/**
130
	 * Sends an after registration summary e-mail to visitor
131
	 *
132
	 * @param	array	recipient mail
133
	 * @param	int		check hash code
134
	 * @param	string	code for recognition of bank transaction
135
	 * @return	mixed	true | error information
136
	 */
137
	public function sendRegistrationSummary(array $recipientMail, $hash, $code4bank)
138
	{
139
		// e-mail templates
140 1
		$template = $this->getTemplate('post_reg');
141 1
		$subject = $template['subject'];
142 1
		$message = $template['message'];
143
144
		// replacing text variables
145 1
		$message = preg_replace('/%%\[kontrolni-hash\]%%/', $hash, $message);
146 1
		$message = preg_replace('/%%\[variabilni-symbol\]%%/', $code4bank, $message);
147
148
		// send it
149 1
		return $this->sendMail($recipientMail, $subject, $message);
150
	}
151
152
	/**
153
	 * Get e-mail templates from settings
154
	 *
155
	 * @param	mixed	id numbers in row
156
	 * @param	string	type of template
157
	 * @return	array	subject and message
158
	 */
159
	public function sendPaymentInfo($recipients, $type)
160
	{
161
		// e-mail templates
162 1
		$template = $this->getTemplate($type);
163 1
		$subject = $template['subject'];
164 1
		$message = $template['message'];
165
166 1
		return $this->sendMail($recipients, $subject, $message);
167
	}
168
}
169