Completed
Pull Request — master (#610)
by Tom
124:16 queued 122:05
created

Hooks::deleteUserStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1.008
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity;
23
24
use OCA\Activity\AppInfo\Application;
25
use OCP\IDBConnection;
26
27
/**
28
 * Handles the stream and mail queue of a user when he is being deleted
29
 */
30
class Hooks {
31
	/**
32
	 * Delete remaining activities and emails when a user is deleted
33
	 *
34
	 * @param array $params The hook params
35
	 */
36 1
	static public function deleteUser($params) {
37 1
		$connection = \OC::$server->getDatabaseConnection();
38 1
		self::deleteUserStream($params['uid']);
39
		self::deleteUserMailQueue($connection, $params['uid']);
40
	}
41
42
	/**
43
	 * Delete all items of the stream
44
	 *
45
	 * @param string $user
46
	 */
47 1
	static protected function deleteUserStream($user) {
48
		// Delete activity entries
49 1
		$app = new Application();
50
		/** @var Data $activityData */
51 1
		$activityData = $app->getContainer()->query('ActivityData');
52 1
		$activityData->deleteActivities(array('affecteduser' => $user));
53
	}
54
55
	/**
56
	 * Delete all mail queue entries
57
	 *
58
	 * @param IDBConnection $connection
59
	 * @param string $user
60
	 */
61
	static protected function deleteUserMailQueue(IDBConnection $connection, $user) {
62
		// Delete entries from mail queue
63
		$queryBuilder = $connection->getQueryBuilder();
64
65
		$queryBuilder->delete('activity_mq')
66
			->where($queryBuilder->expr()->eq('amq_affecteduser', $queryBuilder->createParameter('user')))
67
			->setParameter('user', $user);
68
		$queryBuilder->execute();
69
	}
70
}
71