Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

ExpireVersions::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 2
dl 11
loc 11
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 Victor Dubiniuk <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Files_Versions\BackgroundJob;
25
26
use OCP\IUser;
27
use OCP\IUserManager;
28
use OCA\Files_Versions\AppInfo\Application;
29
use OCA\Files_Versions\Storage;
30
use OCA\Files_Versions\Expiration;
31
32
class ExpireVersions extends \OC\BackgroundJob\TimedJob {
33
34
	const ITEMS_PER_SESSION = 1000;
35
36
	/**
37
	 * @var Expiration
38
	 */
39
	private $expiration;
40
	
41
	/**
42
	 * @var IUserManager
43
	 */
44
	private $userManager;
45
46 View Code Duplication
	public function __construct(IUserManager $userManager = null, Expiration $expiration = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
		// Run once per 30 minutes
48
		$this->setInterval(60 * 30);
49
50
		if (is_null($expiration) || is_null($userManager)) {
51
			$this->fixDIForJobs();
52
		} else {
53
			$this->expiration = $expiration;
54
			$this->userManager = $userManager;
55
		}
56
	}
57
58
	protected function fixDIForJobs() {
59
		$application = new Application();
60
		$this->expiration = $application->getContainer()->query('Expiration');
61
		$this->userManager = \OC::$server->getUserManager();
62
	}
63
64
	protected function run($argument) {
65
		$maxAge = $this->expiration->getMaxAgeAsTimestamp();
66
		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...
67
			return;
68
		}
69
70
		$this->userManager->callForAllUsers(function(IUser $user) {
71
			$uid = $user->getUID();
72
			if (!$this->setupFS($uid)) {
73
				return;
74
			}
75
			Storage::expireOlderThanMaxForUser($uid);
76
		});
77
	}
78
79
	/**
80
	 * Act on behalf on trash item owner
81
	 * @param string $user
82
	 * @return boolean
83
	 */
84 View Code Duplication
	protected function setupFS($user) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
		\OC_Util::tearDownFS();
86
		\OC_Util::setupFS($user);
87
88
		// Check if this user has a versions directory
89
		$view = new \OC\Files\View('/' . $user);
90
		if (!$view->is_dir('/files_versions')) {
91
			return false;
92
		}
93
94
		return true;
95
	}
96
}
97