Completed
Pull Request — master (#129)
by Jan-Christoph
52:33 queued 50:39
created

MailQueueHandler::sendEmailToUser()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 62
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 5.0021

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 62
ccs 43
cts 45
cp 0.9556
rs 8.6652
cc 5
eloc 45
nc 7
nop 5
crap 5.0021

How to fix   Long Method   

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
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Activity;
25
26
use OCA\Activity\Extension\LegacyParser;
27
use OCP\Activity\IEvent;
28
use OCP\Activity\IManager;
29
use OCP\DB\QueryBuilder\IQueryBuilder;
30
use OCP\Defaults;
31
use OCP\IDateTimeFormatter;
32
use OCP\IDBConnection;
33
use OCP\IURLGenerator;
34
use OCP\IUser;
35
use OCP\IUserManager;
36
use OCP\L10N\IFactory;
37
use OCP\Mail\IMailer;
38
use OCP\Template;
39
use OCP\Util;
40
41
/**
42
 * Class MailQueueHandler
43
 * Gets the users from the database and
44
 *
45
 * @package OCA\Activity
46
 */
47
class MailQueueHandler {
48
	/** Number of entries we want to list in the email */
49
	const ENTRY_LIMIT = 200;
50
51
	/** @var array */
52
	protected $languages;
53
54
	/** @var string */
55
	protected $senderAddress;
56
57
	/** @var string */
58
	protected $senderName;
59
60
	/** @var IDateTimeFormatter */
61
	protected $dateFormatter;
62
63
	/** @var DataHelper */
64
	protected $dataHelper;
65
66
	/** @var IDBConnection */
67
	protected $connection;
68
69
	/** @var IMailer */
70
	protected $mailer;
71
72
	/** @var IURLGenerator */
73
	protected $urlGenerator;
74
75
	/** @var IUserManager */
76
	protected $userManager;
77
78
	/** @var IFactory */
79
	protected $lFactory;
80
81
	/** @var IManager */
82
	protected $activityManager;
83
84
	/** @var LegacyParser */
85
	protected $legacyParser;
86
87
	/**
88
	 * Constructor
89
	 *
90
	 * @param IDateTimeFormatter $dateFormatter
91
	 * @param IDBConnection $connection
92
	 * @param DataHelper $dataHelper
93
	 * @param IMailer $mailer
94
	 * @param IURLGenerator $urlGenerator
95
	 * @param IUserManager $userManager
96
	 * @param IFactory $lFactory
97
	 * @param IManager $activityManager
98
	 * @param LegacyParser $legacyParser
99
	 */
100 10
	public function __construct(IDateTimeFormatter $dateFormatter,
101
								IDBConnection $connection,
102
								DataHelper $dataHelper,
103
								IMailer $mailer,
104
								IURLGenerator $urlGenerator,
105
								IUserManager $userManager,
106
								IFactory $lFactory,
107
								IManager $activityManager,
108
								LegacyParser $legacyParser) {
109 10
		$this->dateFormatter = $dateFormatter;
110 10
		$this->connection = $connection;
111 10
		$this->dataHelper = $dataHelper;
112 10
		$this->mailer = $mailer;
113 10
		$this->urlGenerator = $urlGenerator;
114 10
		$this->userManager = $userManager;
115 10
		$this->lFactory = $lFactory;
116 10
		$this->activityManager = $activityManager;
117 10
		$this->legacyParser = $legacyParser;
118 10
	}
119
120
	/**
121
	 * Get the users we want to send an email to
122
	 *
123
	 * @param int|null $limit
124
	 * @param int $latestSend
125
	 * @return array
126
	 */
127 6
	public function getAffectedUsers($limit, $latestSend) {
128 6
		$limit = (!$limit) ? null : (int) $limit;
129
130 6
		$query = $this->connection->prepare(
131
			'SELECT `amq_affecteduser`, MIN(`amq_latest_send`) AS `amq_trigger_time` '
132
			. ' FROM `*PREFIX*activity_mq` '
133 6
			. ' WHERE `amq_latest_send` < ? '
134 6
			. ' GROUP BY `amq_affecteduser` '
135 6
			. ' ORDER BY `amq_trigger_time` ASC',
136 6
			$limit);
137 6
		$query->execute(array($latestSend));
138
139 6
		$affectedUsers = array();
140 6
		while ($row = $query->fetch()) {
141 6
			$affectedUsers[] = $row['amq_affecteduser'];
142 6
		}
143
144 6
		return $affectedUsers;
145
	}
146
147
	/**
148
	 * Get all items for the user we want to send an email to
149
	 *
150
	 * @param string $affectedUser
151
	 * @param int $maxTime
152
	 * @param int $maxNumItems
153
	 * @return array [data of the first max. 200 entries, total number of entries]
154
	 */
155 7
	protected function getItemsForUser($affectedUser, $maxTime, $maxNumItems = self::ENTRY_LIMIT) {
156 7
		$query = $this->connection->prepare(
157
			'SELECT * '
158
			. ' FROM `*PREFIX*activity_mq` '
159 7
			. ' WHERE `amq_timestamp` <= ? '
160 7
			. ' AND `amq_affecteduser` = ? '
161 7
			. ' ORDER BY `amq_timestamp` ASC',
162
			$maxNumItems
163 7
		);
164 7
		$query->execute([(int) $maxTime, $affectedUser]);
165
166 7
		$activities = array();
167 7
		while ($row = $query->fetch()) {
168 7
			$activities[] = $row;
169 7
		}
170
171 7
		if (isset($activities[$maxNumItems - 1])) {
172
			// Reached the limit, run a query to get the actual count.
173 1
			$query = $this->connection->prepare(
174
				'SELECT COUNT(*) AS `actual_count`'
175
				. ' FROM `*PREFIX*activity_mq` '
176 1
				. ' WHERE `amq_timestamp` <= ? '
177 1
				. ' AND `amq_affecteduser` = ?'
178 1
			);
179 1
			$query->execute([(int) $maxTime, $affectedUser]);
180
181 1
			$row = $query->fetch();
182 1
			return [$activities, $row['actual_count'] - $maxNumItems];
183
		} else {
184 7
			return [$activities, 0];
185
		}
186
	}
187
188
	/**
189
	 * Get a language object for a specific language
190
	 *
191
	 * @param string $lang Language identifier
192
	 * @return \OCP\IL10N Language object of $lang
193
	 */
194 1
	protected function getLanguage($lang) {
195 1
		if (!isset($this->languages[$lang])) {
196 1
			$this->languages[$lang] = $this->lFactory->get('activity', $lang);
197 1
		}
198
199 1
		return $this->languages[$lang];
200
	}
201
202
	/**
203
	 * Get the sender data
204
	 * @param string $setting Either `email` or `name`
205
	 * @return string
206
	 */
207 1
	protected function getSenderData($setting) {
208 1
		if (empty($this->senderAddress)) {
209 1
			$this->senderAddress = Util::getDefaultEmailAddress('no-reply');
210 1
		}
211 1
		if (empty($this->senderName)) {
212 1
			$defaults = new Defaults();
213 1
			$this->senderName = $defaults->getName();
214 1
		}
215
216 1
		if ($setting === 'email') {
217 1
			return $this->senderAddress;
218
		}
219 1
		return $this->senderName;
220
	}
221
222
	/**
223
	 * Send a notification to one user
224
	 *
225
	 * @param string $userName Username of the recipient
226
	 * @param string $email Email address of the recipient
227
	 * @param string $lang Selected language of the recipient
228
	 * @param string $timezone Selected timezone of the recipient
229
	 * @param int $maxTime
230
	 * @return bool True if the entries should be removed, false otherwise
231
	 */
232 1
	public function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime) {
233 1
		$user = $this->userManager->get($userName);
234 1
		if (!$user instanceof IUser) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
235 1
			return true;
236
		}
237
238 1
		list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime);
