Emailer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
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
			$message->addTo(
49 1
			    $recipient->email,
50 1
                trim($recipient->name . ' ' . $recipient->surname)
51
            );
52
		}
53
		// add bcc
54 1
		if(!empty($bccMail)) {
55 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...
56 1
				$message->addBcc($bccMail, $bccName);
57
			}
58
		}
59
		// create subject
60 1
		$message->subject = $subject;
61
		// create HTML body
62 1
		$message->htmlBody = $body;
63
		// create alternative message without HTML tags
64 1
		$message->body = strip_tags($body);
65
		// sending e-mail or error status
66
		try {
67 1
			$this->mailer->send($message);
68 1
			return true;
69
		} 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...
70
			Debugger::log($e, 'error');
71
			return false;
72
		}
73
	}
74
75
	/**
76
	 * Get e-mail template from settings
77
	 *
78
	 * @param	string	type of template
79
	 * @return	array	subject and message
80
	 */
81
	public function getTemplate($type)
82
	{
83 1
		$json = $this->settings->getMailJSON($type);
84
85 1
		$subject = html_entity_decode($json->subject);
86 1
		$message = html_entity_decode($json->message);
87
88
		return array(
89 1
			'subject' => $subject,
90 1
			'message' => $message,
91
		);
92
	}
93
94
	/**
95
	 * Sends an e-mail to lecture master
96
	 *
97
	 * @param	array	recipient mail and name
98
	 * @param	string	guid
99
	 * @param	string	program | block
100
	 * @return	mixed	true | error information
101
	 */
102
	public function tutor($recipients, $guid, $type)
103
	{
104 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...
105 1
		$lang['program']['cs'] = "programu";
106
107 1
		$tutorFormUrl = PRJ_DIR . "annotation/edit/{$type}/{$guid}";
108
109
		// e-mail templates
110 1
		$template = $this->getTemplate('tutor');
111 1
		$subject = $template['subject'];
112 1
		$message = $template['message'];
113
114
		// replacing text variables
115 1
		$subject = preg_replace('/%%\[typ-anotace\]%%/', $lang[$type]['cs'], $subject);
116 1
		$message = preg_replace('/%%\[typ-anotace\]%%/', $lang[$type]['cs'], $message);
117 1
		$message = preg_replace('/%%\[url-formulare\]%%/', $tutorFormUrl, $message);
118
119
		// send it
120 1
		return $this->sendMail($recipients, $subject, $message);
121
	}
122
123
	/**
124
	 * Sends an after registration summary e-mail to visitor
125
	 *
126
	 * @param	array	recipient mail
127
	 * @param	int		check hash code
128
	 * @param	string	code for recognition of bank transaction
129
	 * @return	mixed	true | error information
130
	 */
131
	public function sendRegistrationSummary(array $recipientMail, $hash, $code4bank)
132
	{
133
		// e-mail templates
134 1
		$template = $this->getTemplate('post_reg');
135 1
		$subject = $template['subject'];
136 1
		$message = $template['message'];
137
138
		// replacing text variables
139 1
		$message = preg_replace('/%%\[kontrolni-hash\]%%/', $hash, $message);
140 1
		$message = preg_replace('/%%\[variabilni-symbol\]%%/', $code4bank, $message);
141
142
		// send it
143 1
		return $this->sendMail($recipientMail, $subject, $message);
144
	}
145
146
	/**
147
	 * Get e-mail templates from settings
148
	 *
149
	 * @param	mixed	id numbers in row
150
	 * @param	string	type of template
151
	 * @return	array	subject and message
152
	 */
153
	public function sendPaymentInfo($recipients, $type)
154
	{
155
		// e-mail templates
156 1
		$template = $this->getTemplate($type);
157 1
		$subject = $template['subject'];
158 1
		$message = $template['message'];
159
160 1
		return $this->sendMail($recipients, $subject, $message);
161
	}
162
}
163