Completed
Push — master ( 68dc6a...95592f )
by Morris
16:49
created

ExpireVersions::run()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Jörn Friedrich Dreyer <[email protected]>
7
 * @author Victor Dubiniuk <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Files_Versions\BackgroundJob;
26
27
use OCP\IUser;
28
use OCP\IUserManager;
29
use OCA\Files_Versions\AppInfo\Application;
30
use OCA\Files_Versions\Storage;
31
use OCA\Files_Versions\Expiration;
32
33
class ExpireVersions extends \OC\BackgroundJob\TimedJob {
34
35
	const ITEMS_PER_SESSION = 1000;
36
37
	/**
38
	 * @var Expiration
39
	 */
40
	private $expiration;
41
	
42
	/**
43
	 * @var IUserManager
44
	 */
45
	private $userManager;
46
47
	public function __construct(IUserManager $userManager, Expiration $expiration) {
48
		// Run once per 30 minutes
49
		$this->setInterval(60 * 30);
50
51
		$this->expiration = $expiration;
52
		$this->userManager = $userManager;
53
	}
54
55
	protected function run($argument) {
56
		$maxAge = $this->expiration->getMaxAgeAsTimestamp();
57
		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...
58
			return;
59
		}
60
61
		$this->userManager->callForSeenUsers(function(IUser $user) {
62
			$uid = $user->getUID();
63
			if (!$this->setupFS($uid)) {
64
				return;
65
			}
66
			Storage::expireOlderThanMaxForUser($uid);
67
		});
68
	}
69
70
	/**
71
	 * Act on behalf on trash item owner
72
	 * @param string $user
73
	 * @return boolean
74
	 */
75 View Code Duplication
	protected function setupFS($user) {
76
		\OC_Util::tearDownFS();
77
		\OC_Util::setupFS($user);
78
79
		// Check if this user has a versions directory
80
		$view = new \OC\Files\View('/' . $user);
81
		if (!$view->is_dir('/files_versions')) {
82
			return false;
83
		}
84
85
		return true;
86
	}
87
}
88