notifications_email::send()   B
last analyzed

Complexity

Conditions 10
Paths 12

Size

Total Lines 49
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 1
b 0
f 0
nc 12
nop 5
dl 0
loc 49
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * EGroupware - Notifications
4
 *
5
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
6
 * @package notifications
7
 * @subpackage backends
8
 * @link http://www.egroupware.org
9
 * @author Christian Binder <[email protected]>
10
 * @version $Id$
11
 */
12
13
use EGroupware\Api;
14
15
/**
16
 * User notification via email.
17
 */
18
class notifications_email implements notifications_iface {
19
20
	/**
21
	 * Appname
22
	 */
23
	const _appname = 'notifications';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected _APPNAME).
Loading history...
24
25
	/**
26
	 * holds account object for user who sends the message
27
	 *
28
	 * @var object
29
	 */
30
	private $sender;
31
32
	/**
33
	 * holds account object for user to notify
34
	 *
35
	 * @var object
36
	 */
37
	private $recipient;
38
39
	/**
40
	 * holds config object (sitewide application config)
41
	 *
42
	 * @var object
43
	 */
44
	private $config;
45
46
	/**
47
	 * holds preferences object of user to notify
48
	 *
49
	 * @var Api\Preferences
50
	 */
51
	private $preferences;
52
53
	/**
54
	 * holds mail object
55
	 *
56
	 * @var send
57
	 */
58
	private $mail;
59
60
	/**
61
	 * constructor of notifications_email
62
	 *
63
	 * @param object $_sender
64
	 * @param object $_recipient
65
	 * @param object $_config
66
	 * @param object $_preferences
67
	 */
68
	public function __construct($_sender, $_recipient, $_config = null, $_preferences = null) {
69
		if(!is_object($_sender)) { throw new Exception("no sender given."); }
70
		if(!is_object($_recipient)) { throw new Exception("no recipient given."); }
71
		$this->sender = $_sender;
72
		$this->recipient = $_recipient;
73
		$this->config = $_config;
74
		$this->preferences = $_preferences;
75
		if(is_object($this->mail))
76
		{
77
			unset($this->mail);
78
		}
79
		$this->mail = new Api\Mailer();
0 ignored issues
show
Documentation Bug introduced by
It seems like new EGroupware\Api\Mailer() of type EGroupware\Api\Mailer is incompatible with the declared type send of property $mail.

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...
80
	}
81
82
	/**
83
	 * sends notification
84
	 *
85
	 * @param array $_messages
86
	 * @param string $_subject
87
	 * @param array $_links
88
	 * @param array $_attachments
89
	 * @param array $_data value for key "reply_to" to use a custom ReplyTo address
90
	 */
91
	public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false)
92
	{
93
		$body_plain = $_messages['plain'].$this->render_links($_links, false, $this->preferences->external_mailclient);
0 ignored issues
show
Bug introduced by
The property external_mailclient does not seem to exist on EGroupware\Api\Preferences.
Loading history...
94
		$body_html = "<html><body>\n".$_messages['html'].$this->render_links($_links, true, $this->preferences->external_mailclient)."</body>\n</html>\n";
95
96
		$this->mail->ClearAddresses();
97
		$this->mail->ClearAttachments();
98
		$this->mail->addAddress($this->recipient->account_email, $this->recipient->account_fullname);
99
		$this->mail->addHeader('X-EGroupware-Type', 'notification-mail');
100
		$this->mail->addHeader('X-EGroupware-Install', $GLOBALS['egw_info']['server']['install_id'].'@'.$GLOBALS['egw_info']['server']['default_domain']);
101
		//$this->mail->AddHeader('X-EGroupware-URL', 'notification-mail');
102
		//$this->mail->AddHeader('X-EGroupware-Tracker', 'notification-mail');
103
		//error_log(__METHOD__.__LINE__."preparing notification message via email.".array2string($this->mail));
104
105
		if ( $_data && !empty( $_data['reply_to'] ) )
106
		{
107
			$this->mail->addReplyTo($_data['reply_to']);
108
		}
109
		// do NOT set sender as From, as this might not be allowed, set it instead as ReplyTo, if that one it not explicitly set already
110
		elseif ($this->mail->getHeader('From') != Api\Mailer::add_personal($this->sender->account_email, $this->sender->account_fullname))
111
		{
112
			$this->mail->addReplyTo($this->sender->account_email, $this->sender->account_fullname);
113
		}
114
115
		$this->mail->addHeader('Subject', trim($_subject)); // trim the subject to avoid strange wrong encoding problem
116
		if ($_messages['html'])
117
		{
118
			// embed images as inline
119
			\EGroupware\Api\Mail::processURL2InlineImages($this->mail, $body_html, null);
120
		}
121
		$this->mail->setHtmlBody($body_html, null, false);	// no automatic alternativ
122
		$this->mail->setBody($body_plain);
123
124
		if(is_array($_attachments) && count($_attachments) > 0)
125
		{
126
			foreach($_attachments as $attachment)
127
			{
128
				if ($attachment->string)
129
				{
130
					$this->mail->AddStringAttachment($attachment->string, $attachment->filename, $attachment->encoding, $attachment->type);
131
				}
132
				elseif($attachment->path)
133
				{
134
					$this->mail->AddAttachment($attachment->path, $attachment->filename, $attachment->encoding, $attachment->type);
135
				}
136
	  		}
137
		}
138
		//error_log(__METHOD__.__LINE__."about sending notification message via email.".array2string($this->mail));
139
		$this->mail->send();
140
	}
141
142
	/**
143
	 * renders plaintext/html links from given link array
144
	 *
145
	 * @param array $_links
146
	 * @param boolean $_render_html
147
	 * @param boolean $_render_external
148
	 * @return plain or html rendered link(s) as complete string
0 ignored issues
show
Bug introduced by
The type plain was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
149
	 */
150
	private function render_links($_links = false, $_render_html = false, $_render_external = true) {
151
		if(!is_array($_links) || count($_links) == 0) { return false; }
152
153
		// provide defaults if given arguments are null
154
		// php distinguishes between missing and present(null) arguments
155
		if(is_null($_render_html)) { $_render_html = false; }
0 ignored issues
show
introduced by
The condition is_null($_render_html) is always false.
Loading history...
156
		if(is_null($_render_external)) { $_render_external = true; }
0 ignored issues
show
introduced by
The condition is_null($_render_external) is always false.
Loading history...
157
		$newline = $_render_html ? "<br />" : "\n";
158
		$hruler = $_render_html ? Api\Html::hr() : '';
159
160
		$rendered_links = array();
161
		foreach($_links as $link) {
162
			if($_render_external || ! $link->popup) { $link->view['no_popup'] = 1; }
163
			// do not expose sensitive data
164
			$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',Api\Html::link('/index.php', $link->view));
165
			// complete missing protocol and domain part if needed
166
			if ($url[0] == '/' && $_render_external) $url = Api\Framework::getUrl($url);
167
			$a_href = '<a href="'.$url.'" target="_blank">'.$link->text.'</a>';
168
			$rendered_links[] = $_render_html ? $a_href : $url;
169
		}
170
171
		return $hruler.$newline.lang('Linked entries:').$newline.implode($newline,$rendered_links);
172
	}
173
174
}
175