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

ExpireVersions::setupFS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, 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\Files_Versions\BackgroundJob;
23
24
use OCP\IUser;
25
use OCP\IUserManager;
26
use OCA\Files_Versions\AppInfo\Application;
27
use OCA\Files_Versions\Storage;
28
use OCA\Files_Versions\Expiration;
29
30
class ExpireVersions extends \OC\BackgroundJob\TimedJob {
31
32
	const ITEMS_PER_SESSION = 1000;
33
34
	/**
35
	 * @var Expiration
36
	 */
37
	private $expiration;
38
	
39
	/**
40
	 * @var IUserManager
41
	 */
42
	private $userManager;
43 19
44 View Code Duplication
	public function __construct(IUserManager $userManager = null, Expiration $expiration = null) {
45 19
		// Run once per 30 minutes
46
		$this->setInterval(60 * 30);
47 19
48 19
		if (is_null($expiration) || is_null($userManager)) {
49 19
			$this->fixDIForJobs();
50
		} else {
51
			$this->expiration = $expiration;
52
			$this->userManager = $userManager;
53 19
		}
54
	}
55 19
56 19
	protected function fixDIForJobs() {
57 19
		$application = new Application();
58 19
		$this->expiration = $application->getContainer()->query('Expiration');
59 19
		$this->userManager = \OC::$server->getUserManager();
60
	}
61
62
	protected function run($argument) {
63
		$maxAge = $this->expiration->getMaxAgeAsTimestamp();
64
		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...
65
			return;
66
		}
67
68
		$this->callForAllUsers(function(IUser $user) {
69
			$uid = $user->getUID();
70
			if (!$this->setupFS($uid)) {
71
				return;
72
			}
73
			Storage::expireOlderThanMaxForUser($uid);
74
		});
75
	}
76
77
	/**
78
	 * Act on behalf on trash item owner
79
	 * @param string $user
80
	 * @return boolean
81
	 */
82 View Code Duplication
	protected function setupFS($user) {
83
		\OC_Util::tearDownFS();
84
		\OC_Util::setupFS($user);
85
86
		// Check if this user has a versions directory
87
		$view = new \OC\Files\View('/' . $user);
88
		if (!$view->is_dir('/files_versions')) {
89
			return false;
90
		}
91
92
		return true;
93
	}
94
95
	/**
96
	 * The callback is executed for each user on each backend.
97
	 * If the callback returns false no further users will be retrieved.
98
	 *
99
	 * @param \Closure $callback
100
	 * @param string $search
101
	 */
102 View Code Duplication
	protected function callForAllUsers(\Closure $callback, $search = '') {
103
		foreach ($this->userManager->getBackends() as $backend) {
104
			$limit = 500;
105
			$offset = 0;
106
			do {
107
				$users = $backend->getUsers($search, $limit, $offset);
108
				foreach ($users as $user) {
109
					$user = $this->userManager->get($user);
110
					if (is_null($user)) {
111
						continue;
112
					}
113
					$return = $callback($user);
114
					if ($return === false) {
115
						break;
116
					}
117
				}
118
				$offset += $limit;
119
			} while (count($users) >= $limit);
120
		}
121
	}
122
}
123