239
240 1
		$l = $this->getLanguage($lang);
241 1
		$this->dataHelper->setUser($userName);
242 1
		$this->dataHelper->setL10n($l);
243 1
		$this->activityManager->setCurrentUserId($userName);
244
245 1
		$activityList = array();
246 1
		foreach ($mailData as $activity) {
247 1
			$event = $this->activityManager->generateEvent();
248 1
			$event->setApp($activity['amq_appid'])
249 1
				->setType($activity['amq_type'])
250 1
				->setTimestamp((int) $activity['amq_timestamp'])
251 1
				->setSubject($activity['amq_subject'], json_decode($activity['amq_subjectparams'], true));
252
253 1
			$relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay(
254 1
				$activity['amq_timestamp'],
255 1
				'long', 'short',
256 1
				new \DateTimeZone($timezone), $l
257 1
			);
258
259
			try {
260 1
				$event = $this->parseEvent($lang, $event);
261 1
			} catch (\InvalidArgumentException $e) {
262
				continue;
263
			}
264
265 1
			$activityList[] = array(
266 1
				$event->getParsedSubject(),
267 1
				$relativeDateTime,
268
			);
269 1
		}
270
271 1
		$alttext = new Template('activity', 'email.notification', '', false);
272 1
		$alttext->assign('username', $user->getDisplayName());
