Completed
Push — stable8.2 ( 8f2759...8bd3ea )
by Thomas
18:30
created

ExpireTrash::callForAllUsers()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 15

Duplication

Lines 20
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 20
loc 20
ccs 0
cts 9
cp 0
rs 8.8571
cc 6
eloc 15
nc 5
nop 2
crap 42
1
<?php
2
/**
3
 * @author Lukas Reschke <[email protected]>
4
 * @author Victor Dubiniuk <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Files_Trashbin\BackgroundJob;
24
25
use OCP\IConfig;
26
use OCP\IUser;
27
use OCP\IUserManager;
28
use OCA\Files_Trashbin\AppInfo\Application;
29
use OCA\Files_Trashbin\Expiration;
30
use OCA\Files_Trashbin\Helper;
31
use OCA\Files_Trashbin\Trashbin;
32
33
class ExpireTrash extends \OC\BackgroundJob\TimedJob {
34
35
	/**
36
	 * @var Expiration
37
	 */
38
	private $expiration;
39
	
40
	/**
41
	 * @var IUserManager
42
	 */
43
	private $userManager;
44
45
	/**
46
	 * @param IUserManager|null $userManager
47
	 * @param Expiration|null $expiration
48
	 */
49 View Code Duplication
	public function __construct(IUserManager $userManager = null,
50
								Expiration $expiration = null) {
51
		// Run once per 30 minutes
52
		$this->setInterval(60 * 30);
53
54
		if (is_null($expiration) || is_null($userManager)) {
55
			$this->fixDIForJobs();
56
		} else {
57
			$this->userManager = $userManager;
58 20
			$this->expiration = $expiration;
59
		}
60
	}
61
62 20
	protected function fixDIForJobs() {
63
		$application = new Application();
64 20
		$this->userManager = \OC::$server->getUserManager();
65 19
		$this->expiration = $application->getContainer()->query('Expiration');
66 19
	}
67 1
68 1
	/**
69 1
	 * @param $argument
70
	 * @throws \Exception
71 20
	 */
72
	protected function run($argument) {
73 19
		$maxAge = $this->expiration->getMaxAgeAsTimestamp();
74 19
		if (!$maxAge) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxAge of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
75 19
			return;
76 19
		}
77 19
78 19
		$this->callForAllUsers(function(IUser $user) {
79
			$uid = $user->getUID();
80
			if (!$this->setupFS($uid)) {
81
				return;
82
			}
83
			$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
84 1
			Trashbin::deleteExpiredFiles($dirContent, $uid);
85 1
		});
86 1
		
87 1
		\OC_Util::tearDownFS();
88
	}
89
90
	/**
91
	 * Act on behalf on trash item owner
92
	 * @param string $user
93
	 * @return boolean
94
	 */
95 View Code Duplication
	protected function setupFS($user) {
96
		\OC_Util::tearDownFS();
97
		\OC_Util::setupFS($user);
98
99
		// Check if this user has a trashbin directory
100
		$view = new \OC\Files\View('/' . $user);
101
		if (!$view->is_dir('/files_trashbin/files')) {
102
			return false;
103
		}
104
105
		return true;
106
	}
107
108
	/**
109
	 * The callback is executed for each user on each backend.
110
	 * If the callback returns false no further users will be retrieved.
111
	 *
112
	 * @param \Closure $callback
113
	 * @param string $search
114
	 */
115 View Code Duplication
	protected function callForAllUsers(\Closure $callback, $search = '') {
116
		foreach ($this->userManager->getBackends() as $backend) {
117
			$limit = 500;
118
			$offset = 0;
119
			do {
120
				$users = $backend->getUsers($search, $limit, $offset);
121
				foreach ($users as $user) {
122
					$user = $this->userManager->get($user);
123
					if (is_null($user)) {
124
						continue;
125
					}
126
					$return = $callback($user);
127
					if ($return === false) {
128
						break;
129
					}
130
				}
131
				$offset += $limit;
132
			} while (count($users) >= $limit);
133
		}
134
	}
135
}
136