Test Failed
Branch devel (807472)
by Litera
06:16
created

Emailer::sendMail()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 8
nop 4
dl 0
loc 30
rs 8.439
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
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
		$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
		$this->settings = $settings;
31
	}
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(array $recipient, $subject, $body, array $bccMail = NULL)
43
	{
44
		$message = new Message;
45
		$message->setFrom('[email protected]', 'Srazy VS');
46
47
		foreach($recipient as $mail => $name) {
48
			// add recipient address and name
49
			$message->addTo($mail, $name);
50
		}
51
		// add bcc
52
		if(!empty($bccMail)) {
53
			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...
54
				$message->addBcc($bccMail, $bccName);
55
			}
56
		}
57
		// create subject
58
		$message->subject = $subject;
59
		// create HTML body
60
		$message->htmlBody = $body;
61
		// create alternative message without HTML tags
62
		$message->body = strip_tags($body);
63
		// sending e-mail or error status
64
		try {
65
			$this->mailer->send($message);
66
			return true;
67
		} 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...
68
			Debugger::log($e, 'error');
69
			return false;
70
		}
71
	}
72
73
	/**
74
	 * Get e-mail template from settings
75
	 *
76
	 * @param	string	type of template
77
	 * @return	array	subject and message
78
	 */
79
	public function getTemplate($type)
80
	{
81
		$json = $this->settings->getMailJSON($type);
82
83
		$subject = html_entity_decode($json->subject);
84
		$message = html_entity_decode($json->message);
85
86
		return array(
87
			'subject' => $subject,
88
			'message' => $message,
89
		);
90
	}
91
92
	/**
93
	 * Sends an e-mail to lecture master
94
	 *
95
	 * @param	array	recipient mail and name
96
	 * @param	string	guid
97
	 * @param	string	program | block
98
	 * @return	mixed	true | error information
99
	 */
100
	public function tutor(array $recipients, $guid, $type)
101
	{
102
		$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...
Coding Style Comprehensibility introduced by
The string literal bloku does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
103
		$lang['program']['cs'] = "programu";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal programu does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
104
105
		$tutorFormUrl = PRJ_DIR . $type . "/annotation/" . $guid;
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal /annotation/ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

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