273 1
		$alttext->assign('activities', $activityList);
274 1
		$alttext->assign('skippedCount', $skippedCount);
275 1
		$alttext->assign('installation', $this->urlGenerator->getAbsoluteURL('/'));
276 1
		$alttext->assign('overwriteL10N', $l);
277 1
		$emailText = $alttext->fetchPage();
278
279 1
		$message = $this->mailer->createMessage();
280 1
		$message->setTo([$email => $user->getDisplayName()]);
281 1
		$message->setSubject((string) $l->t('Activity notification'));
282 1
		$message->setPlainBody($emailText);
283 1
		$message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]);
284
285
		try {
286 1
			$this->mailer->send($message);
287 1
		} catch (\Exception $e) {
288
			return false;
289
		}
290
291 1
		$this->activityManager->setCurrentUserId(null);
292 1
		return true;
293
	}
294
295
	/**
296
	 * @param string $lang
297
	 * @param IEvent $event
298
	 * @return IEvent
299
	 * @throws \InvalidArgumentException when the event could not be parsed
300
	 */
301 1
	protected function parseEvent($lang, IEvent $event) {
302 1
		foreach ($this->activityManager->getProviders() as $provider) {
303
			try {
304
				$this->activityManager->setFormattingObject($event->getObjectType(), $event->getObjectId());
305
				$event = $provider->parse($lang, $event);
306
				$this->activityManager->setFormattingObject('', 0);
307
			} catch (\InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
308
			}
309 1
		}
310
311 1
		if (!$event->getParsedSubject()) {
312 1
			$this->activityManager->setFormattingObject($event->getObjectType(), $event->getObjectId());
313 1
			$event = $this->legacyParser->parse($lang, $event);
314 1
			$this->activityManager->setFormattingObject('', 0);
315 1
		}
316
317 1
		return $event;
318
	}
319
320
	/**
321
	 * Delete all entries we dealt with
322
	 *
323
	 * @param array $affectedUsers
324
	 * @param int $maxTime
325
	 */
326 5
	public function deleteSentItems(array $affectedUsers, $maxTime) {
327 5
		if (empty($affectedUsers)) {
328
			return;
329
		}
330
331 5
		$query = $this->connection->getQueryBuilder();
332 5
		$query->delete('activity_mq')
333 5
			->where($query->expr()->lte('amq_timestamp', $query->createNamedParameter($maxTime, IQueryBuilder::PARAM_INT)))
334 5
			->andWhere($query->expr()->in('amq_affecteduser', $query->createNamedParameter($affectedUsers, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR));
335 5
		$query->execute();
336 5
	}
337
}
338