Completed
Push — stable8.2 ( bbfc5b...98e94d )
by Morris
71:01
created

ExpireTrash   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 42.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 13
dl 0
loc 103
ccs 19
cts 45
cp 0.4222
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A fixDIForJobs() 0 6 1
B run() 0 28 5
A setupFS() 0 16 3
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\IUserManager;
27
use OCA\Files_Trashbin\AppInfo\Application;
28
use OCA\Files_Trashbin\Expiration;
29
use OCA\Files_Trashbin\Helper;
30
use OCA\Files_Trashbin\Trashbin;
31
32
class ExpireTrash extends \OC\BackgroundJob\TimedJob {
33
34
	const ITEMS_PER_SESSION = 1000;
35
36
	/**
37
	 * @var Expiration
38
	 */
39
	private $expiration;
40
41
	/**
42
	 * @var IConfig
43
	 */
44
	private $config;
45
	
46
	/**
47
	 * @var IUserManager
48
	 */
49
	private $userManager;
50
	
51
	const USERS_PER_SESSION = 1000;
52
53
	/**
54
	 * @param IConfig|null $config
55
	 * @param IUserManager|null $userManager
56
	 * @param Expiration|null $expiration
57
	 */
58 20
	public function __construct(IConfig $config = null,
59
								IUserManager $userManager = null,
60
								Expiration $expiration = null) {
61
		// Run once per 30 minutes
62 20
		$this->setInterval(60 * 30);
63
64 20
		if (is_null($expiration) || is_null($userManager) || is_null($config)) {
65 19
			$this->fixDIForJobs();
66 19
		} else {
67 1
			$this->config = $config;
68 1
			$this->userManager = $userManager;
69 1
			$this->expiration = $expiration;
70
		}
71 20
	}
72
73 19
	protected function fixDIForJobs() {
74 19
		$application = new Application();
75 19
		$this->config = \OC::$server->getConfig();
76 19
		$this->userManager = \OC::$server->getUserManager();
77 19
		$this->expiration = $application->getContainer()->query('Expiration');
78 19
	}
79
80
	/**
81
	 * @param $argument
82
	 * @throws \Exception
83
	 */
84 1
	protected function run($argument) {
85 1
		$maxAge = $this->expiration->getMaxAgeAsTimestamp();
86 1
		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...
87 1
			return;
88
		}
89
		
90
		$offset = $this->config->getAppValue('files_trashbin', 'cronjob_user_offset', 0);
91
		$users = $this->userManager->search('', self::USERS_PER_SESSION, $offset);
92
		if (!count($users)) {
93
			// No users found, reset offset and retry
94
			$offset = 0;
95
			$users = $this->userManager->search('', self::USERS_PER_SESSION);
96
		}
97
		
98
		$offset += self::USERS_PER_SESSION;
99
		$this->config->setAppValue('files_trashbin', 'cronjob_user_offset', $offset);
100
		
101
		foreach ($users as $user) {
102
			$uid = $user->getUID();
103
			if (!$this->setupFS($uid)) {
104
				continue;
105
			}
106
			$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
107
			Trashbin::deleteExpiredFiles($dirContent, $uid);
108
		}
109
		
110
		\OC_Util::tearDownFS();
111
	}
112
113
	/**
114
	 * Act on behalf on trash item owner
115
	 * @param string $user
116
	 * @return boolean
117
	 */
118
	private function setupFS($user){
119
		if (!$this->userManager->userExists($user)) {
120
			return false;
121
		}
122
123
		//Check if this user has a trashbin directory
124
		$view = new \OC\Files\View('/' . $user);
125
		if (!$view->is_dir('/files_trashbin/files')){
126
			return false;
127
		}
128
129
		\OC_Util::tearDownFS();
130
		\OC_Util::setupFS($user);
131
132
		return true;
133
	}
134
}
135